#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2022/04/06 10:33:32 +0900> """ tkinter Canvas + Scrollbar + ImageOps sample. 参考ページ 【Python/tkinter】Canvasに画像を表示する | イメージングソリューション https://imagingsolution.net/program/python/tkinter/canvas_draw_image Pythonの文法メモ: 【Pillow】ImageOpsモジュールによる画像拡大縮小・トリミング・パディング https://python-no-memo.blogspot.com/2020/04/pillowimageops_26.html Tkinterの使い方 : スクロールバー(Scrollbar)の使い方 | だえうホームページ https://daeudaeu.com/scrollbar/ Python, Pillowで画像を一括リサイズ(拡大・縮小) | note.nkmk.me https://note.nkmk.me/python-pillow-image-resize/ 【Python/tkinter】マウスホイールで画像のリサイズを行う | だえうホームページ https://daeudaeu.com/tkinter-mousewheel/ * Windows10 x64 21H2 + Python 3.9.12 64bit + Pillow 9.0.1 ... success * Windows10 x64 21H2 + Python 2.7.18 32bit + Pillow 6.2.2 ... failure. MemoryError """ import sys import platform if sys.version_info.major == 2: # Python 2.7 import Tkinter as tk else: # Python 3.x import tkinter as tk from PIL import Image, ImageTk, ImageOps class App(tk.Frame, object): def __init__(self, master=None): try: # Python 3.x super().__init__(master) except Exception: # Python 2.7 # tk.Frame.__init__(self, master) super(App, self).__init__(master) self.master.title("Canvas zoom sample") # self.master.geometry("512x512") self.rootfrm = tk.Frame(self.master) self.rootfrm.pack(expand=1, fill=tk.BOTH) self.rootfrm.rowconfigure(1, weight=1) self.rootfrm.columnconfigure(0, weight=1) # create Button self.btnfrm = tk.Frame(self.rootfrm) self.btnfrm.grid(row=0, column=0, sticky=tk.W) b0 = tk.Button(self.btnfrm, text="Fit", command=lambda: self.set_zoom_fit()) b1 = tk.Button(self.btnfrm, text="-", command=lambda: self.dec_ratio()) b2 = tk.Button(self.btnfrm, text="1:1", command=lambda: self.set_zoom_full()) b3 = tk.Button(self.btnfrm, text="+", command=lambda: self.inc_ratio()) b0.pack(side=tk.LEFT, padx=4, ipadx=8) b1.pack(side=tk.LEFT, padx=4, ipadx=8) b2.pack(side=tk.LEFT, padx=4, ipadx=8) b3.pack(side=tk.LEFT, padx=4, ipadx=8) self.ratiostr = tk.StringVar() self.ratiostr.set("100%") self.ratiolbl = tk.Label(self.btnfrm, textvariable=self.ratiostr, relief=tk.GROOVE) self.ratiolbl.pack(side=tk.LEFT, padx=4, ipadx=8) # create Canvas self.cvsfrm = tk.Frame(self.rootfrm) self.cvsfrm.grid(row=1, column=0, sticky=tk.N + tk.S + tk.W + tk.E) self.cvsfrm.rowconfigure(0, weight=1) self.cvsfrm.columnconfigure(0, weight=1) self.canvas = tk.Canvas(self.cvsfrm, bg="#666666", width=512, height=256) self.canvas.grid(row=0, column=0, sticky=tk.N + tk.S + tk.W + tk.E) # create Scrollbar xbar = tk.Scrollbar(self.cvsfrm, orient=tk.HORIZONTAL, command=self.canvas.xview) ybar = tk.Scrollbar(self.cvsfrm, orient=tk.VERTICAL, command=self.canvas.yview) xbar.grid(row=1, column=0, sticky=tk.W + tk.E) ybar.grid(row=0, column=1, sticky=tk.N + tk.S) self.canvas.config(xscrollcommand=xbar.set) self.canvas.config(yscrollcommand=ybar.set) # set left button drag scroll self.canvas.bind("", lambda e: self.canvas.scan_mark(e.x, e.y)) self.canvas.bind("", lambda e: self.canvas.scan_dragto(e.x, e.y, gain=1)) # set middle button drag scroll self.canvas.bind("", lambda e: self.canvas.scan_mark(e.x, e.y)) self.canvas.bind("", lambda e: self.canvas.scan_dragto(e.x, e.y, gain=1)) # set mouse wheel event # .bind_all() ... Worked with Python 2.7 and Python 3.9 # .bind() ... It worked in Python 3.9, but not in Python 2.7. self.canvas.bind_all("", lambda e: self.event_wheel(e)) if platform.system() == "Linux": self.canvas.bind_all("", lambda e: self.dec_ratio()) self.canvas.bind_all("", lambda e: self.inc_ratio()) self.reset_ratio() self.zoomfit = False self.im = Image.open("tex_1024.png") self.set_image() def set_zoom_fit(self): self.zoomfit = True self.set_image() def set_zoom_full(self): self.zoomfit = False self.set_image() def reset_ratio(self): self.ratio = 100 def inc_ratio(self): # check 32bit, 64bit, Py27, Py3x is64bits = (sys.maxsize > 2**32) ispy3x = (sys.version_info.major >= 3) maxsize = 1024 * (8 if (is64bits and ispy3x) else 5) w, h = self.im.size maxratio = min([(maxsize * 100 / w), (maxsize * 100 / h)]) if self.ratio == maxratio: return self.ratio = self.ratio + (10 if self.ratio < 100 else 100) self.ratio = min([self.ratio, maxratio]) self.change_ratio() def dec_ratio(self): minratio = 10 if self.ratio == minratio: return self.ratio = self.ratio - (10 if self.ratio <= 100 else 100) self.ratio = max([self.ratio, minratio]) self.change_ratio() def event_wheel(self, e): if e.delta > 0: self.dec_ratio() elif e.delta < 0: self.inc_ratio() def change_ratio(self): w, h = self.im.size w = int(w * self.ratio / 100) h = int(h * self.ratio / 100) if w < 0 or h < 0: return self.canvas.delete("all") if self.ratio < 100: self.im_pad = self.im.resize((w, h), Image.LANCZOS) self.photo_image = ImageTk.PhotoImage(image=self.im_pad) elif self.ratio > 100: self.im_pad = self.im.resize((w, h), Image.NEAREST) self.photo_image = ImageTk.PhotoImage(image=self.im_pad) else: self.photo_image = ImageTk.PhotoImage(image=self.im) self.create_canvas_image() def set_image(self): self.update() cw = self.canvas.winfo_width() ch = self.canvas.winfo_height() self.canvas.delete("all") if self.zoomfit: # view fit self.im_pad = ImageOps.pad(self.im, (cw - 4, ch - 4), method=Image.LANCZOS) self.photo_image = ImageTk.PhotoImage(image=self.im_pad) ow, oh = self.im.size nw = self.photo_image.width() nh = self.photo_image.height() rw = float(nw) / float(ow) rh = float(nh) / float(oh) self.ratio = int(min([rw, rh]) * 100) else: # view 1:1 self.reset_ratio() self.photo_image = ImageTk.PhotoImage(image=self.im) self.create_canvas_image() def create_canvas_image(self): self.canvas.create_image(0, 0, image=self.photo_image, anchor=tk.NW) # set scroll region iw = self.photo_image.width() ih = self.photo_image.height() region = (0, 0, iw, ih) self.canvas.config(scrollregion=region) # update ratio label self.ratiostr.set("%d%%" % int(self.ratio)) def main(): root = tk.Tk() app = App(master=root) app.mainloop() if __name__ == '__main__': main()