I have an image that's encoded as a data url.
How do I display the original image from this in python?
You can use Tkinter to open a window for viewing the image and urllib to read the image data e.g;
import io
import base64
try:
import Tkinter as tk
from urllib2 import urlopen
except ImportError:
import tkinter as tk
from urllib.request import urlopen
root = tk.Tk()
image_url = "data:image/png;base64,iVB........"
image_byt = urlopen(image_url).read()
image_b64 = base64.encodestring(image_byt)
photo = tk.PhotoImage(data=image_b64)
cv = tk.Canvas(bg='white')
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(10, 10, image=photo, anchor='nw')
root.mainloop()
Related
Attribute Error: type object 'Image' has no attribute 'open'
this code is giving this error
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk
\#from PIL import Image
import PIL.Image
root=Tk()
root.geometry('300x400')
Button(root,text='open second window',command=open).pack()
def open():
global myimage , img
img=PIL.Image.open("C:\\Users\\HP\\Desktop\\test\\img_lights.jpg")
myimage = ImageTk.PhotoImage(img)
top=Toplevel()
top.geometry("300x400")
Button(top,text='close window',command=top.destroy).pack()
Label(top,image=myimage).pack()
mainloop()
I want the image to come on top level window but it is showing attribute error
I do not used PIL. Re-arranged your code in order to make it working. Don't use double slash.
Code:
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk
import PIL.Image
root=Tk()
root.geometry('300x400')
def open():
global myimage , img
img=PIL.Image.open(r"C:\Users\HPDesktop\test\img_lights.jpg")
myimage = ImageTk.PhotoImage(img)
top=Toplevel()
top.geometry("300x400")
Button(top,text='close window',command=top.destroy).pack()
Label(top,image=myimage).pack()
Button(root,text='open second window',command=open).pack()
root.mainloop()
Output:
I am trying to use a tkinter button to display a ERD diagram using sqlalchemy and PIL.
I have managed to get this to work by saving the picture generated to a file and then re-opening that file in order to display in a label.
Is there a way to get the image to display without having to save it first?
import tkinter as tk
from sqlalchemy_schemadisplay import create_schema_graph
import urllib
from sqlalchemy import MetaData, create_engine
from PIL import Image, ImageTk
root = tk.Tk()
def erd_gen(pr):
global erd_img
conn = create_engine("mssql+pyodbc:///?odbc_connect={}".format(pr))
grph_data = MetaData(bind=conn)
graph = create_schema_graph(metadata=grph_data, show_datatypes=False, show_indexes=False, rankdir='LR', concentrate=False)
graph.write_png('dbschema.png') # write file to folder
erd_img = ImageTk.PhotoImage(Image.open("dbschema.png")) #open file from folder, would like to do this by just referring to 'graph' and not the saved file
panel.configure(image = erd_img)
conn.close()
params = urllib.parse.quote_plus("DRIVER={SQL Server Native Client 11.0};"
"SERVER=(localdb)\ProjectsV13;"
"DATABASE=Test;"
"Trusted_Connection=yes")
tk.Button(root, text='Generate', command=lambda: erd_gen(params)).pack()
panel = tk.Label(root)
panel.pack()
root.mainloop()
You can use create_png() instead of write_png() to create a PNG image data buffer, then use io.BytesIO to simulate a file input stream:
from io import BytesIO
def erd_gen(pr):
global erd_img
conn = create_engine("mssql+pyodbc:///?odbc_connect={}".format(pr))
grph_data = MetaData(bind=conn)
graph = create_schema_graph(metadata=grph_data, show_datatypes=False, show_indexes=False, rankdir='LR', concentrate=False)
iostream = BytesIO(graph.create_png())
erd_img = ImageTk.PhotoImage(file=iostream)
panel.configure(image=erd_img)
conn.dispose()
You can save it to a file-like object with io, see example:
from tkinter import *
from PIL import Image, ImageTk
import urllib.request
import io
root = Tk()
show = Label(root)
show.pack()
img_url = 'http://hem.bredband.net/b235373/kroppkakor/DSCF0002.JPG'
with urllib.request.urlopen(img_url) as url:
temp_file = io.BytesIO(url.read())
img = Image.open(temp_file)
tkimg = ImageTk.PhotoImage(image=img)
show.config(image=tkimg)
root.mainloop()
i'm trying to show a picture in TKInter. I'm using the Pycapture library in a FLEA3 Point Grey Camera. I can take the picture, but when i try to show in my GUI, show the error saying Pycapture does not have the attribute "read"
import PyCapture2
from sys import exit
from PIL import ImageTk
import PIL.Image
from time import sleep
from tkinter import *
enableEmbeddedTimestamp(camera, True)
camera.startCapture()
img = ImageTk.PhotoImage(PIL.Image.open(grabImages(camera, 1)))
showImage = Label(imageContainer, image=img)
showImage.pack(side=BOTTOM, expand="yes")
imageLabel.create_image(20, 20, anchor="n", image=img)
camera.stopCapture()
I'm trying to resize and apply antialiasing to an image I previously displayed in Tkinter. I'm reading it from a url. The problem is, I opened the image with tkinter.PhotoImage, and the resize() function I need is in PIL.Image. I'd like to know if there's a way to convert from one to another, or some other way I can resolve this issue.
Here's the code:
import tkinter
from urllib.request import urlopen
import base64
from PIL import Image
window = tkinter.Tk()
url = "https://s.yimg.com/os/weather/1.0.1/shadow_icon/60x60/partly_cloudy_night#2x.png"
b64_data = base64.encodestring(urlopen(url).read())
image = tkinter.PhotoImage(data=b64_data)
# Function I need:
#image = image.resize((100, 100), Image.ANTIALIAS)
label = tkinter.Label(image=image)
label.pack()
window.mainloop()
If there's a completely different way I can achieve this, I'd like to hear it.
Ok, well you first use PIL and then use PIL's TKinter Format to convert it to a TKinter Image. I don't have the urllib on my system so I've used Requests, but that part should be exchangeable.
import tkinter
import base64
from PIL import Image, ImageTk
import requests
from PIL import Image
from StringIO import StringIO
url = "https://s.yimg.com/os/weather/1.0.1/shadow_icon/60x60/partly_cloudy_night#2x.png"
r = requests.get(url)
pilImage = Image.open(StringIO(r.content))
pilImage.resize((100, 100), Image.ANTIALIAS)
window = tkinter.Tk()
image = ImageTk.PhotoImage(pilImage)
label = tkinter.Label(image=image)
label.pack()
window.mainloop()
There is one whole page dedicated for PIL and Tkinter: http://effbot.org/tkinterbook/photoimage.htm
I edited #user1767754 code since I had some problems with it, but it did help me greatly.
Note: I used BytesIO instead of StringIO. Also, I added image mode to 'RGBA' since I have problems when displaying grey-scale images. Also, minor fixes.
Code:
import tkinter
from PIL import Image, ImageTk
from io import BytesIO
import requests
window = tkinter.Tk()
url = "https://s.yimg.com/os/weather/1.0.1/shadow_icon/60x60/partly_cloudy_night#2x.png"
r = requests.get(url)
pilImage = Image.open(BytesIO(r.content))
pilImage.mode = 'RGBA'
pilImage = pilImage.resize((50, 50), Image.ANTIALIAS)
image = ImageTk.PhotoImage(pilImage)
label = tkinter.Label(image=image)
label.pack()
window.mainloop()
I am Trying to set an image as the background of my Tkinter window and everything I try doesn't seem to work. Here's the code I have:
import random
import Tkinter # note use of caps
from Tkinter import *
from PIL import Image, ImageTk
import os.path
window = Tk()
window.title('Ask If You Dare')
window.configure(background = '#007501')
If you want the background to be an image (without using canvas), use labels:
import random
import Tkinter # note use of caps
from Tkinter import *
from PIL import Image, ImageTk
import os.path
window = Tk()
window.title('Ask If You Dare')
photo=PhotoImage(file="path/to/your/file")
l=Label(window,image=photo)
l.image=photo #just keeping a reference
l.grid()