【问题标题】:OSError: cannot identify image file <_io.StringIO object at 0x00000000022810D8>OSError: 无法识别图像文件 <_io.StringIO object at 0x00000000022810D8>
【发布时间】:2016-10-24 03:06:48
【问题描述】:

使用win8和python3.4,我需要将文本转换为图像。所以我尝试自己实现一个。但是我遇到了一个OSError。我尝试使用BytesIO而不是StringIO,它会弹出错误“OSError:无法识别图像文件 <_io.bytesio object at xxxx>.

我还是找不到原因。

代码如下:

# -*- coding: utf-8 -*-
import os
import pygame
from io import StringIO,BytesIO
from PIL import Image

pygame.init()
text = u'This is a test text,test 123.'
font_path = "C:/windows/fonts/simsun.ttc"
im = Image.new("RGB",(300,50),(255,255,255))
font = pygame.font.Font(os.path.join(font_path),22)
rtext = font.render(text, True, (0,0,0),(255,255,255))

sio = StringIO()
print(sio.getvalue())
pygame.image.save(rtext, sio)
sio.seek(0)
#print(sio.getvalue())
line = Image.open(sio)
im.paste(line,(10,5))

im.show()
im.save("t1.png")

我得到了这个错误:

Traceback (most recent call last):
  File "D:/mypython/learn/demo.py", line 19, in <module>
    line = Image.open(sio)
  File "D:\Python34\lib\site-packages\PIL\Image.py", line 2319, in open
    % (filename if filename else fp))
OSError: cannot identify image file <_io.StringIO object at 0x00000000022810D8>

【问题讨论】:

  • 您发现这有什么用吗?

标签: python-imaging-library python-3.4 stringio


【解决方案1】:
line = Image.open(sio)

就我而言,sio 仍然是StringIO()。如果您尝试将其作为图像打开,请尝试使用line = Image.open(name) 打开它,其中name 是图像的实际名称,而不是StringIO()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    相关资源
    最近更新 更多