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()
Related
I'm trying to zoom in on an image and display it with the following code
import tkinter as tk
from PIL import Image as PIL_image, ImageTk as PIL_imagetk
window = tk.Tk()
img1 = PIL_imagetk.PhotoImage(file="C:\\Two.jpg")
img1 = img1.zoom(2)
window.mainloop()
But python says AttributeError: 'PhotoImage' object has no attribute 'zoom'. There is a comment on a related post here:
Image resize under PhotoImage
which says "PIL's PhotoImage does not implement zoom from Tkinter's PhotoImage (as well as some other methods)".
I think this means that I need to import something else into my code, but I'm not sure what. Any help would be great!
img1 has no method zoom, however img1._PhotoImage__photo does. So just change your code to:
import tkinter as tk
from PIL import Image as PIL_image, ImageTk as PIL_imagetk
window = tk.Tk()
img1 = PIL_imagetk.PhotoImage(file="C:\\Two.jpg")
img1 = img1._PhotoImage__photo.zoom(2)
label = tk.Label(window, image=img1)
label.pack()
window.mainloop()
By the way if you want to reduce the image you can use the method subsample img1 = img1._PhotoImage__photo.subsample(2) reduces the picture by half.
If you have a PIL Image, then you can use resize like following example:
import tkinter as tk
from PIL import Image, ImageTk
window = tk.Tk()
image = Image.open('C:\\Two.jpg')
image = image.resize((200, 200), Image.ANTIALIAS)
img1 = ImageTk.PhotoImage(image=image)
label = tk.Label(window, image=img1)
label.pack()
window.mainloop()
Note I just import Image and ImageTk, see no need to rename to PIL_image and PIL_imagetk, which to me is only confusing
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()
I want to have a image of a map next to my application I am creating right now, which includes a filter system. However I wasn't sure why it wasn't showing up. Here are snippits from my code
from Tkinter import *
import Tkinter as tk
from PIL import ImageTk, Image
import os
class MainPage(tk.Frame):
def __init__(self,app):
tk.Frame.__init__(self)
frame_A = Frame(self,width=930,height=780)
frame_A.grid(row=0,column=0,columnspan=2,rowspan=3)
contentmap = Frame(frame_A)
img = ImageTk.PhotoImage(Image.open("/Users/J/Desktop/unknown.png"))
panel = Label(contentmap, image = img)
App().mainloop()
Additionally I would like to ask for some advice as to whether it was possible to make my image a interactive image like if I were to zoom in google maps.
Thanks a million!
I am attempting to replicate the following code to output a cv2 image onto a GUI :
import numpy as np
import cv2
import Tkinter
from PIL import Image
from PIL import ImageTk
# Load an color image
img = cv2.imread('img.jpg')
#Rearrang the color channel
b,g,r = cv2.split(img)
img = cv2.merge((r,g,b))
# A root window for displaying objects
root = Tkinter.Tk()
# Convert the Image object into a TkPhoto object
im = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(image=im)
# Put it in the display window
Tkinter.Label(root, image=imgtk).pack()
root.mainloop() # Start the GUI
however on trying to execute this, I end up with the following error:
TclError: image "pyimage1" doesn't exist
In my understanding it is looking for something name pyimage1, I have searched by code multiple times and there is nothing by that name, unless this is a module I am missing.
The output is essentially just a blank GUI with no image being displayed accompanied by the error in my terminal.
Any guidance on how to proceed will be very helpful.
Try changing the line:
root = Tkinter.Tk()
to
root = Toplevel
.. Just a guess let me know if it works.
I am trying to load an image into a canvas in python. However I am getting the error: TclError: couldn't recognize data in image file "C:\testimage\spongesea.jpg"
import Tkinter
from Tkinter import *
import time
import inspect
import os
from PIL import Image, ImageTk
class game_one:
def __init__(self):
global root
global canvas_one
root = Tk()
root.title(" Thanks Josh Dark
canvas_one = Tkinter.Canvas(root, bg="BLACK")
canvas_one.pack(expand= YES, fill= BOTH)
canvas_one.focus_set() #allows keyboard events
p = PhotoImage(file="C:\testimage\spongesea.jpg")
canvas_one.create_image(0, 0, image = p, anchor=NW)
root.grab_set()#I forget what this does. Don't change it.
root.lift()#This makes root appear in front of the other applications
ObjectExample = game_one()# starts the animation
I can open the image manually from the file, so it is not corrupted, and it is calling the correct place. Any ideas? Thanks
PhotoImage works only with GIF and PGM/PPM.
You have to use Image, ImageTk to work with other formats
from PIL import Image, ImageTk
image = Image.open("lenna.jpg")
photo = ImageTk.PhotoImage(image)
BTW: read PhotoImage and see "Garbage Collection problem" in note.