#!/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)
# scale
img.resize((int(w/2),int(h/2))).save('xyz_half_size.png')
img.resize((w*2,h*2)).save('xyz_twice_size.png')
img.close()
# display new image sizes
f = csave
img = Image.open(f)
ImageSize(csave,img)
img.close()
f = 'xyz_half_size.png'
img = Image.open(f)
ImageSize(f,img)
img.close()
f = 'xyz_twice_size.png'
img = Image.open(f)
ImageSize(f,img)
img.close()
if __name__ == '__main__':
#root = Tk()
# create the canvas, size in pixels
##canvas = Canvas(width=800, height=300, bg='black')
# pack the canvas into a frame/form
##canvas.pack(expand=YES, fill=BOTH)
## load an image file
##img = PhotoImage(file='small_globe.gif')
##img = PhotoImage(file='ludlow.png')
# put an image on the canvas
## pic's upper left corner (NW) on the canvas is at x=50 y=10
##canvas.create_image(50, 10, image=image1, anchor=NW)
##canvas.create_image(0, 0, image=img, anchor=NW)
ScaleResizeImage('ludlow.png')
# event loop
#root.mainloop()