Python学习11-17.1-17.7操作图像
本文为学习python编程时所记录的笔记,仅供学习交流使用。
17.1 颜色和RGBA值
>>> from PIL import ImageColor
>>> ImageColor.getcolor('red','RGBA')
(255, 0, 0, 255)
>>> ImageColor.getcolor('RED','RGBA')
(255, 0, 0, 255)
>>> ImageColor.getcolor('Black','RGBA')
(0, 0, 0, 255)
>>> ImageColor.getcolor('chocolate','RGBA')
(210, 105, 30, 255)
>>> ImageColor.getcolor('CornflowerBlue','RGBA')
(100, 149, 237, 255)
>>> from PIL import Image
>>> catIm=Image.open('2251.png')
>>> catIm.size
(92, 92)
>>> width,height=catIm.size
>>> width
92
>>> height
92
>>> catIm.filename
'2251.png'
>>> catIm.format
'PNG'
>>> catIm.format_description
'Portable network graphics'
>>> catIm.save('zophie.jpg')
Traceback (most recent call last):
File "C:\Users\VECTOR\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\JpegImagePlugin.py", line 615, in _save
rawmode = RAWMODE[im.mode]
KeyError: 'P'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
catIm.save('zophie.jpg')
File "C:\Users\VECTOR\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2102, in save
save_handler(self, fp, filename)
File "C:\Users\VECTOR\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\JpegImagePlugin.py", line 617, in _save
raise OSError("cannot write mode %s as JPEG" % im.mode)
OSError: cannot write mode P as JPEG
>>> catIm.mode
'P'
>>> catIm=catIm.convert('RGB')
Warning (from warnings module):
File "C:\Users\VECTOR\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 932
"Palette images with Transparency expressed in bytes should be "
UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images
>>> catIm=catIm.convert('RGBA')
>>> catIm.save('zophie.jpg')
Traceback (most recent call last):
File "C:\Users\VECTOR\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\JpegImagePlugin.py", line 615