Open Pil Image from Byte File

Open PIL image from byte file

You can try this:

image = Image.frombytes('RGBA', (128,128), image_data, 'raw')
Source Code:

def frombytes(mode, size, data, decoder_name="raw", *args):
param mode: The image mode.
param size: The image size.
param data: A byte buffer containing raw data for the given mode.
param decoder_name: What decoder to use.

Python PIL bytes to Image

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)

How to get image from bytes in Python?

Use the PIL module. More information in answers here: Decode image bytes data stream to JPEG

from PIL import Image
from io import BytesIO

with open('img.jpg', 'rb') as f:
data = f.read()

# Load image from BytesIO
im = Image.open(BytesIO(data))

# Display image
im.show()

# Save the image to 'result.FORMAT', using the image format
im.save('result.{im_format}'.format(im_format=im.format))

Python PIL.Image.tobytes() gives invalid bytes for image

Answered by Jason Yang in comments -

Method Image.tobytes returns the raw image data from the internal storage. For compressed image data (e.g. PNG, JPEG) use save(), with a BytesIO parameter for in-memory data.

PIL: Convert Bytearray to Image

If you manipulate with bytearrays, then you have to use io.BytesIO. Also you can read a file directly to a bytearray.

import os
import io
import PIL.Image as Image

from array import array

def readimage(path):
count = os.stat(path).st_size / 2
with open(path, "rb") as f:
return bytearray(f.read())

bytes = readimage(path+extension)
image = Image.open(io.BytesIO(bytes))
image.save(savepath)

Image from bytes (python)

That's kind of a late response but maybe it helps others in the future: Hopefully I interpreted your question right, but if your "arbitrary text file" represents the structure of an image file like ".jpg" you can just change the files extension from ".txt" to ".jpg" and import it with PIL for example.

You can do something like this:

from PIL import Image

path_to_file = 'path/to/arbitraty_textfile.txt'
safe_path = path_to_file.replace('.txt','.jpg')

with open(path_to_file,'rb') as textfile:
bytestring = textfile.read()

with open(safe_path, 'wb') as imagefile:
imagefile.write(bytestring)

#Import with PIL
image = Image.open(safe_path)

# ...

If you want to read or write a string of bytes in python the attribute 'rb' or 'wb' is the key word here.

Let me know if this is close to the solution, that you've already found I guess.

Convert PIL Image to byte array?

Thanks everyone for your help.

Finally got it resolved!!

import io

img = Image.open(fh, mode='r')
roi_img = img.crop(box)

img_byte_arr = io.BytesIO()
roi_img.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()

With this i don't have to save the cropped image in my hard disc and I'm able to retrieve the byte array from a PIL cropped image.



Related Topics



Leave a reply



Submit