The main problem is that, from what I could find, there is no easy/documented way to load an image from base64 encoded image. I use the following code to encode the image to base64 (so that I wouldn't need to include all the images with the source, nor should I create temp files and delete them at exit). The image format I use is .png which is supported in Gtk3+. (from GdkPixbuf.Pixbuf.get_formats() i have ['png'] in the results. I am really confused on how to use Gtk3+ for this purpose.
import base64
image_name = 'image.png'
image_loc = 'd:\\Home\\' + image_name
with open(image_loc, 'rb') as image_file:
encoded_string = base64.b64encode(image_file.read())
print(encoded_string)
I want to use the output for example:
base64_data="""
iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABsklEQVRYhe2XIVMCQRSAv2AwGAgEAuFmJBiMFmcMBH8AgUAgXHDMFww2gtFA
IBicsRgNBgLBcS7yE4gEIsFgIBDO8Pa8x7FwB7cnxTfzyr637327b+/dLiTSBIbAHIgydAGMgAscyUOOpDZdAu2iyZsq4BcwAHpb9NE1xFAl
P8s558klRFzzwQ5zejgsRxygVxBgbwiXAHtBuAaIIa7KBAgyACJgBlTKAqgBH8A0pWmIXKXYB2CbdFRM/xAA3qEBKipm8A9wCIAa8q/oUOJn
6FTKAqgA10gZWkD9rwAugRfWm1IEfCKlKQ2ghdwrstp0vwyAuiX5HGnRMwtE1zVAfLPS6hubZ7HNgaorgFPkppxOEvcBG0AE3LoCuGZ1Zb7R
hrGfqLGJ8h24ArhTcaYZvqHyDV0BtFWcGbLlHrJygCM1Nla+r5Cc0OcCAA3sNfaN3dtgDwDeSO5xzQIQthvRNoAlcA7yGFmowTFSmzz6jmwv
rL6wYp0Yv7HFttKMusC3xSmP3qs4/ZxzJiTn41c85N032mEHQqQBHacWs+mFvTSQa8ldSxW4Qb7zEDntAabmWn4A0clKl9nNvDwAAAAASUVO
RK5CYII
"""
And render the image from base64.
As a side note, on tkinter this was easily done with:
tkinter.PhotoImage(data=base64_data)
And then display the image where you needed it.
Getting back to Gtk3+, I didn't find a method of loading the image from base64. Even with GdkPixbuf.Pixbuf.new_from_data, I get a broken image. I have also tried with Gio.MemoryInputStream.new_from_bytes, but it says that the format of the image isn't supported.
Your data is base64 encoded, in order for Gtk3+ to use it, you must first decode it:
import base64
raw_data = base64.b64decode(data)
Then you were right with GdkPixbuf.Pixbuf.new_from_data:
(I cannot test, but I think this may work)
import base64
raw_data = base64.b64decode(data)
image = GdkPixbuf.Pixbuf.new_from_data(raw_data)
image_show_2.set_from_pixbuf(image)
Else you can do as you showed:
import base64
raw_data = base64.b64decode(data)
byting = GLib.Bytes(raw_data)
inputing = Gio.MemoryInputStream.new_from_bytes(byting)
image = GdkPixbuf.Pixbuf.new_from_data(inputing)
image_show_2.set_from_pixbuf(image)
Related
I'm trying to open a PNG file with Python. I do believe I have a properly encoded PNG.
It starts with:
\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR
And ends with:
\x00IEND\xaeB`\x82
My code so far:
import PIL.Image as Image
with open('./test_image_3.txt', 'rb') as f:
b = f.read()
b = base64.b64decode(b).decode("unicode_escape").encode("latin-1")
b = b.decode('utf-16-le')
img = Image.open(io.BytesIO(b))
img.show()
b = base64.b64decode(b).decode("unicode_escape").encode("latin-1")
UnicodeDecodeError: 'unicodeescape' codec can't decode bytes in position 178-179: truncated \uXXXX escape
Unfortunately I can't read the file you've provided as the website butchered it massively. Either use pastebin or github (or something similar) where it'll be possible to retrieve text/plain e.g. via curl so I can attempt to reproduce the problem 1:1 for the contents.
However, the general approach would be this:
from PIL import Image
with Image.open("./test_image_3.txt") as im:
im.show()
it's directly from Pillow's documentation and it does not care about the file's name or extension.
Alternatively, if you have open() call with a file handle:
from PIL import Image
with open("./test_image_3.txt", "rb") as file:
with Image.open(file) as im:
im.show()
And if you have it mangled somehow, then judging from your encode() and decode() calls it would be this:
from PIL import Image
from io import BytesIO
data = <some raw PNG bytes, the original image>
# here I store it in that weird format and write as bytes
with open("img.txt", "wb") as file:
file.write(data.decode("latin-1").encode("unicode_escape"))
# here I read it back as bytes, reverse the chain of calls and invert
# the call pairs for en/decoding so encode() -> decode() and vice-versa
with open("img.txt","rb") as file:
content = BytesIO()
content.write(
file.read().decode("unicode_escape").encode("latin-1")
)
# seek back, so the BytesIO() can return back the full content
content.seek(0)
# then simply read as if using a file handle
with Image.open(content) as img:
img.show()
I am trying to decode a base64 string to an image object in python in google app engine. i
imported Image from PIL
from PIL import Image
and try to decode like this
image = Image.fromString('RGBA', (iWidth, iHeight), decodeString(imageEncodedString))
i get error saying type object 'Image' has no attribute 'fromString'
So how do i get around doing this :
send a base64 image to google app engine as request. Decode it on server. Do some transformations and encode it back to base64 and return as response.
Edit
As mentioned in comments first issue was a typo and second was that iWidth and iHeight were interpreted as string. so i changed my code to
image = Image.fromstring('RGB', (int(iWidth), int(iHeight)), decodestring(imageEncodedString))
But now i get the error
ValueError: not enough image data
Here is my base64 encoded image
iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAWJJREFUeNrs3T0OgjAYgGEwnIow4MnUk8lAOnsEbqKSuOrwFVTK86ymJOJry08CVQUAAAAAAABAgerIoLbtru8+S2k82q3b0QTH9XZdGQ52gQAQAAJAAAgAASAABIAAEAACQAAIAAEgAASAABAApWjsgri27e4Zwy8pjWczAAJAAAgAASAABIAAEABfsZkrgSVcdTMDIAAEgAAQAAJAALgOwOz16L0+OHyIPp7PDGAJQAAIAAHgLGDxI1t38MwACAABIAAEwK7OAv7R88ykrzLeeFLamckebwbNP/4pY3xRAVgCHAMgAASAABAAAkAACAABIAAEgAAQAAJAAAgAASAABIAAEAACQAAIAAEgAASAABAAAkAACAABIAAEgAD4Q2s/JWzIGDuttK0pc1trfb9bxrbCY+vIoE9PAU9prP2vLAEIgC1YfAngZ0Iv2DADWAIQAAJAAAgAAAAAAACAkjwEGAC6hS93fcHebgAAAABJRU5ErkJggg==
I ended up doing this:
from PIL import Image
from cStringIO import StringIO
from base64 import decodestring
decoded_img_str = base64.decodestring(imageEncodedString)
imgfile = StringIO(decoded_img_str)
image = Image.open(imgfile)
I wrote this code :
with Image.open(objective.picture.read()) as image:
image_file = BytesIO()
exifdata = image.info['exif']
image.save(image_file, 'JPEG', quality=50, exif=exifdata)
zf.writestr(zipped_filename, image_file)
Which is supposed to open the image stored in my model (this is in a Django application). I want to reduce the quality of the image file before adding it to the zipfile (zf). So I decided to work with BytesIO to prevent writing useless file on the disk. Though I'm getting an error here. It says :
embedded NUL character
Could someone help me out with this ? I don't understand what's going on.
Well I was kind of dumb. objective.picture.read() returns a byte string (really long byte string...) so I shouldn't have used Image but ImageFile.Parser() and feed that byte string to the parser so it can return an Image that I can work with. Here is the code :
from PIL import ImageFile
from io import BytesIO
p = ImageFile.Parser()
p.feed(objective.picture.read())
image = p.close()
image_file = BytesIO()
exifdata = image.info['exif']
image.save(image_file, 'JPEG', quality=50, exif=exifdata)
# Here zf is a zipfile writer
zf.writestr(zipped_filename, image_file.getvalue())
The close() actually returns the image parsed from the bytestring.
Here is the doc : The ImageFile Documentation
Is it possible to generate a functional image tag in html from a BytesIO buffer? I'd like to do something along these lines:
import matplotlib
matplotlib.use('Agg')
import pylab
import Image
import io
temp_data = {'x':[1,2,3],'y':[2,4,5]}
pylab.plot(temp_data['x'], temp_data['y'])
img_buffer = io.BytesIO()
pylab.savefig(img_buffer, format = 'png')
img_buffer.seek(0)
img_tag = "<img src='data:image/png;base64,'" + img_buffer.getvalue() + "</img>"
May be necessary to re-format the value of the buffer in some way, or to change the content of the 'src' data. Thank you.
Python2
Towards the end of the code above, do this
import base64
img_tag = "<img src='data:image/png;base64," + base64.b64encode(img_buffer.getvalue()) + "'/>"
Python3
For this to work in python3 you will need to decode the bytes variable generated from base64.b64encode using str.decode method into a string as follows
import base64
str_equivalent_image = base64.b64encode(img_buffer.getvalue()).decode()
img_tag = "<img src='data:image/png;base64," + str_equivalent_image + "'/>"
If you are working with Flask, then you can return the UTF-8 format of the image and play with it.
figfile = BytesIO()
plt.savefig(figfile, format='png')
plt.clf() # this will clear the image
figfile.seek(0)
figdata_png = base64.b64encode(figfile.getvalue())
return figdata_png.decode('UTF-8')
Remember to mention it in <img/> tags. This is to implement in Flask.
I am looking to create base64 inline encoded data of images for display in a table using canvases. Python generates and creates the web page dynamically. As it stands python uses the Image module to create thumbnails. After all of the thumbnails are created Python then generates base64 data of each thumbnail and puts the b64 data into hidden spans on the user's webpage. A user then clicks check marks by each thumbnail relative to their interest. They then create a pdf file containing their selected images by clicking a generate pdf button. The JavaScript using jsPDF generates the hidden span b64 data to create the image files in the pdf file and then ultimately the pdf file.
I am looking to hopefully shave down Python script execution time and minimize some disk I/O operations by generating the base64 thumbnail data in memory while the script executes.
Here is an example of what I would like to accomplish.
import os, sys
import Image
size = 128, 128
im = Image.open("/original/image/1.jpeg")
im.thumbnail(size)
thumb = base64.b64encode(im)
This doesn't work sadly, get a TypeErorr -
TypeError: must be string or buffer, not instance
Any thoughts on how to accomplish this?
You first need to save the image again in JPEG format; using the im.tostring() method would otherwise return raw image data that no browser would recognize:
from io import BytesIO
output = BytesIO()
im.save(output, format='JPEG')
im_data = output.getvalue()
This you can then encode to base64:
image_data = base64.b64encode(im_data)
if not isinstance(image_data, str):
# Python 3, decode from bytes to string
image_data = image_data.decode()
data_url = 'data:image/jpg;base64,' + image_data
Here is one I made with this method:
data:image/jpg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCACAAIADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKS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snww8ZF9v9g3WffaB+ea9i+GHwzl8LSNq2qsrag6bI4l5EIPXnue350wPUKKKKYBRRRQB/9k=
Unfortunately the Markdown parser doesn't let me use this as an actual image, but you can see it in action in a snippet instead:
<img src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCACAAIADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3+iiigAooooAKwde8Y6D4aQ/2nqEccuNwhU7pG/4CKx/iX4zfwd4eEtqEN/cv5cAb+Hj5nx3xx+JFfMF3fXN/dSXN1M808rbnkc5LE0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s/hl4OtfGfio2d+zizghM8qocF8EALnt1/SgRwwb5sU8V6D8YvBemeEfEFgdIiMNpdwFvJLltrKcHBJzzkfrXn69KAHqea7r4VQ+b8Q9JXHR2bn2RjXDL1r0L4PIG+I2nn+6kp/8AIbUDPp+iiiqAKKKKAMCPw3EB8zUTeHUKnYwJrfozSsPmZ59qGh3cGWWI7fWsGXfCSH4r11lVlIIBB7Vi6l4YstQU8eWx7ilYdz5p+IwB1yFxg7rden+81cbwa988ZfCXU9ViWSxlhlmizsBbbuB7HNefN8H/ABuD/wAgXP8A28xf/FUEs4Va9C+D+qHSfGhkIzFJbsknsMqf6UkPwZ8aSMA2mxRj1a5j/oa9M8FfCNNI0yZtVn/0+fGfJOVjUds96AOF+O+pR6nrmkmFt0Mdu+D7luf6V5Wte8eLPhFq+ooBaTxXAQ5jLNtYexz/AI1wFx8I/GluxA0dpR6xyof/AGagDiV613Hwr83/AIWNpHlHne2f93Y2f0qsnww8ZF9v9g3WffaB+ea9i+GHwzl8LSNq2qsrag6bI4l5EIPXnue350wPUKKKKYBRRRQB/9k="/>
In Python 3, you may need to use BytesIO:
from io import BytesIO
...
outputBuffer = BytesIO()
bg.save(outputBuffer, format='JPEG')
bgBase64Data = outputBuffer.getvalue()
# http://stackoverflow.com/q/16748083/2603230
return 'data:image/jpeg;base64,' + base64.b64encode(bgBase64Data).decode()
thumb = base64.b64encode(im.tostring())
I think would work
I use PNG when I save to the buffer. With JPEG the numpy arrays are a bit different.
import base64
import io
import numpy as np
from PIL import Image
image_path = 'dog.jpg'
img2 = np.array(Image.open(image_path))
# Numpy -> b64
buffered = io.BytesIO()
Image.fromarray(img2).save(buffered, format="PNG")
b64image = base64.b64encode(buffered.getvalue())
# b64 -> Numpy
img = np.array(Image.open(io.BytesIO(base64.b64decode(b64image))))
print(img.shape)
np.testing.assert_almost_equal(img, img2)
Note that it will be slower.