How to resize image using canvas frame in python - python

Here I am trying to display images using the canvas frame but it is not showing anything.
what is wrong in this.
when I replace the frame with root in Label widget it is working.
Thanks
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
import tkinter as tk
root = Tk()
root.title("Title")
root.geometry('600x600')
def resize_image(event):
new_width = event.width
new_height = event.height
image = copy_of_image.resize((new_width, new_height))
photo = ImageTk.PhotoImage(image)
label.config(image = photo)
label.image = photo
def on_configure(event):
canvas.configure(scrollregion=canvas.bbox('all'))
scrollbary = Scrollbar(root, orient = tk.VERTICAL)
scrollbarx = Scrollbar(root,orient = tk.HORIZONTAL)
canvas = tk.Canvas(root,yscrollcommand = scrollbary.set,
xscrollcommand = scrollbarx.set)
scrollbary.config(command=canvas.yview)
scrollbarx.config(command=canvas.xview)
scrollbary.pack(side=tk.RIGHT, fill='y',expand = tk.FALSE)
scrollbarx.pack(side=tk.BOTTOM,fill = 'x',expand = tk.FALSE)
canvas.pack(side=tk.LEFT, padx=5, pady=5,
fill=tk.BOTH, expand=tk.TRUE)
canvas.bind('<Configure>', on_configure)
frame = tk.Frame(canvas)
canvas.create_window((0,0), window=frame, anchor='nw')
image = Image.open('logo1.png')
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = ttk.Label(frame, image = photo)
label.bind('<Configure>', resize_image)
label.pack(fill=BOTH, expand = YES)
root.mainloop()

Related

How can I hide the image in Tkinter Python

I want to hide the image in Tkinter Python module. How can I do it?
For example:
from tkinter import *
root = Tk()
root.title("Example")
root.geometry("500x500")
root.resizable(0, 0)
def hide():
# How can I hide the image?
canvas = Canvas(root, width=500, height=500)
canvas.pack()
image = PhotoImage(file='example.png')
img = canvas.create_image(0, 0, anchor=NW, image=image)
hide = Button(root, text="Hide", command=hide)
hide = canvas.create_window(0, 30, anchor=NW, window=hide)
root.mainloop()
I try the [image].destory(), but it's not working.
If you want to remove a canvas object from a canvas, you can use the delete method of the canvas. It takes as a parameter the id of the object to be deleted:
canvas.delete(img)

There was a problem in getting coordinates inside the image (not the main root window root tkinter

There was a problem in getting the coordinates inside the picture (not the main root window, the source code will be at the end), the image is loaded using cv2 and added to the application interface via label. How can I get coordinates when clicking the mouse only on the picture? If it is possible to do this through tkinter, again only inside the picture.
import tkinter as tk
from tkinter import ttk, filedialog
import cv2
from PIL import Image
from PIL import ImageTk
class interface:
def __init__(self):
self.panelA = None
self.path = None
self.s = None
self.a = None
self.root = tk.Tk()
self.f1 = tk.Frame(self.root)
self.f1.grid(row=0, column=0)
self.f2 = tk.Frame(self.root)
self.f2.grid(row=0, column=1)
self.e = tk.Entry(self.f1)
self.e.grid(column=1, row=1, sticky='ew')
self.l1 = tk.Label(self.f1, text="Choose a picture")
self.l1.grid(column=0, row=0, sticky='ew')
self.l2 = tk.Label(self.f1, text="Введите напор H, м")
self.l2.grid(column=0, row=1, sticky='ew')
b1 = tk.Button(self.f1, text="Specify the file with the characteristic", command=self.select_image)
b1.grid(column=0, row=0, sticky='ew', columnspan=3)
b2 = tk.Button(self.f1, text="select the pressure")
b2.grid(column=2, row=1, sticky='ew')
columns = ['N', 'ny']
self.table = ttk.Treeview(self.f1, columns=columns, show='headings', height=20)
self.table.heading("N", text="N")
self.table.heading("ny", text="%")
self.table.grid(column=0, row=3, columnspan=3, sticky='ew')
self.root.mainloop()
def get_s(self):
return self.s
def select_image(self):
self.path = filedialog.askopenfilename()
if self.path:
image = cv2.imread(self.path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = Image.fromarray(image)
image = ImageTk.PhotoImage(image)
if self.panelA is None:
self.panelA = tk.Label(self.f2, image=image)
self.panelA.image = image
self.panelA.grid()
else:
self.panelA.configure(image=image)
self.panelA.image = image
obj_interface = interface()
I tried it in tkinter, but it turned out to click only in the entire application

Image size increases after zooming in and zooming out

Hi All I have a frame with a canvas within the frame which I am using to display an image. I have a scale configured for zooming-in and zooming-out the image. However, I realized that when I zoom-in the image and zoom-out, the image size increases. I am not sure why that's happening. Any suggestion to correct this is welcomed. Thanks. Below is my sample code for demonstration.
frame = tk.Frame(root, width=480, height=450)
frame.place(x=2, y=287)
canvas = tk.Canvas(frame, bg="gray80", bd=1, height=400, width=470)
canvas.pack()
def Create_Image():
global img, Img, canv_img, button
Img = Image.open('.\\image.png')
img = ImageTk.PhotoImage(Img.resize((420, 390), Image.ANTIALIAS))
canv_img = canvas.create_image(235, 207, image=img, anchor=CENTER)
def zoom_img(zoom):
global img
newsize = (Img.size[0]* int(zoom),
Img.size[1]*int(zoom))
scaledimg = Img.resize(newsize, Image.LINEAR)
img = ImageTk.PhotoImage(scaledimg)
canvas.itemconfig(canv_img, image=img)
var = StringVar()
img_scale = tk.Scale(root, variable=var, orient='horizontal', bd=1,
from_=1, to=5, length=200, resolution=1, command=zoom_img)
img_scale.place(x=120, y=140)
def show_image():
Create_Image()
button = tk.Button(root, width=220, height=25, command=show_image)
receive_button.place(x=130, y=50)

TKinter background resizer + buttons/labels

I'm trying to setup tkinter gui screen with wallpaper on the background when the bg image fit the resize of the screen, i have took code from here..but the problem is when i'm trying to put buttons on it or another labels, the wallpaper still stay on the top and i can't see them.
Here the code:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("Title")
root.geometry("800x600")
class Example(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.configure(background="black")
self.grid(sticky=N+S+E+W)
self.image = Image.open("bg.jpg")
self.img_copy= self.image.copy()
self.background_image = ImageTk.PhotoImage(self.image)
self.background = Label(self, image=self.background_image)
self.background.grid(row =0, column =0,sticky="nsew")
self.background.grid_rowconfigure(0, weight=1)
self.background.grid_columnconfigure(0, weight=1)
self.master = master
self.master.bind('<Configure>', self._resize_image)
# create a button
self.button= Button(root, text="EXIT", width=4).grid(row=3, column=1, sticky=N+S+E+W)
def _resize_image(self,event):
new_width = self.master.winfo_width()
new_height = self.master.winfo_height()
self.image = self.img_copy.resize((new_width, new_height))
self.background_image = ImageTk.PhotoImage(self.image)
self.background.configure(image = self.background_image)
e = Example(root)
e.grid(row =0, column =0,sticky="nsew")
e.grid_rowconfigure(0, weight=1)
e.grid_columnconfigure(0, weight=1)
root.mainloop()
You need to use place() to put the background image so that it does not interfere other widgets in the same parent.
Below is a simplified example based on your code:
from tkinter import *
from PIL import Image, ImageTk
class Example(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.configure(background="black")
self.image = Image.open("bg.jpg")
# label for the background image
self.background = Label(self)
self.background.place(x=0, y=0)
self.bind('<Configure>', self._resize_image)
# create a button
self.button = Button(self, text="EXIT", width=4)
self.button.grid(row=3, column=1, sticky=N+S+E+W, padx=50, pady=50)
def _resize_image(self,event):
if event.widget is self:
# resize background image to fit the frame size
image = self.image.resize((event.width, event.height))
self.background_image = ImageTk.PhotoImage(image)
self.background.configure(image=self.background_image)
root = Tk()
root.title("Title")
root.geometry("800x600")
e = Example(root)
e.pack(fill=BOTH, expand=1)
root.mainloop()

Tkinter Image not showing up

I'm quite new to coding and I'm trying to have an image show up in a tkinter window.
However, when I run the code below there is the gap where the image should be. I am getting no error from this code as well.
window2 = Toplevel()
window2.geometry("1920x1200")
Namearea = Label(window2, text="Please Name the Prebuild:")
Namearea.pack()
e = Entry(window2, width=50, borderwidth=3, bg="Light Grey", fg="black")
e.pack()
#Here is the part that is not working.
img3 = PhotoImage(file=r"C:\\Tkinter\\ComputerImage.png")
picture1 = Label(window2, image=img3)
picture1.pack()
SaveAndContinue = Button(window2, text="Save and Return to Main Menu", padx=75, pady=20, bg="Light Grey")
SaveAndContinue.pack()
Try the answer from josav09 for this question:
How to add an image in Tkinter?
from tkinter import *
from PIL import ImageTk, Image
import os
root = Tk()
img = ImageTk.PhotoImage(Image.open("True1.gif"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()

Categories