将jpeg字符串转换为PIL图像对象 [英] Converting jpeg string to PIL image object
问题描述
我从应用程序的后端收到了应该是jpeg文件的文件列表.但是对于我一生,我一直无法将它们转换为PIL图像对象.当我打电话
I've been handed a list of files from the backend of an application that are supposed to be jpeg files. However for the life of me I haven't been able to convert them into PIL image objects. When I call
str(curimg)
我回来了:
<type 'str'>
.我试过使用open()、. read,io.BytesIO(img.read(),但也没有做任何事情,但始终将其视为字符串.当我打印字符串时,我得到了无法识别的字符.有人知道吗如何告诉python如何将该字符串解释为jpeg并将其转换为药丸图像,我可以在其中调用.size和np.array?
. I have tried using open(), .read, io.BytesIO(img.read() and also doing nothing to it, but it keeps seeing it as a string. When i print the string, I get unrecognizable characters. Does anyone know how to tell python how to intepret this string as a jpeg and convert it into a pill image where I can call .size and np.array on?
推荐答案
您应该能够将StringIO对象传递给PIL并以这种方式打开它.
You should be able to pass a StringIO object to PIL and open it that way.
即:
from PIL import Image
import StringIO
tempBuff = StringIO.StringIO()
tempBuff.write(curimg)
tempBuff.seek(0) #need to jump back to the beginning before handing it off to PIL
Image.open(tempBuff)
这篇关于将jpeg字符串转换为PIL图像对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!