1

I'm trying to open image files and display them in python 3.8 using Tkinter and Pillow, but something is scaling the images wrong on my screen.

import tkinter as tk
from PIL import Image, ImageTk


class ViewingWindow(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.image = None
        self.canvas = tk.Canvas(self, width=500, height=500)
        self.canvas.pack()

    def setImage(self, img):
        self.image = img
        print(img.width())
        print(img.height())
        print(self.canvas["width"])
        print(self.canvas["height"])
        self.canvas.create_image(0, 0, anchor=tk.NW, image=img)


window = tk.Tk()

canvas = ViewingWindow(window)
canvas.pack()

img = Image.open("500x500.jpg")
img = ImageTk.PhotoImage(img)
canvas.setImage(img)

window.mainloop()

This is the result, shown for reference is Windows image viewer at "show actual size", and Gimp at scaling=100%: enter image description here

The 4 print statements all show "500", every part of the system seems to agree that the image is shown at 500x500, except the actual pixels on the screen. For whatever reason it's scaled to something close to 750x750, what in the world is scaling my image? This is consistent for all images I've tried to open in Tkinter, and regardless on window size and widget sizes.

Tested on Windows 10 with screen resolution 1920x1080.

  • 1
    you should add image `500x500.jpg` so we could see if this file makes problem in other computers. Or maybe image has other informations which makes it bigger on Canvas - like information about DPI. – furas Apr 11 '20 at 01:57
  • @furas https://lh3.googleusercontent.com/proxy/whPnifk9sDrCazOnmcvPfeRVJ1tKtn39SY3yb2LlCoXWQ0fNQJbS4leBW7gIfv_fnKghhzP5wUsjAWn2u-3Aywt-svaIH44U7j1c-emP8RXz5NuMPQ-Y1TIygA – MikkelBybjerg Apr 11 '20 at 02:18
  • you should add it in question - more people will see it and maybe someone will check it. – furas Apr 11 '20 at 02:32
  • image looks the same in Tkinter and GIMP on Linux on very old computer. I suspect: maybe problem is system or hardware if it uses HDPI and maybe Tkinter can't display it correctly. – furas Apr 11 '20 at 02:42
  • maybe this can help https://stackoverflow.com/questions/34132203/scaling-of-tkinter-gui-in-4k-38402160-resolution – furas Apr 11 '20 at 02:45

1 Answers1

2

it's scaled to something close to 750x750

750/500 = 150%.It seems that your system zoom ratio is 150%.

enter image description here To show the right image size.Just like furas said,you need to use DPI awareness.Read the problem in MSDN official doc

About DPI awareness.

To solve the problem,you can set the DPI awareness in your code.(Or you can change the zoom ratio to 100%) In my PC,the are not the same size without DPI awareness.Now after set the DPI awareness: enter image description here

Add this in your code:

import ctypes

ctypes.windll.shcore.SetProcessDpiAwareness(2) # this could only be used when your version of windows >= 8.1
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49