python Image PIL to binary Hex

12,790

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()

The PIL-for-python-3 landscape is rather muddled at the moment. The Pillow fork looks to be the best, maintained version out there at the moment. It includes fixes that make saving to a BytesIO object work. If you run into a io.UnsupportedOperation: fileno exception using the above code, you have a version that was not yet fixed, in which case you'll have to resort to using a temporary file instead.

Share:
12,790
Admin
Author by

Admin

Updated on June 05, 2022

Comments

  • Admin
    Admin almost 2 years
    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)
    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",(0,255,0),font=font)
    draw = ImageDraw.Draw(img)
    
    with open(img,'rb') as in_file: #error on here invalid file:
         hex_data = in_file.read()
    # Unhexlify the data.
    bin_data = binascii.unhexlify(bytes(hex_data))
    print(bin_data)
    

    Question

    converting hex to image and draw a text on the image, after that convert image to binary hex,but having the problem at here with open(img,'rb') as in_file:, how to convert img to hex?

  • Jonathan Root
    Jonathan Root over 10 years
    By saving a PIL.Image to an io.BytesIO(), I got the "UnsupportedOperation: fileno PIL" exception from CherryPy. I don't know if it's the same or something similar, but switching to Pillow worked as well, thanks.
  • Martijn Pieters
    Martijn Pieters over 10 years
    That'd be in Python 2, not 3... StringIO in Py3 would indicate you are using text mode and would gave to encode the data to get binary data, which could be corrupted because newline translation has been applied.
  • Martijn Pieters
    Martijn Pieters over 10 years
    Did you see my comments above on the question yet? :-)
  • Ying Xiong
    Ying Xiong almost 9 years
    I using app engine and am stuck with a PIL version that throws io.UnsupportedOperation: fileno exception. However, I found that version of PIL is happy to write to a StringIO object, as described here.
  • Martijn Pieters
    Martijn Pieters almost 9 years
    @YingXiong: this answer was specifically written for Python 3, not Python 2. The Google App Engine uses Python 2.7, not 3.
  • Jonny
    Jonny almost 9 years
    I'm here because I use App Engine as well and got in the same boat. My code worked well offline but when on Google's end I gave me the io.UnsupportedOperation: fileno
  • Martijn Pieters
    Martijn Pieters almost 4 years
    @RonaldoFelipe then make sure you find out what format you need to save it in. That’s not something I can help with, not without a lot more information about what you need to do with the image data.