Convert jpeg string to PIL image object
I was handed a list of files from the backend of the application that should be jpeg files. However, for my life, I have not been able to convert them to PIL image objects. When i call
str(curimg)
I'm coming back:
<type 'str'>
... I tried to use open (),. Read, io.BytesIO (img.read () and also do nothing with it, but it keeps seeing it as a string. When I print a string, I get unrecognizable characters. How to say python, how to intepret this string as jpeg and convert it to thumbnail where can i call .size and np.array?
+3
source to share
2 answers
The same, but a little simpler
from PIL import Image
import io
Image.open(io.BytesIO(image))
Note:
If the image is on the Internet; you need to download it first.
import requests
image = requests.get(image_url).content #download image from web
And then pass it to the io module.
io.BytesIO(image)
If the image is in your hd; you can open directly from PIL.
Image.open('image_file.jpg') #image in your HD
+1
source to share