Python学习11-17.1-17.7操作图像

这篇博客介绍了Python中处理图像的各种技巧,包括使用RGBA值操作颜色,裁剪图像,复制粘贴图像,调整图像大小,旋转和翻转图像。此外,还详细讲解了如何在图像上进行绘画,绘制形状和文本,以及一个添加徽标的项目实践。
摘要由CSDN通过智能技术生成


本文为学习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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值