Python-使用PIL和Image处理完图片存储时出现的问题
2020-03-08 16:08
627 查看
使用如下代码
from PIL import Image import numpy as np im0 = np.array(Image.open("E:/1/1.jpg").convert("L"))#灰度方式打开图片,并且转换为数组 im1 = 255 - im0#反变换 pil_im = Image.fromarray(np.uint(im1)) pil_im.save("E:/1/im1.jpg")#存储
会出现问题
OSError: cannot write mode I as JPEG
from PIL import Image import numpy as np im0 = np.array(Image.open("E:/1/1.jpg").convert("L")) im1 = 255-im0 pil_im = Image.fromarray(np.uint(im1)) pil_im = pil_im.convert("L")#添加此行代码即可解决 pil_im.save("E:/1/im1.jpg")
《Python语言程序设计基础(第二版)》嵩天
后续:循环保存
from PIL import Image import numpy as np im0 = np.array(Image.open("E:/1/1.jpg").convert("L")) im1 = 255 - im0 im2 = (100/255)*im0+150 im3 = 255*(im1/255)**2 for s in range(1,4): file = Image.fromarray(np.uint(eval("im{}".format(s))))#np.uint()操作对象是一个变量而不是字符串,所以要用eval()变为可执行语句 file = file.convert("L") file.save("E:/1/im{}.jpg".format(s))
初学python时常犯的小错误合集 -数据类型错误是最常见的
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Python 之 使用 PIL 库做图像处理(pillow+ImageDraw)
- Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed
- 使用ImageMagick+Jmagick处理图片问题
- 使用ImageIO.write存储png格式图片性能较差问题
- python使用Image处理图片常用技巧分析
- 在Python中使用PIL模块对图片进行高斯模糊处理的教程
- Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed
- python使用Image处理图片常用技巧分析
- python处理图片之PIL模块简单使用方法
- Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed
- 在Python中使用PIL模块对图片进行高斯模糊处理的教程
- python处理图片之PIL模块简单使用
- Android第三方框架universal-image-loader[图片缓存加载]及其OOM问题处理【常用】,CardView使用[给列表中项+边框]
- qt中使用opencv处理图片 QImage 和 IplImage 相互之间转换问题
- python使用PIL处理图片的一些收获和经验
- [Android初级]关于使用universal-image-loader加载imagelist出现图片闪烁的问题
- python处理图片之PIL模块简单使用方法
- 使用Python PIL模块处理图片
- qt中使用opencv处理图片 QImage 和 IplImage 相互之间转换问题
- 使用universalimageloader图片加载框架,正确加载却无法显示图片问题的处理