Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter photo image problem
#1
i have the basic code that follows


window = Tk()
window.title("Reminder Application")
window.geometry("1079x719")

bg_pic = PhotoImage(file="christ_photo.png")
canvas = Canvas(window, width=835, height=560)
canvas.grid(row=0, column=1, columnspan=10)
canvas.create_image(400, 337, image=bg_pic)
im trying to display a background image. it appears however that the picture in the interface i zoomed in.. i first attach a picture of the GUI of the app


this is my GUI window
[Image: M7RxQgR]

and here is the original picture
[Image: WB0x8B9]

for some reason when i display the picture to the interface it zooms in. Ive trying tinkering with the width and height params but i cant get it to zoom out so the whole picture can be in the interface
Reply
#2
You may be better off using a label to display the image

The images you posted are not showing
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
(Jun-24-2024, 04:12 PM)menator01 Wrote: You may be better off using a label to display the image

The images you posted are not showing

I tried using the label but i could get it to align in the middle and it opens the pic in its initial size whereas i want to crop it and have it in the centre of the app.. here are the two pictures i attached before

first the interface picture with the canvas.create_image option
https://ibb.co/M7RxQgR


and following that the original picture
https://ibb.co/WB0x8B9
Reply
#4
Are you trying to get something like this?

import tkinter as tk 
from PIL import Image, ImageTk
import os 

# Setting a path to executing script
path = os.path.realpath(os.path.dirname(__file__))

# Using pillow to open and resize image
image = Image.open(f'{path}/christ-photo.jpg')

# Get width and height of image
width, height = image.size

# Number to divide by
num = 6

# Set the size
size = width//num, height//num

image = image.resize(size)

root = tk.Tk()

# Using geometry manager
root.geometry('800x600+250+250')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

# Using frame as a container
container = tk.Frame(root)
container.grid(column=0, row=0, sticky='news', padx=10, pady=10)

# Configuring container columns
for i in range(5):
    container.grid_columnconfigure(i, weight=3)

# Setting image to display in tkinter
image = ImageTk.PhotoImage(image)

# creating a label and setting image
label = tk.Label(container, image=image)
label.grid(column=0, columnspan=5, row=0, sticky='news', pady=10)

# Ceating some test label. 
for i in range(5):
    label = tk.Label(container, text=f'label {i}')
    label.grid(column=i, row=1, sticky='new')

    # Setting columns using columnconfigure. Uniform ensures all columns are same size
    label.grid_columnconfigure(i, weight=3, uniform='col')

root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#5
(Jun-24-2024, 05:15 PM)menator01 Wrote: Are you trying to get something like this?

import tkinter as tk 
from PIL import Image, ImageTk
import os 

# Setting a path to executing script
path = os.path.realpath(os.path.dirname(__file__))

# Using pillow to open and resize image
image = Image.open(f'{path}/christ-photo.jpg')

# Get width and height of image
width, height = image.size

# Number to divide by
num = 6

# Set the size
size = width//num, height//num

image = image.resize(size)

root = tk.Tk()

# Using geometry manager
root.geometry('800x600+250+250')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

# Using frame as a container
container = tk.Frame(root)
container.grid(column=0, row=0, sticky='news', padx=10, pady=10)

# Configuring container columns
for i in range(5):
    container.grid_columnconfigure(i, weight=3)

# Setting image to display in tkinter
image = ImageTk.PhotoImage(image)

# creating a label and setting image
label = tk.Label(container, image=image)
label.grid(column=0, columnspan=5, row=0, sticky='news', pady=10)

# Ceating some test label. 
for i in range(5):
    label = tk.Label(container, text=f'label {i}')
    label.grid(column=i, row=1, sticky='new')

    # Setting columns using columnconfigure. Uniform ensures all columns are same size
    label.grid_columnconfigure(i, weight=3, uniform='col')

root.mainloop()

thanks for your help. if anybody sees this post looking for a solution i ended up just resizing the picture using paint in order to better match the dimensions of the widgets
Reply
#6
Your image might be zoomed in because of theĀ CanvasĀ size. Try resizing the image to fit the canvas before displaying it. from tkinter import Tk, Canvas from PIL import Image, ImageTk window = Tk() window.title("Reminder Application") window.geometry("1079x719") #Load and resize the image image = Image.open("christ_photo.png").resize((835, 560), Image.ANTIALIAS) bg_pic = ImageTk.PhotoImage(image) # Create the canvas and add the resized image canvas = Canvas(window, width=835, height=560) canvas.grid(row=0, column=1, columnspan=10) canvas.create_image(0, 0, anchor='nw', image=bg_pic) window.mainloop() This should make the image fit the canvas. Hope this helps!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem When using canny edge detection,black image returned rickyw2777 1 419 Feb-17-2025, 03:22 AM
Last Post: rickyw2777
  aoigram, pil Help with photo processing kolpac21 0 1,390 Aug-07-2023, 04:59 PM
Last Post: kolpac21
  Tkinter Pillow Resize Image Looks Choppy While Resizing AaronCatolico1 2 2,426 Jan-03-2023, 05:41 PM
Last Post: AaronCatolico1
  rotating image with Tkinter Frankduc 6 10,675 May-21-2022, 03:00 PM
Last Post: Frankduc
  tkinter layout problem agentperry 4 2,725 Jan-26-2022, 08:18 PM
Last Post: BashBedlam
  Problem posting image to clipboard noel 0 3,026 Sep-26-2020, 10:50 AM
Last Post: noel
  problem with libspatialindex using tkinter Timych 1 2,368 Aug-05-2020, 11:39 AM
Last Post: buran
  Problem using a button with tkinter SmukasPlays 6 4,708 Jul-02-2020, 08:06 PM
Last Post: SmukasPlays
  Problem loading an image dataset into Tensorflow MohammedSohail 1 2,274 Jun-09-2020, 02:09 PM
Last Post: nuffink
  cv2 problem when saving image majstr 7 7,539 Jan-21-2020, 05:39 PM
Last Post: majstr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020