I've opened an image in PIL like so:
from PIL import Image
i = Image.open("image.jpg")
I need to access the raw contents of this file. How can I get the entire picture data, as if I would have done open(...).read()
?
In the above code, we save the im_resize Image object into BytesIO object buf . Note that in this case, you have to specify the saving image format because PIL does not know the image format in this case. The bytes string can be retrieved using getvalue() method of buf variable.
If you have an entire image in a string, wrap it in a BytesIO object, and use open() to load it.
The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images. Image. convert() Returns a converted copy of this image.
you can see this answer python Image PIL to binary Hex
The img object needs to be saved again; write it to another BytesIO object:
output = io.BytesIO() img.save(output, format='JPEG')
then get the written data with the .getvalue() method:
hex_data = output.getvalue()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With