前提・実現したいこと
canvas上に任意の画像を開く→
スタンプボタンをクリック→
別ウィンドウでスタンプを選択→
選択したスタンプをcanvas上に重ねて表示→
マウス操作で任意の場所に配置
というスタンプ機能を実装したいです。
該当のソースコード
python
1import os,sys 2import math 3from tkinter import * 4from tkinter import ttk 5from tkinter import filedialog 6from tkinter import messagebox 7from tkinter import simpledialog 8from PIL import Image, ImageTk, ImageOps 9import glob 10 11 12 13_maxline = 700 14 15class Application(ttk.Frame): 16 def __init__(self, master): 17 super().__init__(master) 18 self.master = master 19 self.master.geometry('620x600+280+240') 20 self.grid() 21 self.button_create() 22 23 def button_create(self): 24 self.photo_var = StringVar() 25 ttk.Entry(self.master, textvariable=self.photo_var, width=70).grid(row=0, column=0, pady=100, padx=20) 26 self.bln = BooleanVar() 27 self.bln.set(True) 28 ttk.Button(self.master, text='選択', width=8, command=self.file_select).grid(row=0, column=1, padx=(0, 20)) 29 ttk.Button(self.master, text='決定', width=8, command=self.image_open).grid(row=0, column=2, padx=(0, 20)) 30 31 def file_select(self): 32 iDir = os.getenv("HOMEDRIVE") + os.getenv("HOMEPATH") + "\Desktop" 33 fTyp = [('','*.jpg;*.jpeg;')] 34 filepath = filedialog.askopenfilename(filetypes = fTyp, initialdir = iDir) 35 self.photo_var.set(filepath) 36 37 def image_open(self): 38 self.image_dir = self.photo_var.get() 39 read_image = Image.open(self.image_dir) 40 self.xw, self.xh = self.image_resize(read_image) 41 self.x_image = read_image.resize((self.xw, self.xh)) 42 self.image_window() 43 44 def image_resize(self, image): 45 magnification = 1 46 if image.width > image.height: 47 magnification = _maxline / image.width 48 else: 49 magnification = _maxline / image.height 50 w = int(image.width * magnification) 51 h = int(image.height * magnification) 52 53 return w, h 54 55 def image_window(self): 56 self.photo_window = Toplevel(self.master) 57 self.photo_window.title(os.path.basename(self.image_dir) + ' size:%s*%s'% (self.xw, self.xh)) 58 self.photo_window.geometry('%sx%s+500+240' % (self.xw+1, self.xh+1)) 59 self.canvas = Canvas(self.photo_window, width=self.xw, height=self.xh) 60 self.canvas.grid() 61 self.canvas_create(self.x_image) 62 63 buttonlst = {'crop': [50, self.photo_window.destroy], 'mirror': [80, self.photo_window.destroy], 'Rrotate': [110, self.photo_window.destroy], 'Lrotate': [140, self.photo_window.destroy], 'stamp': [170, self.stamp]} 64 [ttk.Button(self.canvas, text=key, command=value[1]).place(x=self.xw-100, y=value[0]) for key, value in buttonlst.items()] 65 66 def canvas_create(self, img): 67 self.canvas.photo = ImageTk.PhotoImage(image=img) 68 self.canvas.create_image(0, 0, anchor='nw', image=self.canvas.photo) 69 70 def stamp(self): 71 stamplst = glob.glob('./スタンプ/*.png') 72 self.stamp_window = Toplevel(self.photo_window) 73 self.stamp_window.geometry('300x360+800+240') 74 self.cb = ttk.Combobox(self.stamp_window, state='readonly', width=25) 75 self.cb['value'] = stamplst 76 self.cb.pack(pady=10) 77 self.stamp_canvas = Canvas(self.stamp_window, width=250, height=250) 78 self.stamp_canvas.pack(pady=10) 79 80 ttk.Button(self.stamp_window, text='選択', command=self.stamp_selected).pack() 81 82 self.cb.bind('<<ComboboxSelected>>', self.stamp_previous) 83 84 def stamp_previous(self, event): 85 read_image = Image.open(self.cb.get()) 86 self.stamp_image = read_image.resize((250, 250)) 87 self.stamp_canvas.photo = ImageTk.PhotoImage(image=self.stamp_image) 88 self.stamp_canvas.create_image(0, 0, anchor='nw', image=self.stamp_canvas.photo) 89 90 def stamp_selected(self): 91 select_stamp = self.cb.get() 92 self.stamp_window.destroy() 93 stamp = Image.open(select_stamp) 94 stamp = stamp.resize((200, 200)) 95 self.canvas.photo = ImageTk.PhotoImage(image=stamp) 96 self.canvas.create_image(0, 0, anchor='nw', image=self.canvas.photo) 97 98 99 100def main(): 101 root = Tk() 102 app = Application(master = root) 103 app.mainloop() 104 105if __name__ == '__main__': 106 main()
サンプルデータ
補足情報(FW/ツールのバージョンなど)
python3.8.1
windows10
回答1件
あなたの回答
tips
プレビュー