python3: PIL.Image模块的常用函数及属性

最近写了一个小工具,用python3在手机上截长图,所以对PIL.Image模块做了一些了解学习。

https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.crop

对在这次学习过程中常用到的一些函数和属性做了一个思维导图。

python3: PIL.Image模块的常用函数及属性
PIL.Image 常用函数及属性

 

实例:

In [7]: path = input(':')
:C:\Users\loginname\Desktop\test.jpg

In [8]: img = Image.open(path)

In [9]: img.show()

In [10]: img.size
Out[10]: (861, 480)

In [11]: img.width
Out[11]: 861

In [12]: img.height
Out[12]: 480

In [13]: img.format
Out[13]: 'JPEG'

In [14]: img.info
Out[14]:
{'jfif': 257,
 'jfif_version': (1, 1),
 'dpi': (300, 300),
 'jfif_unit': 1,
 'jfif_density': (300, 300)}

In [15]: box = (0, 0, 400, 400)

In [16]: newimg = img.crop(box)

In [17]: newimg.show()

In [18]: newimg.getpixel((200, 200))
Out[18]: (9, 29, 28)

In [19]: newimg.getbbox()
Out[19]: (0, 0, 400, 400)

In [21]: new = Image.new('RGB', (400, 400))

In [22]: new.paste(newimg, (0, 0))

In [23]: new.save('newImage.jpg')