【发布时间】:2014-09-15 05:48:41
【问题描述】:
我正在编写一个程序,它可以接收各种常见图像格式的图像,但需要以一种一致的格式检查它们。什么图像格式并不重要,主要只是它们都是相同的。由于我需要转换图像格式然后继续处理图像,我不想将其保存到磁盘;只需转换它并继续。这是我使用 StringIO 的尝试:
image = Image.open(cStringIO.StringIO(raw_image)).convert("RGB")
cimage = cStringIO.StringIO() # create a StringIO buffer to receive the converted image
image.save(cimage, format="BMP") # reformat the image into the cimage buffer
cimage = Image.open(cimage)
它返回以下错误:
Traceback (most recent call last):
File "server.py", line 77, in <module>
s.listen_forever()
File "server.py", line 47, in listen_forever
asdf = self.matcher.get_asdf(data)
File "/Users/jedestep/dev/hitch-py/hitchhiker/matcher.py", line 26, in get_asdf
cimage = Image.open(cimage)
File "/Library/Python/2.7/site-packages/PIL/Image.py", line 2256, in open
% (filename if filename else fp))
IOError: cannot identify image file <cStringIO.StringO object at 0x10261d810>
我也尝试使用 io.BytesIO 获得相同的结果。有关如何处理此问题的任何建议?
【问题讨论】:
标签: python python-imaging-library python-2.x stringio