python Image PIL to binary Hex

前端 未结 1 582
一整个雨季
一整个雨季 2021-01-12 09:00
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import urllib.request
import io
import binascii

data = urllib.request.urlopen(\'http://pas         


        
相关标签:
1条回答
  • 2021-01-12 09:31

    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.

    0 讨论(0)
提交回复
热议问题