Python PIL Image.draft()方法

Python PIL Image.draft()方法

PIL是Python图像库,它为Python解释器提供了图像编辑功能。图像模块提供了一个同名的类,用来表示一个PIL图像。该模块还提供了一些工厂函数,包括从文件加载图像和创建新图像的函数。
Image.draft() 配置图像文件加载器,使其返回尽可能符合给定模式和尺寸的图像版本。例如,你可以用这个方法在加载一个彩色JPEG文件时将其转换成灰度,或者从一个PCD文件中提取一个128×192版本。

语法: Image.draft(mode, size)
参数:
mode – 要求的模式。
size – 要求的尺寸。
返回:一个图像对象。
返回的类型:图像

使用的图片:

Python PIL Image.draft()方法

# importing image object from PIL
from PIL import Image
  
# creating an image object
im = Image.open(r"C:\Users\System-Pc\Desktop\rose.jpg")
 
# print the original image object
print(im)
 
# using draft function
# convert mode and size as well
im1 = im.draft("L", (im.width // 2, im.height // 2))
im2 = im1.decoderconfig, im1.mode, im.size, im1.tile
print(im1)
print(im2)
 
# show the converted image
im1.show()

输出1:

PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=217x232 at 0x27A3D65FD68
PIL.JpegImagePlugin.JpegImageFile image mode=L size=109x116 at 0x27A3D65FD68
((2, 0), 'L', (109, 116), [('jpeg', (0, 0, 109, 116), 0, ('L', ''))])

输出2:

Python PIL Image.draft()方法

另一个例子:这里我们使用另一个图像。

使用的图片:

Python PIL Image.draft()方法

# importing image object from PIL
from PIL import Image
  
# creating an image object
im = Image.open(r"C:\Users\System-Pc\Desktop\tree.jpg")
 
# print the original image object
print(im)
 
# using draft function
# convert mode and size as well
im1 = im.draft("L", (im.width // 2, im.height // 2))
im2 = im1.decoderconfig, im1.mode, im.size, im1.tile
print(im1)
print(im2)
 
# show the converted image
im1.show()

输出1:

PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=259x194 at 0x28A1C2C1CC0
PIL.JpegImagePlugin.JpegImageFile image mode=L size=130x97 at 0x28A1C2C1CC0
((2, 0), 'L', (130, 97), [('jpeg', (0, 0, 130, 97), 0, ('L', ''))])

输出2:

Python PIL Image.draft()方法

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Python pil