【Python】pillow库(图像处理)

pillow

官方文档:https://pillow.readthedocs.io/en/latest/handbook/tutorial.html

读写图像

—获取图像的基本信息

from PIL import Image
image=Image.open('1.jpg')#读取图像
#图像的基本信息
print(image.info)        #图像的二进制数据
print(image.size)        #图像的尺寸
print(image.format)      #图像的格式
print(image.mode)        #图像的格式
print(dir(image))        #查看Image实例对象的属性

Image对象实例的属性
im.format:标识了图像来源。如果图像不是从文件读取它的值就是None
im.size:是一个二元tuple,包含width和height(宽度和高度,单位都是px) im.mode:常见的modes 有 “L” (luminance) 表示灰度图像, “RGB” 表示真彩色图像, “CMYK” 表示出版图像

—将图片转换为JPEG格式

from PIL import Image
Image.open('1.jpg').save('2.jpg')

—创建JPEG略缩图

from PIL import Image
im_size=(80,80)
im=Image.open('1.jpg')
print(im.format,im.size,im.mode)
im.thumbnail(im_size)
im.save('newpic.JPEG','JPEG')#保存图片

im.save(file_name,pic_format)的参数
file_name:[string]
pic_format:[string] 可选参数"JPG",“JPEG”,“PNG”

发布了82 篇原创文章 · 获赞 468 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/qq_44647926/article/details/101384312