一、下载及安装

PIllow模块需要安装,然后才可以使用。
下载地址: https://pypi.org/project/Pillow/
image.png
选择合适的版本进行下载
image.png
whl格式本质上是一个压缩包,里面包含了py文件,以及经过编译的pyd文件。使得可以在不具备编译环境的情况下,选择合适自己的python环境进行安装。

在安装过程中,报下面的错
image.png
原因是下载的pyhook版本与python版本不一致
image.png
需要找到合适的版本进行下载
image.png
然后进行安装
image.png

二、加载图像文件

首先需要打开文件,使用open()函数
open(infile [,mode])
infile:打开图像文件的路径
mode:打开文件的模式

  1. from PIL import Image
  2. path = "E:\\pythonstduy\\image\\"
  3. img = Image.open(path+"1.4.jpg")
  4. ##通过实例属性查看文件内容
  5. print(img.format,img.size,img.mode)
  6. ##显示图像
  7. img.show()
  8. ##结果
  9. JPEG (3120, 4160) RGB
  1. from tkinter import *
  2. from PIL import Image, ImageTk
  3. ##创建主窗口
  4. win = Tk()
  5. win.title(string = '加载图像')
  6. path = "E:\\pythonstduy\\image\\"
  7. imgFile1 = Image.open(path+"1.1.png")
  8. imgFile2 = Image.open(path+"1.2.gif")
  9. img1 = ImageTk.PhotoImage(imgFile1)
  10. img2 = ImageTk.PhotoImage(imgFile2)
  11. canvas = Canvas(win,background='pink',width=600,height=1000)
  12. canvas.create_image(40,40,image=img1,anchor=NW)
  13. canvas.create_image(600,40,image=img2,anchor=NW)
  14. canvas.pack(fill=BOTH)
  15. ##开始窗口的事件循环
  16. win.mainloop()

image.png

三、图像文件的属性

使用Image模块的open()函数打开的图像文件属性

属性 说明
format 图像文件的格式,eg:JPEG、GIF、BMP、TIFF等
mode 图像文件的色彩表示模式,eg:RGB、P等
size 图像文件的大小,以像素为单位。
返回值一个含两个元素的元组,格式为(width,height)
palette 图像文件的颜色对照表(color palette table)
info 图像文件的辞典集,返回值是一个列表

图像的色彩表示模式

模式 说明
1 1位的像素,黑和白,存储位8位的像素
L 8位的像素,黑和白
P 8位的像素,使用颜色对照表(color palette table)
RGB 3×8位的像素,真实颜色
RGBA 4×8位的像素,真实颜色加上屏蔽
CMYK 4×8位的像素,颜色分离
YCbCr 3×8位的像素,颜色图像格式
I 32位的整数像素
F 32位的浮点数像素
  1. from tkinter import *
  2. import tkinter.filedialog
  3. from PIL import Image
  4. ##创建主窗口
  5. win = Tk()
  6. win.title(string = "图像文件的属性")
  7. ##打开一个[打开旧文件]对话框
  8. def createOpenFileDialog():
  9. ##返回打开的文件夹
  10. filename = myDialog.show()
  11. ##打开该文件
  12. imgFile = Image.open(filename)
  13. ##输入该文件的属性
  14. label1.config(text="format ="+ imgFile.format) ##图像文件的格式
  15. label2.config(text="mode = "+ imgFile.mode) ##图像文件的色彩表示模式
  16. label3.config(text="size = "+ str(imgFile.size)) ##图像文件的大小,返回值是一个元素,需要转换成字符串
  17. label4.config(text="info = "+ str(imgFile.info)) ##图像文件的辞典集,返回值是一个列表,需要转换成字符串
  18. ##创建Label 控件,用于输入图像
  19. label1 = Label(win , text = "format = ")
  20. label2 = Label(win , text = "mode = ")
  21. label3 = Label(win , text = "size = ")
  22. label4 = Label(win , text = "info = ")
  23. ##靠左边对齐
  24. label1.pack(anchor = W)
  25. label2.pack(anchor = W)
  26. label3.pack(anchor = W)
  27. label4.pack(anchor = W)
  28. ##单击按钮后,即打开对话框
  29. Button(win,text = "打开图像文件",command = createOpenFileDialog).pack(anchor = CENTER)
  30. ##设置对话框打开的文件类型
  31. myFileTypes = [('Graphics Interchange Format ',"*.gif"),('Windows bitmap','*bmp'),('JPEG format','*.jpg'),('Tag Iagme File Format',"*.tif"),('ALL image files','*.gif *.jpg *.bmp *.tif ')]
  32. ##创建一个[打开旧文件]对话框
  33. myDialog = tkinter.filedialog.Open(win,filetypes = myFileTypes)
  34. ##开始程序循环
  35. win.mainloop()

image.png
image.png

四、复制和粘贴图像

使用Image模块的copy()方法复制该图像
使用Image模块的paste()方法粘贴该图像
使用Image模块的crop()方法剪下图像中的矩形方块
语法格式:
copy()
paste(image,box)
crop(box)
paste方法中box是一个含有两个元素的元组,((left,top),(right,bottom))
crop方法中box 是图像中的一个矩形方块,是一个含有4个元素的元组:(left,top,right,bottom),表示矩形左上角和右下角的图标

  1. from tkinter import *
  2. import tkinter.filedialog
  3. from PIL import Image, ImageTk
  4. ##创建主窗口
  5. win = Tk()
  6. win.title (string = '复制和粘贴图像')
  7. ##打开图像文件
  8. path = "E:\\pythonstduy\\image\\"
  9. imgFile = Image.open(path +"1.4.jpg")
  10. ##创建第一个图像实例变量
  11. img1 = ImageTk.PhotoImage(imgFile)
  12. ##读取图像文件的宽与高
  13. width,height = imgFile.size
  14. ##设置剪下的区块范围
  15. box1 = (0,0,width,int(height/2))
  16. ##将图像的上半部分剪下
  17. part = imgFile.crop(box1)
  18. ##将剪下的部分旋转180度
  19. part = part.transpose(Image.ROTATE_180)
  20. ##将图像的上半部分粘贴到上半部分
  21. imgFile.paste(part,box1)
  22. ##创建第一个图像实例变量
  23. img2 =ImageTk.PhotoImage(imgFile)
  24. ##创建Label控件,以显示图像
  25. label1 = Label(win,width=500,height=1000,image=img1)
  26. label2 = Label(win,width=500,height=1000,image=img2)
  27. label1.pack(side=LEFT,padx=10)
  28. label2.pack(side=LEFT,padx=10)
  29. ##开始窗口的事件循环
  30. win.mainloop()

image.png

五、图像的几何变化

操作 函数及语法格式
改变图像的大小 resize((width , height))
ps:里面的(width,height) 是一个元组
旋转图像 rotate(angle)
颠倒图像 transpose(method)
method可以是FILP_LEFT_RIGHT(左右翻转)、FILP_TOP_BOTTOM(上下翻转)、ROTATE_90、ROTATE_180、ROTATE_270
  1. from tkinter import *
  2. from PIL import Image,ImageTk
  3. ##创建主窗口
  4. win = Tk()
  5. win.title (string = "图像的几何变换")
  6. ##打开图像文件
  7. path = "E:\\pythonstduy\\image\\"
  8. imgFile1 = Image.open(path+"1.5.jpg")
  9. ##创建第一个图像实例变量
  10. img1 = ImageTk.PhotoImage(imgFile1)
  11. ##创建Label控件,用于显示原始图像
  12. label1 = Label(win,width=300,height=300,image=img1)
  13. label1.pack(side = LEFT,padx = 10)
  14. ##旋转图像成45度
  15. imgFile2 = imgFile1.rotate(45)
  16. img2 = ImageTk.PhotoImage(imgFile2)
  17. ##创建Label控件,用于显示旋转后的图像
  18. label2 = Label(win,width=300,height=300,image=img2)
  19. label2.pack(side=LEFT,padx=10)
  20. ##图像上下翻转
  21. imgFile3 = imgFile1.transpose(Image.FLIP_TOP_BOTTOM)
  22. img3 = ImageTk.PhotoImage(imgFile3)
  23. ##创建Label控件,用于显示翻转后的图像
  24. label3 = Label(win,width=300,height=300,image=img3)
  25. label3.pack(side = LEFT,padx = 10)
  26. ##将图像改变1/4倍
  27. width,height = imgFile1.size
  28. imgFile4 = imgFile1.resize((int(width/2),int(height/2)))
  29. img4 = ImageTk.PhotoImage(imgFile4)
  30. ##创建Label控件,用于显示缩小后的图像
  31. label4 = Label(win,width=300,height=300,image=img4)
  32. label4.pack(side = LEFT,padx = 10)
  33. ##开始窗口的事件循环
  34. win.mainloop()

image.png

六、存储图像

使用save() 方法存储文件
语法格式:
save(outfile [,options])
options位文件格式的名称

  1. from PIL import Image
  2. ##打开图像文件
  3. path = "E:\\pythonstduy\\image\\"
  4. imgFile = Image.open(path+"1.5.jpg")
  5. imgFile.save("E:\\pythonstduy\\image\\1.6.png","png")

image.png