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!
Related
I am writing an application that consists of three tkinter windows on the same page: a calendar, a notepad and a 'picture of the day' which is fetched from a bank of images when initiated by the user. The image is named after the calendar date + .jpg. It works fine...the first time. When I select another date and retrieve the image for that date, it comes up behind the first image in the 'picture of the day' window. The problem is that the first image does not disappear when replaced by another. I do not want to close the window, just close the current picture and replace it by the new one. There might a simple way, but I spent hours searching for it. The code below of part of the application. Hopefully, it shows where the problem is. Can you point me in the right direction? Thanks
from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
import os
photo = Toplevel()
photo.geometry("300x250+1300+150")
photo.resizable(width=True, height=True)
photo.attributes("-topmost", True)
def openfn(): # To go fetch a new image
filename = filedialog.askopenfilename(title='open')
return filename
root.destroy()
def open_img(): # To open a new image
x = openfn()
img = Image.open(x)
img = img.resize((225,200), Image.ANTIALIAS)
im = ImageTk.PhotoImage(img)
panel = Label(photo, image=im)
panel.image = im
panel.pack()
img=img.save(cal.calphoto) # Saves the image (calphoto is date + .jpg)
def retrieve_photo(): # To open an existing image
img=Image.open(cal.calphoto)
im = ImageTk.PhotoImage(img)
panel = Label(photo, image=im)
panel.image = im
panel.pack()
I changed a few things in your code. The main thing is the take the label with the image into the global scope so the function open_img() can make changes to it instead of creating a new label each time it is called. Below open_img() now configures the label to show the image selected each time it is called. (This is basically illustrating what Delrius said.)
from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
import os
photo = Toplevel()
photo.geometry("300x250+1300+150")
photo.resizable(width=True, height=True)
photo.attributes("-topmost", True)
panel = Label(photo) # panel Label moved into global scope so the
# functions below can access it
def openfn():
filename = filedialog.askopenfilename(title='open')
return filename
root.destroy()
def open_img():
x = openfn()
img = Image.open(x)
img = img.resize((225,200), Image.ANTIALIAS)
im = ImageTk.PhotoImage(img)
panel.config(image=im) # panel label is configured to show image selected
panel.image = im
panel.pack()
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'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()
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.