#!/usr/bin/python
# ==================================================================
# Tk Canvas Image
# ------------------------------------------------------------------
# Base on: stackoverflow.com/questions/43009527/
# how-to-insert-an-image-in-a-canvas-item
# From: www.youtube.com/watch?v=uNDuHcnmJ4A
# ==================================================================
from tkinter import *
from PIL import Image
cwidth = 800 # canvas/image width
cheight = 300 # canvas/image height
csave = 'xyz.png' # saved resized image
def ImageSize(title,img):
w,h = img.size
print('{} width={} height={}'.format(title,w,h))
return (w,h)
def ScaleResizeImage(image):
img = Image.open(image)
w,h = ImageSize('original',img)
# resize
img.resize((cwidth,cheight)).save(csave)
img.close()
# display new image sizes
f = csave
img = Image.open(f)
ImageSize(csave,img)
img.close()
if __name__ == '__main__':
root = Tk()
ScaleResizeImage('ludlow.png')
# create the canvas, size in pixels
canvas = Canvas(width=cwidth, height=cheight, bg='black')
# pack the canvas into a frame/form
canvas.pack(expand=YES, fill=BOTH)
# load an image file
img = PhotoImage(file=csave)
# put an image on the canvas
canvas.create_image(0, 0, image=img, anchor=NW)
# event loop
root.mainloop()