Python的PIL字节到图像 [英] Python PIL bytes to Image
问题描述
进口PIL
从PIL进口图片
从PIL进口ImageDraw
从PIL进口ImageFont
进口urllib.request里与urllib.request.urlopen('http://pastebin.ca/raw/2311595'),为in_file中:
hex_data = in_file.read()
打印(hex_data)
IMG = Image.frombuffer(RGB,(320240),hex_data)#我已经试过fromstring
画= ImageDraw.Draw(IMG)
字体= ImageFont.truetype(ARIAL.TTF,14)
draw.text((0,220),这是一个TEST11,(255,255,0),字体=字体)
画= ImageDraw.Draw(IMG)
img.save(a_test.jpg)
我米试图二进制格式转换为图像,然后绘制text.but我得到的错误有:
IMG = Image.frombuffer(RGB,(320240),hex_data)
提高ValueError错误(没有足够的图像数据)
ValueError错误:没有足够的图像数据
我已经上传字节的字符串在这里 http://pastebin.ca/raw/2311595 结果
和图片大小,我可以肯定为320x240
其他
这是我可以肯定的画面字节字符串从320×240,只需运行code将从字节字符串创建一个图片
进口urllib.request里
进口binascii
进口结构#下载数据。
与urllib.request.urlopen('http://pastebin.ca/raw/2311595'),为in_file中:
hex_data = in_file.read()
#Unhexlify的数据。
bin_data = binascii.unhexlify(hex_data)
打印(bin_data)
#删除嵌入的长度。
jpeg_data = bin_data
#写出JPEG。
开放('out.jpg','WB)为out_file:
out_file.write(jpeg_data)
解决,这是code更新时间:
从PIL进口图片
从PIL进口ImageDraw
从PIL进口ImageFont
进口urllib.request里
进口IO
进口binascii数据= urllib.request.urlopen('http://pastebin.ca/raw/2311595').read()
r_data = binascii.unhexlify(数据)
#r_data =.unhexlify(CHR(中间体(b_data:在范围(0 [I I + 2],16))对于i,LEN(b_data),2))流= io.BytesIO(r_data)IMG = Image.open(流)
画= ImageDraw.Draw(IMG)
字体= ImageFont.truetype(ARIAL.TTF,14)
draw.text((0,220),这是一个TEST11,(255,255,0),字体=字体)
画= ImageDraw.Draw(IMG)
img.save(a_test.png)
这形象不是原始字节组成 - 而这是一间codeD JPEG文件。
此外,你是不是解析ASCII HEX重新流进适当的字节presentation:
即,在该文件中的FF序列被传递到PIL作为两片c字母F,而不是用数字255字节
所以,首先,你去code中的字符串到合适的字节序列 - 遗憾的卷积 - 它很可能有一个更好的方式来做到这一点,但我是一个缓慢的一天:
数据=了urllib.urlopen(http://pastebin.ca/raw/2311595).read()
r_data =。加入(CHR(中间体(数据:在范围(0 [I I + 2],16))对于i,LEN(数据),2))
嗯,C.Y.张贴在兴田评论 - 这种方式:
>>>进口binascii
>>> b_data = binascii.unhexlify(数据)
而现在,你把它导入到PIL为JPEG图像:
>>>从PIL进口图片
>>> cStringIO导入StringIO的作为
>>>流= StringIO.StringIO(b_data)
>>> IMG = Image.open(流)
>>> img.size
(320,240)
import PIL
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import urllib.request
with urllib.request.urlopen('http://pastebin.ca/raw/2311595') as in_file:
hex_data = in_file.read()
print(hex_data)
img = Image.frombuffer('RGB', (320,240), hex_data) #i have tried fromstring
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("arial.ttf",14)
draw.text((0, 220),"This is a test11",(255,255,0),font=font)
draw = ImageDraw.Draw(img)
img.save("a_test.jpg")
i m trying to convert binary to image,and then draw the text.but i get the error with:
img = Image.frombuffer('RGB', (320,240), hex_data)
raise ValueError("not enough image data")
ValueError: not enough image data
i have uploaded the bytes string at here http://pastebin.ca/raw/2311595
and the picture size i can sure that is 320x240
ADDITIONAL
here is what i can sure the picture bytes string are from 320x240,just run the code will create a image from the bytes string
import urllib.request
import binascii
import struct
# Download the data.
with urllib.request.urlopen('http://pastebin.ca/raw/2311595') as in_file:
hex_data = in_file.read()
# Unhexlify the data.
bin_data = binascii.unhexlify(hex_data)
print(bin_data)
# Remove the embedded lengths.
jpeg_data = bin_data
# Write out the JPEG.
with open('out.jpg', 'wb') as out_file:
out_file.write(jpeg_data)
SOLVED, THIS IS THE CODE UPDATED
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import urllib.request
import io
import binascii
data = urllib.request.urlopen('http://pastebin.ca/raw/2311595').read()
r_data = binascii.unhexlify(data)
#r_data = "".unhexlify(chr(int(b_data[i:i+2],16)) for i in range(0, len(b_data),2))
stream = io.BytesIO(r_data)
img = Image.open(stream)
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("arial.ttf",14)
draw.text((0, 220),"This is a test11",(255,255,0),font=font)
draw = ImageDraw.Draw(img)
img.save("a_test.png")
That image is not formed of raw bytes - rather it is an encoded JPEG file. Moreover, you are not parsing the ascii HEX representation of the stream into proper bytes: that is, an "ff" sequence in that file is being passed to PIL as two c letters "f" instead of a byte with the number 255.
So, first, you decode the string to a proper byte sequence - sorry for the convolution - it is likely there is a better way to do that, but I am on a slow day:
data = urllib.urlopen("http://pastebin.ca/raw/2311595").read()
r_data = "".join(chr(int(data[i:i+2],16)) for i in range(0, len(data),2))
Ah, C.Y. posted on hte comment - this way:
>>> import binascii
>>> b_data = binascii.unhexlify(data)
And now, you import it to PIL as a JPEG image:
>>> from PIL import Image
>>> import cStringIO as StringIO
>>> stream = StringIO.StringIO(b_data)
>>> img = Image.open(stream)
>>> img.size
(320, 240)
这篇关于Python的PIL字节到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!