Display base64 jpeg

Post here if you need help with creating a Graphical User Interface in Python.

Display base64 jpeg

Postby GoshFather » Tue Dec 03, 2013 3:54 am

I want to display a base64 encoded jpeg image with Tkinter. But when I run my code it tells me "couldnt identify image file". What am I doing wrong?


Code: Select all
import Tkinter, base64
from PIL import ImageTk, Image

root = Tkinter.Tk()
ImageEncoded = #### the image's binary data as an encoded string  ####
ImageForTK = ImageTk.PhotoImage(data = ImageEncoded) ##### this is where the error occurs
panel = Tkinter.Label(root, image = ImageForTk)
panel.pack(side = "bottom", fill = "both", expand = "yes")

root.mainloop()
Last edited by Mekire on Tue Dec 03, 2013 5:06 am, edited 1 time in total.
Reason: First post lock. Code Tags.
GoshFather
 
Posts: 8
Joined: Tue Dec 03, 2013 3:44 am

Re: Display base64 jpeg

Postby wuf » Wed Dec 04, 2013 7:56 pm

Hi GoshFather

Try this out:
Code: Select all
import io
import base64
try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk
from PIL import ImageTk, Image
   
root = tk.Tk()

with open("image.b64") as b64_data:
    jpg_str = base64.b64decode(b64_data.read())
    jpg_data = io.BytesIO(jpg_str)
           
pil_image = Image.open(jpg_data)
tk_image = ImageTk.PhotoImage(pil_image)

panel = tk.Label(root, image = tk_image)
panel.pack(side = "bottom", fill = "both", expand = "yes")

root.mainloop()

wuf ;)
wuf
 
Posts: 38
Joined: Fri Feb 08, 2013 6:42 am

Re: Display base64 jpeg

Postby Hissing Cockroach » Fri Dec 06, 2013 3:19 pm

I would suggest using PNGs. JPGs aren't the best file type to use.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Display base64 jpeg

Postby GoshFather » Thu Dec 12, 2013 12:04 am

I tried it and it gives me a type error "incorrect padding" for the line "jpg_str = base64.b64decode(b64_data.read())" I am using python 2.6.2 by the way

am I supposed to replace the "image.b64" with my long image string? Because my aim is to have the image data come already embeded into the script itself.
GoshFather
 
Posts: 8
Joined: Tue Dec 03, 2013 3:44 am


Return to GUI

Who is online

Users browsing this forum: W3C [Linkcheck] and 1 guest