Python Imaging Library(PIL)

PIL官网:http://effbot.org/imagingbook/(查看PIL基本概念)

1.打开影像

from PIL import Image

#打开图片
img=Image.open("image/minons.jpeg")
img.show()

2.影像的size

from PIL import Image

img=Image.open("image/minons.jpeg")

#图片的size
print(img.size)
w,h=img.size
print(w,h)

3.影像的通道

from PIL import Image

img1=Image.open("image/person.jpg")

#通道
bands=img1.getbands()
print(bands)

4.影像的模式

from PIL import Image

img1=Image.open("image/person.jpg")
img2=Image.open("image/horse.jpg")
#模式
mode=img1.mode
print(mode)
print(img2.mode)

5.影像的滤波

from PIL import Image,ImageFilter

#打开图片
img=Image.open("image/minons.jpeg")

img1=img.filter(ImageFilter.CONTOUR)#轮廓滤波
img1.show()

img3=img.filter(ImageFilter.BLUR)#模糊滤波
img3.show()

img4=img.filter(ImageFilter.BoxBlur(radius=20))#自定义模糊。radius:模糊角度
img4.show()

img5=img.filter(ImageFilter.DETAIL)#高清
img5.show()

img6=img.filter(ImageFilter.EDGE_ENHANCE)#高清(锐化更清晰)
img6.show()

img7=img.filter(ImageFilter.EMBOSS())#浮雕效果
img7.show()

img8=img.filter(ImageFilter.EMBOSS())#浮雕效果
img8.show()

6.影像的转换

from PIL import Image

img=Image.open("image/minons.jpeg")

pixel_1=img.getpixel((400,750))#获取像素值(原始影像)
print("原始彩色影像的像素值:",pixel_1)
img8=img.convert("L")#转换为灰度图片
pixel_2=img8.getpixel((400,750))#获取像素值(单通道影像)
print("单通道影像的像素值:",pixel_2)
img9=img8.convert("RGB")#颜色值转不回去
pixel_3=img9.getpixel((400,750))#获取像素值(转换以后影像)
print("转换回去影像的像素值:",pixel_3)
img8.show()

7.影像的缩放

from PIL import Image

img=Image.open("image/minons.jpeg")

img10=img.resize((300,600))
print(img10.size)
img10.show()

8.影像的形状变换

8.1从影像转换为数组

from PIL import Image
import numpy as np

img=Image.open("image/minons.jpeg")

#从影像转数组
img=np.array(img)/255#归一化
print(img)
print(img.shape)#H W C

8.2从数组转换为影像

from PIL import Image
import numpy as np

img=Image.open("image/minons.jpeg")

#从数组转影像
img=np.array(img)
print(img)
img=Image.fromarray(img,"RGB")
img.show()

9.查看影像通道

from PIL import Image
import numpy as np

img=Image.open("image/minons.jpeg")

#取每个通道图影像查看
img=np.array(img)
img=img[:,:,2]
img=Image.fromarray(img)
img.show()

10.影像的数据对象

from PIL import Image

img=Image.open("image/minons.jpeg")

#获取图片数据对象
data=img.getdata()
print(data)
#将数据对象转换为列表
list=list(data)
print(list)
#数据个数
size=img.size
print(size)
w,h=size
print(w*h)
a=len(list)
print(a)

11.影像的复制&粘贴

from PIL import Image,ImageFilter,ImageDraw,ImageFont

img=Image.open("image/minons.jpeg")

#复制
img11=img.copy()
print(img11)
#粘贴(将地球粘贴到小黄人中)
logo=Image.open("image/earth.jpg")
logo=logo.resize((100,100))#缩小。丢数据。
logo=logo.resize((100,100),resample=Image.ANTIALIAS)#重采样
img.paste(logo, (70,300))#粘贴
img.show()

12.影像的等比缩放

from PIL import Image,ImageFilter,ImageDraw,ImageFont

img=Image.open("image/minons.jpeg")

#等比缩放
w,h=img.size
img.thumbnail((w//2,h//2),resample=Image.ANTIALIAS)#resample:重采样。ANTIALIAS:消锯齿
img.show()

13.抠图

from PIL import Image,ImageFilter,ImageDraw,ImageFont

img=Image.open("image/minons.jpeg")

#抠图
img=img.crop(box=(250,300,400,500))
img.show()

14.影像的旋转

from PIL import Image

img=Image.open("image/minons.jpeg")
#影像的旋转
img=img.rotate(30)#逆时针旋转30度
img.show()
img=img.rotate(-30)#顺时针旋转30度
img.show()

15.影像的像素直方图

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

img=Image.open("image/minons.jpeg")
#影像的像素直方图
hist=img.histogram()#直方图的数据统计。像素值出现的频率。
print(hist)

#查看直方图
x=np.array(img)
plt.subplot(2,1,1)
plt.imshow(img)
plt.axis("off")#去掉框框
plt.subplot(2,1,2)
plt.hist(x.flatten(),bins=256)#像素直方图
plt.show()

16.练习:二维码的制作

from PIL import Image,ImageDraw,ImageFont
import random

class GenerateCode():

    def getcode(self):#是个随机数
        return chr(random.randint(65,90))#a-z的英文字母。a-z的ASCII为65-90。chr:将ASCII码转化为字符。

    def backgroundcolor(self):#背景色(背景色与前景色既有不同的地方,又有相同的地方)
        return (random.randint(90,160),random.randint(90,160),random.randint(90,160))

    def foregroundcolor(self):#前景色(背景色与前景色既有不同的地方,又有相同的地方)
        return (random.randint(60,120), random.randint(60,120), random.randint(60,120))

    def genValidateCode(self):
        w,h=240,60
        panel=Image.new(size=(w,h),color=(255,255,255),mode="RGB")#在一张白纸上画.给定范围和颜色(白纸)
        draw=ImageDraw.Draw(panel)#在画板里画。
        font=ImageFont.truetype(font="C:\Windows\Fonts\ALGER.TTF",size=30)#字体
        #循环整个画板,逐像素画,画背景颜色
        for y in range(h):
            for x in range(w):
                draw.point((x, y), fill=self.backgroundcolor())#画点,用背景色填充
        #画数字,画前景颜色
        for i in range(4):
            draw.text((60*i+20,15),text=self.getcode(),fill=self.foregroundcolor(),font=font)
        panel.show()
if __name__ == '__main__':
    d=GenerateCode()
    # print(d.getcode())
    # print(d.backgroundcolor())
    # print(d.foregroundcolor())
    print(d.genValidateCode())
发布了29 篇原创文章 · 获赞 45 · 访问量 5048

猜你喜欢

转载自blog.csdn.net/sinat_39783664/article/details/103541092