我正在编写一个程序,可以接收多种常见的图像格式,但需要以一种统一的格式检查它们。实际上,图像格式并不重要,主要是所有图像都是相同的格式。由于我需要转换图像格式,然后继续处理图像,因此我不想将其保存到磁盘上,只需转换并继续进行。以下是使用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,但结果相同。有什么建议如何处理这个问题吗?