PIL clipboard image for Base64 string

I want to get the image to the clipboard and convert its data to base64 encoding so that I can put this in the HTML img tag.

I tried the following:

from PIL import ImageGrab from base64 import encodestring img = ImageGrab.grabclipboard() imgStr = encodestring(img.fp.read()) 

Plus some other combinations, all of which give me misconceptions.

I struggle with documents on this; Does anyone have an idea on how to do this?

+5
source share
1 answer

ImageGrab.grabclipboard() returns an Image object. You need to convert it to a known image format, for example jpeg or png, then encode the resulting string in base64 to be able to use it in the HTML img tag:

 import cStringIO jpeg_image_buffer = cStringIO.StringIO() image.save(jpeg_image_buffer, format="JPEG") imgStr = base64.b64encode(jpeg_image_buffer.getvalue()) 

(the answer has been edited to correct a typo).

+10
source

Source: https://habr.com/ru/post/1200399/


All Articles