将PILLOW图像转换为StringIO [英] Convert PILLOW image into StringIO

查看:95
本文介绍了将PILLOW图像转换为StringIO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序可以接收多种常见图像格式的图像,但需要以一种一致的格式检查所有图像.真正的图像格式并不重要,主要是所有图像都相同.由于我需要转换图像格式,然后继续使用图像,因此我不想将其保存到磁盘.只需将其转换并继续即可.这是我使用StringIO的尝试:

I'm writing a program which can receive images in a variety of common image formats but needs to examine them all in one consistent format. It doesn't really matter what image format, mainly just that all of them are the same. Since I need to convert the image format and then continue working with the image, I don't want to save it to disk; just convert it and continue on. Here's my attempt using 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获得相同的结果.关于如何解决这个问题有什么建议吗?

I've also tried with io.BytesIO with the same results. Any suggestions as to how to approach this?

推荐答案

根据实例的创建方式,cStringIO.StringIO()对象有两种类型.一个用于阅读,另一个用于写作.您不能互换这些.

There are two types of cStringIO.StringIO() objects depending on how the instance was created; one for just reading, the other for writing. You cannot interchange these.

当创建一个 empty cStringIO.StringIO()对象时,您实际上会得到一个cStringIO.StringO(请注意最后的O)类,它只能用作 output ,即写信.

When you create an empty cStringIO.StringIO() object, you really get a cStringIO.StringO (note the O at the end) class, it can only act as output, i.e. write to.

相反,创建一个具有初始内容的对象会生成一个cStringIO.StringI对象(以I结尾作为输入),您永远无法对其进行写入,只能对其进行读取.

Conversely, creating one with initial content produces a cStringIO.StringI object (ending in I for input), you can never write to it, only read from it.

这对于 just cStringIO模块而言尤其如此; StringIO(纯python模块)没有此限制. 文档使用别名

This is particular to just the cStringIO module; the StringIO (pure python module) does not have this limitation. The documentation uses the aliases cStringIO.InputType and cStringIO.OutputType for these, and has this to say:

StringIO模块的另一个不同之处是,使用字符串参数调用StringIO()会创建一个只读对象.与没有字符串参数创建的对象不同,它没有写方法.这些对象通常是不可见的.它们以StringIStringO的形式出现在回溯中.

Another difference from the StringIO module is that calling StringIO() with a string parameter creates a read-only object. Unlike an object created without a string parameter, it does not have write methods. These objects are not generally visible. They turn up in tracebacks as StringI and StringO.

使用cStringIO.StringO.getvalue()从输出文件中获取数据:

Use cStringIO.StringO.getvalue() to get the data out of an output file:

# replace cStringIO.StringO (output) with cStringIO.StringI (input)
cimage = cStringIO.StringIO(cimage.getvalue())
cimage = Image.open(cimage)

可以使用 io.BytesIO() ,但是随后您需要在写完后倒回:

You can use io.BytesIO() instead, but then you need to rewind after writing:

image = Image.open(io.BytesIO(raw_image)).convert("RGB")
cimage = io.BytesIO()
image.save(cimage, format="BMP")
cimage.seek(0)  # rewind to the start
cimage = Image.open(cimage)

这篇关于将PILLOW图像转换为StringIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆