图像的参数.pil中的fromstring函数

我需要做的是在内存中修改图像,例如调整内存中的图像等...没有保存磁盘,因为我必须在保存磁盘上返回图像,然后将图像返回
PIL支持使用弦乐对象代替文件
对象. Stringio对象是可变长度的二进制字符串
在内存中,我将图像保存在Stringio对象中
导入图像
导入Stringio
im = image.open("/home/home/bharath/desktop/photos/lena.gif")
fil = stringio.stringio()
IM.Save(FIL," GIF")
cached_image = fil.getValue()
或用于JPEG图像
导入图像
导入Stringio
im = image.open("/home/home/bharath/desktop/photos/lena.jpg")
fil = stringio.stringio()
IM.Save(FIL," JPEG")
cached_image = fil.getValue()
但是我不能在cached_image上应用调整大小操作,因为它是str对象
但是我发现了image.formstring(模式,大小,数据,解码器,参数)从字符串中的像素数据创建图像存储器,因此我为GIF图像做了以下操作
ima = image.fromstring('p',(128,128),cached_image,'g if')
imr = ima.resize((64,64),image.antialias)
imr.save("/home/bharath/desktop/photos/resize_lena3.gif"," gif")
我收到以下错误
Trackback(最近的最新电话):
文件" ",第1行,<模块>
文件" /home/bharath/desktop/image.py",第8行,
ima = image.fromstring('p',(128,128),cached_image,'g if')
文件"/usr/lib/python2.5/site-packages/pil/image.py",第1744行
im.fromstring(数据,decoder_name,args)
文件"/usr/lib/python2.5/site-packages/pil/image.py",第577行
提高ValueError("无法解码图像数据")
ValueError:无法解码JPEG图像的图像数据
ma = image.fromstring('p',(128,128),cached_image,'jp eg')
imr = ima.resize((64,64),image.antialias)
imr.save("/home/bharath/desktop/photos/resize_lena3.gif"," jpeg")
我收到以下错误...
Trackback(最近的最新电话):
文件" ",第1行,<模块>
文件" /home/bharath/desktop/image.py",第8行,
ima = image.fromstring('rgb',(128,128),cached_image,'jpeg')
文件"/usr/lib/python2.5/site-packages/pil/image.py",第1744行
im.fromstring(数据,decoder_name,args)
文件"/usr/lib/python2.5/site-packages/pil/image.py",第570行
d = _getDecoder(self.mode,desoder_name,args)
文件"/usr/lib/python2.5/site-packages/pil/image.py",第373行,in _getDecoder
返回应用(解码器,(模式) + args + extra)
TypeError:功能至少需要3个参数(1个给定)
但是,如果我删除dododer_name的第四个参数,即函数image.formstring(模式,大小,数据,解码器,参数)中的第四个参数,即'gif'或'jpeg' )..图像甚至可以调整大小并保存大小,但是图像是扭曲的图像,表明它不是以正确的方式解码.通过 函数image.fromstring(模式,大小,数据,解码器,参数)中的额外参数以及要正确解码的解码器名称. 请您告诉我必须通过一个示例传递的额外参数(以我的示例本身) 回复 向前

# 回答1

我不确定从弦模块中传递什么. 但是您可以做类似的事情来从字符串缓冲区保存JPEG图像: newImage = image.fromstring('l',(325,256),cached_image_string) newimage.save(文件名," jpeg")

标签: python

添加新评论