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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tkinter as tk
|
2 |
+
from tkinter import filedialog, messagebox, simpledialog
|
3 |
+
from tkinterdnd2 import DND_FILES, TkinterDnD
|
4 |
+
from PIL import Image, ImageDraw, ImageGrab, ImageTk
|
5 |
+
import win32clipboard
|
6 |
+
import win32con
|
7 |
+
import io
|
8 |
+
import os
|
9 |
+
import winsound
|
10 |
+
from datetime import datetime
|
11 |
+
from colorsys import hsv_to_rgb
|
12 |
+
import colorsys
|
13 |
+
import random
|
14 |
+
import ctypes
|
15 |
+
import time
|
16 |
+
user32 = ctypes.windll.user32
|
17 |
+
|
18 |
+
class ImageDrawer(TkinterDnD.Tk):
|
19 |
+
def __init__(self):
|
20 |
+
super().__init__()
|
21 |
+
#クイックセーブのデフォルト保存フォルダを自分で指定したい方は次の行を書き換えてください。
|
22 |
+
self.default_save_folder = os.path.dirname(os.path.abspath(__file__)) + "/saved_image"
|
23 |
+
#画面の描画間隔: 50FPS => 0.02, 100FPS => 0.01, 125FPS => 0.008
|
24 |
+
self.refresh_time = 0.01
|
25 |
+
#補間の点の数: 数値を上げると線が途切れにくくなります
|
26 |
+
self.points = 64
|
27 |
+
|
28 |
+
|
29 |
+
self.hwnd = ctypes.windll.kernel32.GetConsoleWindow()
|
30 |
+
if self.hwnd:
|
31 |
+
ctypes.windll.user32.ShowWindow(self.hwnd, 0)
|
32 |
+
print("ターミナルを最小化します。")
|
33 |
+
|
34 |
+
window_width = 950
|
35 |
+
window_height = 996
|
36 |
+
self.img_width = self.img_height = self.resize_width = self.resize_height = 1024
|
37 |
+
self.geometry(f"{window_width}x{window_height}+0+0")
|
38 |
+
self.display_title()
|
39 |
+
self.current_size = (window_width, window_height)
|
40 |
+
|
41 |
+
# キャンバスとスクロールバーを含むフレーム
|
42 |
+
self.canvas_frame = tk.Frame(self)
|
43 |
+
self.canvas_frame.grid(row=0, column=0, padx=(10, 0),sticky="nsew")
|
44 |
+
# キャンバス
|
45 |
+
self.canvas = tk.Canvas(self.canvas_frame, bg="gray50", highlightthickness=0, bd=0)
|
46 |
+
self.canvas.grid(row=0, column=0, sticky="nsew")
|
47 |
+
self.canvas.bind("<B1-Motion>", self.paint) # 左ドラッグで描画
|
48 |
+
self.canvas.bind("<Button-1>", self.paint)
|
49 |
+
self.canvas.bind("<Button-2>", self.toggle_eraser)
|
50 |
+
self.canvas.bind("<Button-3>", self.Eyedropper) # 右クリックでスポイト
|
51 |
+
self.bind("<space>", self.Eyedropper)
|
52 |
+
# `canvas_frame` をウィンドウ全体に広げる
|
53 |
+
self.grid_rowconfigure(0, weight=1)
|
54 |
+
self.grid_columnconfigure(0, weight=1)
|
55 |
+
# `canvas_frame` 内の `canvas` も拡張
|
56 |
+
self.canvas_frame.grid_rowconfigure(0, weight=1)
|
57 |
+
self.canvas_frame.grid_columnconfigure(0, weight=1)
|
58 |
+
# スクロールバー (縦)
|
59 |
+
self.y_scrollbar = tk.Scrollbar(self.canvas_frame, orient=tk.VERTICAL, command=self.canvas.yview)
|
60 |
+
self.y_scrollbar.grid(row=0, column=1, sticky="ns") # 縦スクロールバーをキャンバスの右端に配置
|
61 |
+
# スクロールバー (横)
|
62 |
+
self.x_scrollbar = tk.Scrollbar(self, orient=tk.HORIZONTAL, command=self.canvas.xview)
|
63 |
+
self.x_scrollbar.grid(row=1, column=0, sticky="ew") # 横スクロールバーをキャンバス下部に配置
|
64 |
+
# スクロール設定
|
65 |
+
self.canvas.configure(xscrollcommand=self.x_scrollbar.set, yscrollcommand=self.y_scrollbar.set)
|
66 |
+
# DND 設定
|
67 |
+
self.drop_target_register(DND_FILES)
|
68 |
+
self.dnd_bind("<<Drop>>", self.on_drop)
|
69 |
+
# ボタンエリア
|
70 |
+
button_frame = tk.Frame(self, bg="gray66")
|
71 |
+
button_frame.grid(row=2, column=0, columnspan=2, sticky="ew")
|
72 |
+
# # ボタン
|
73 |
+
new_image = tk.Button(button_frame, text="新規", command=lambda: self.display_size_input_window())
|
74 |
+
new_image.pack(side=tk.LEFT, padx=5)
|
75 |
+
tk.Frame(button_frame).pack(side="left", expand=True)
|
76 |
+
open_button = tk.Button(button_frame, text="開く", command=self.open_image)
|
77 |
+
open_button.pack(side=tk.LEFT)
|
78 |
+
open_button.bind("<Button-3>", self.get_clipboard_image)
|
79 |
+
tk.Frame(button_frame).pack(side="left", expand=True)
|
80 |
+
save_image = tk.Button(button_frame, text="保存", command=lambda: self.save_image(None))
|
81 |
+
save_image.pack(side=tk.LEFT, padx=(5,0))
|
82 |
+
save_image.bind("<Button-3>", self.save_image)
|
83 |
+
tk.Frame(button_frame).pack(side="left", expand=True)
|
84 |
+
|
85 |
+
self.brush_size = 5
|
86 |
+
self.hue = 0
|
87 |
+
self.saturation = 0
|
88 |
+
self.brightness = 0
|
89 |
+
self.color = self.precolor = "#000000"
|
90 |
+
self.cursor = None
|
91 |
+
# 線の太さ表示
|
92 |
+
self.brush_size_label = tk.Label(button_frame, text=self.brush_size, font=("Arial Black", 20, "bold"), width=3, bg="gray66", justify="right", anchor="e")
|
93 |
+
self.brush_size_label.pack(side=tk.LEFT)
|
94 |
+
tk.Frame(button_frame).pack(side="left", expand=True)
|
95 |
+
|
96 |
+
self.brush_canvas = tk.Canvas(button_frame, bg="white", highlightthickness=0, bd=0, width=120, height=40)
|
97 |
+
self.brush_canvas.pack(side=tk.LEFT, pady=3)
|
98 |
+
self.brush_canvas.bind("<Button-1>", self.select_blush_size)
|
99 |
+
self.brush_canvas.bind("<B1-Motion>", self.select_blush_size)
|
100 |
+
self.brush_canvas.bind("<Button-3>", self.toggle_eraser)
|
101 |
+
tk.Frame(button_frame).pack(side="left", expand=True)
|
102 |
+
|
103 |
+
self.eraser_button = tk.Button(button_frame, text="消", command=lambda: self.toggle_eraser(), font=("メイリオ", 15, "bold"), bg="black", fg="white")
|
104 |
+
self.eraser_button.pack(side=tk.LEFT, padx=(0,10))
|
105 |
+
self.eraser_button.bind("<Button-3>", self.toggle_eraser)
|
106 |
+
self.bind("<x>", self.toggle_eraser)
|
107 |
+
tk.Frame(button_frame).pack(side="left", expand=True)
|
108 |
+
|
109 |
+
black_white_frame = tk.Frame(button_frame, bg="gray66")
|
110 |
+
black_white_frame.pack(side=tk.LEFT)
|
111 |
+
white_canvas = tk.Canvas(black_white_frame, highlightthickness=0, bd=0, width=20, height=20, bg="white")
|
112 |
+
white_canvas.grid(row=0, column=0, padx=5)
|
113 |
+
white_canvas.bind("<Button-1>", self.white)
|
114 |
+
self.bind("<w>", self.white)
|
115 |
+
black_canvas = tk.Canvas(black_white_frame, highlightthickness=0, bd=0, width=20, height=20, bg="black")
|
116 |
+
black_canvas.grid(row=1, column=0, padx=5)
|
117 |
+
black_canvas.bind("<Button-1>", self.black)
|
118 |
+
self.bind("<b>", self.black)
|
119 |
+
tk.Frame(button_frame).pack(side="left", expand=True)
|
120 |
+
|
121 |
+
self.grayscale_canvas = tk.Canvas(button_frame, highlightthickness=0, bg="black", bd=0, width=40, height=40)
|
122 |
+
self.grayscale_canvas.pack(side=tk.LEFT,pady=3)
|
123 |
+
self.grayscale_canvas.bind("<Button-1>", self.pick_grayscale)
|
124 |
+
self.grayscale_canvas.bind("<B1-Motion>", self.pick_grayscale)
|
125 |
+
tk.Frame(button_frame).pack(side="left", expand=True)
|
126 |
+
self.grayscale_canvas_init()
|
127 |
+
|
128 |
+
self.palette_canvas = tk.Canvas(button_frame, highlightthickness=0, bd=0, width=360, height=40)
|
129 |
+
self.palette_canvas.pack(side=tk.LEFT, padx=5, pady=3)
|
130 |
+
self.palette_canvas.bind("<Button-1>", self.pick_palette_color)
|
131 |
+
self.palette_canvas.bind("<B1-Motion>", self.pick_palette_color)
|
132 |
+
self.palette_canvas.bind("<Button-3>", self.pick_palette_color)
|
133 |
+
tk.Frame(button_frame).pack(side="left", expand=True)
|
134 |
+
|
135 |
+
self.nav_canvas = tk.Canvas(button_frame, bg="navy", highlightthickness=0, bd=0, width=40, height=40)
|
136 |
+
self.nav_canvas.pack(side=tk.LEFT, pady=3)
|
137 |
+
self.nav_canvas.bind("<Button-1>", self.on_drag)
|
138 |
+
self.nav_canvas.bind("<B1-Motion>", self.on_drag)
|
139 |
+
self.nav_canvas.bind("<Button-3>", self.toggle_resize_image)
|
140 |
+
tk.Frame(button_frame).pack(side="left", expand=True)
|
141 |
+
|
142 |
+
layer_frame = tk.Frame(button_frame, bg="gray66")
|
143 |
+
layer_frame.pack(side=tk.RIGHT, padx=5)
|
144 |
+
self.label_text = ["1", "2", "3", "背"]
|
145 |
+
self.reverse_number = [3, 2, 1, 0]
|
146 |
+
self.layer_label = [] # 空のリストを作成
|
147 |
+
for i in range(4):
|
148 |
+
label = tk.Label(layer_frame, text=self.label_text[i], fg="white", bg="green") # ラベル作成
|
149 |
+
label.grid(row=0, column=i) # grid で配置
|
150 |
+
self.layer_label.append(label) # リストに追加
|
151 |
+
label.bind("<Button-1>", lambda event, idx=i: self.select_drawing_layer(event, idx)) # インデックスを渡す
|
152 |
+
label.bind("<Button-2>", lambda event, idx=i: self.unite_layer(event, idx))
|
153 |
+
label.bind("<Button-3>", lambda event, idx=i: self.toggle_layer(event, idx))
|
154 |
+
self.layer_status = [True, True, True, True]
|
155 |
+
clipboard_button = tk.Button(layer_frame, text="Clipboard",command= lambda: self.copy_to_clipboard(None))
|
156 |
+
clipboard_button.grid(row=1, column=0, columnspan=4)
|
157 |
+
clipboard_button.bind("<Button-2>", self.copy_to_clipboard)
|
158 |
+
clipboard_button.bind("<Button-3>", self.copy_to_clipboard)
|
159 |
+
|
160 |
+
self.bind("<MouseWheel>", self.change_brush_size) # ホイールで太さ変更
|
161 |
+
self.bind("<a>", self.change_brush_size)
|
162 |
+
self.bind("<z>", self.change_brush_size)
|
163 |
+
self.bind("<ButtonRelease>", self.on_release)
|
164 |
+
self.bind("<Configure>", lambda event: self.play_window_resize_event(event))
|
165 |
+
self.protocol("WM_DELETE_WINDOW", self.on_close)
|
166 |
+
self.image_path = None
|
167 |
+
self.eraser_mode = False # 消しゴムモードの初期状態
|
168 |
+
self.click_off = True
|
169 |
+
self.layer_mode = 0
|
170 |
+
self.last_paint_time = 0 # 最後に描画した時間
|
171 |
+
self.last_x, self.last_y = 0, 0
|
172 |
+
self.cursor = None
|
173 |
+
self.sizes = [(640, 1536), (768, 1344), (832, 1216), (896, 1152), (1024, 1024), (1152, 896), (1216, 832), (1344, 768), (1536, 640)]
|
174 |
+
self.display_brush_canvas()
|
175 |
+
self.draw_palette_canvas()
|
176 |
+
self.init_palette_line()
|
177 |
+
self.change_bgcolor_grayscale_canvas()
|
178 |
+
self.view_rect = self.nav_canvas.create_rectangle(0, 0, 10, 10, outline="yellow", width=2)
|
179 |
+
self.images = []
|
180 |
+
for i in range(4):
|
181 |
+
self.images.append(Image.new("RGBA", (self.img_width, self.img_height), (0, 0, 0, 0)))
|
182 |
+
self.images[0] = Image.new("RGBA", (self.img_width, self.img_height), "#ffffff")
|
183 |
+
self.update_idletasks()
|
184 |
+
self.resize_mode = True
|
185 |
+
self.reset_images()
|
186 |
+
self.update_view_rect()
|
187 |
+
winsound.PlaySound("C:/Windows/Media/Alarm02.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
188 |
+
|
189 |
+
def reset_images(self):
|
190 |
+
for i in range(4):
|
191 |
+
if not i == 0:
|
192 |
+
self.images[i] = (Image.new("RGBA", (self.img_width, self.img_height), (0, 0, 0, 0)))
|
193 |
+
self.layer_status[i] = True
|
194 |
+
self.layer_label[i].config(bg="green")
|
195 |
+
self.layer_label[i].config(text= self.label_text[i], fg= "white")
|
196 |
+
self.select_drawing_layer(None, 0)
|
197 |
+
|
198 |
+
def display_title(self):
|
199 |
+
self.title(f"I2I Paint 4Layer 【{self.img_width} x {self.img_height}】")
|
200 |
+
|
201 |
+
def on_release(self, event):
|
202 |
+
self.click_off = True
|
203 |
+
|
204 |
+
def on_close(self):
|
205 |
+
result = messagebox.askyesno("確認", "終了してウインドウを閉じますか?", parent=self)
|
206 |
+
if result:
|
207 |
+
self.destroy()
|
208 |
+
|
209 |
+
def play_window_resize_event(self, event):
|
210 |
+
if hasattr(self, 'resize_timer') and self.resize_timer:
|
211 |
+
self.after_cancel(self.resize_timer)
|
212 |
+
self.resize_timer = self.after(500, self.check_if_resized)
|
213 |
+
|
214 |
+
def check_if_resized(self):
|
215 |
+
new_size = (self.winfo_width(), self.winfo_height())
|
216 |
+
if new_size != self.current_size: # サイズが変わっている場合
|
217 |
+
self.current_size = new_size
|
218 |
+
self.display_image() # サイズが確定したときに一度だけ呼び出す
|
219 |
+
|
220 |
+
def display_size_input_window(self):
|
221 |
+
self.size_input_window = tk.Toplevel(self)
|
222 |
+
self.size_input_window.title("画像サイズ選択")
|
223 |
+
tk.Label(self.size_input_window, text="画像サイズを選択してください").pack()
|
224 |
+
size_var = tk.IntVar(value=4) # デフォルトを中央の 1024x1024 に設定
|
225 |
+
self.radio_buttons = []
|
226 |
+
for index, size in enumerate(self.sizes):
|
227 |
+
rb = tk.Radiobutton(self.size_input_window, text=f"{size[0]} x {size[1]}", variable=size_var, value=index)
|
228 |
+
rb.pack(anchor="w")
|
229 |
+
self.radio_buttons.append(rb)
|
230 |
+
rb = tk.Radiobutton(self.size_input_window, text="キーボードから入力", variable=size_var, value=9)
|
231 |
+
rb.pack(anchor="w")
|
232 |
+
self.radio_buttons.append(rb)
|
233 |
+
tk.Button(self.size_input_window, text="決定", command=lambda: self.new_image(size_var.get())).pack()
|
234 |
+
parent_width = self.winfo_width()
|
235 |
+
parent_height = self.winfo_height()
|
236 |
+
parent_x = self.winfo_x()
|
237 |
+
parent_y = self.winfo_y()
|
238 |
+
self.size_input_window.update_idletasks()
|
239 |
+
# 子ウインドウのサイズを設定
|
240 |
+
child_width = self.size_input_window.winfo_width()
|
241 |
+
child_height = self.size_input_window.winfo_height()
|
242 |
+
# 子ウインドウの位置を親ウインドウの中心に設定
|
243 |
+
child_x = parent_x + (parent_width // 2) - (child_width // 2)
|
244 |
+
child_y = parent_y + (parent_height // 2) - (child_height // 2)
|
245 |
+
self.size_input_window.geometry(f"{child_width}x{child_height}+{child_x}+{child_y}")
|
246 |
+
|
247 |
+
def new_image(self, selected_size):
|
248 |
+
if hasattr(self, 'size_input_window') and self.size_input_window:
|
249 |
+
self.size_input_window.destroy()
|
250 |
+
if selected_size ==9:
|
251 |
+
while True:
|
252 |
+
input_str = simpledialog.askstring("新規画像サイズ", "幅と高さをスペースで区切って入力してください/n例: 「800 600」 (最大値は4096)")
|
253 |
+
if not input_str:
|
254 |
+
return
|
255 |
+
try:
|
256 |
+
width, height = map(int, input_str.split())
|
257 |
+
if 4096 > width > 0 and 4096 > height > 0:
|
258 |
+
self.img_width = width
|
259 |
+
self.img_height = height
|
260 |
+
break
|
261 |
+
except ValueError:
|
262 |
+
pass # 数値以外が入力された場合はやり直し
|
263 |
+
else:
|
264 |
+
self.img_width = self.resize_width = self.sizes[selected_size][0]
|
265 |
+
self.img_height = self.resize_height = self.sizes[selected_size][1]
|
266 |
+
self.canvas.config(scrollregion=(0, 0, self.img_width, self.img_height))
|
267 |
+
result = messagebox.askyesnocancel("背景色", f"新規画像:{self.img_width}x{self.img_height}/n新規画像を現在の色で塗りつぶしますか?\n(「いいえ」=白背景)", parent=self)
|
268 |
+
if result is None:
|
269 |
+
return
|
270 |
+
elif result:
|
271 |
+
background_color = self.color
|
272 |
+
else:
|
273 |
+
background_color = "#ffffff"
|
274 |
+
# 新しい画像を作成(RGBAモードで、背景色は透明)
|
275 |
+
self.images[0] = Image.new("RGBA", (self.img_width, self.img_height), background_color)
|
276 |
+
# 塗りつぶし処理
|
277 |
+
draw = ImageDraw.Draw(self.images[0])
|
278 |
+
# 現在の色で全体を塗りつぶす
|
279 |
+
draw.rectangle([0, 0, self.img_width, self.img_height], fill=background_color)
|
280 |
+
self.reset_images()
|
281 |
+
# 必要に応じて描画後の画像をキャンバスに再表示する処理を追加
|
282 |
+
self.resize_mode = True
|
283 |
+
self.display_image()
|
284 |
+
self.display_title()
|
285 |
+
winsound.PlaySound("C:/Windows/Media/chord.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
286 |
+
self.on_drag(None)
|
287 |
+
result = messagebox.askyesno("ノイズ", f"新規画像:{self.img_width}x{self.img_height}/n背景画面にノイズを載せますか?", parent=self)
|
288 |
+
if result:
|
289 |
+
noise_level = simpledialog.askinteger("ノイズ濃度", "ノイズの濃さを入力してください。(%)", minvalue=0, maxvalue=100, initialvalue=20, parent=self)
|
290 |
+
if noise_level:
|
291 |
+
self.images[0] = self.add_noise_to_image(self.images[0], noise_level)
|
292 |
+
self.display_image()
|
293 |
+
|
294 |
+
def add_noise_to_image(self, image, noise_level):
|
295 |
+
# 画像をRGBAモードで開く(透明度も考慮)
|
296 |
+
img = image.convert("RGBA")
|
297 |
+
pixels = img.load() # ピクセルデータを取得
|
298 |
+
width, height = img.size
|
299 |
+
for y in range(height):
|
300 |
+
for x in range(width):
|
301 |
+
# ノイズの強さに応じてピクセルのRGB値をランダムに変化させる
|
302 |
+
if random.random() < (noise_level / 100.0): # noise_level%の確率でノイズ
|
303 |
+
r = random.randint(0, 255) # ランダムな赤の値
|
304 |
+
g = random.randint(0, 255) # ランダムな緑の値
|
305 |
+
b = random.randint(0, 255) # ランダムな青の値
|
306 |
+
a = pixels[x, y][3] # 元の透明度(アルファ値)を保持
|
307 |
+
pixels[x, y] = (r, g, b, a) # ノイズを加えた新しい色を設定
|
308 |
+
return img
|
309 |
+
|
310 |
+
def on_drop(self, event):
|
311 |
+
"""ドラッグアンドドロップで画像を開く"""
|
312 |
+
self.image_path = event.data.strip("{}") # ファイルパス取得
|
313 |
+
self.load_image(self.image_path)
|
314 |
+
|
315 |
+
def open_image(self):
|
316 |
+
"""ダイアログから画像を開く"""
|
317 |
+
file_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.webp;*.gif;*.bmp")])
|
318 |
+
if file_path:
|
319 |
+
self.load_image(file_path)
|
320 |
+
|
321 |
+
def load_image(self, file_path):
|
322 |
+
self.image_path = file_path
|
323 |
+
try:
|
324 |
+
"""画像を読み込んでキャンバスに表示"""
|
325 |
+
self.images[0] = Image.open(file_path).convert("RGBA")
|
326 |
+
self.img_width, self.img_height = self.images[0].size
|
327 |
+
self.reset_images()
|
328 |
+
self.resize_mode = True
|
329 |
+
self.display_image()
|
330 |
+
self.display_title()
|
331 |
+
winsound.PlaySound("C:/Windows/Media/chimes.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
332 |
+
self.on_drag(None)
|
333 |
+
except Exception as e:
|
334 |
+
print(f"ファイルの読み込みに失敗しました: {e}")
|
335 |
+
|
336 |
+
def save_image(self, event=None):
|
337 |
+
"""タイムスタンプ付きで画像をファイルに保存"""
|
338 |
+
image_to_save = self.displayed_image
|
339 |
+
if event: # `event` が渡された場合(ショートカット保存)
|
340 |
+
folder_path = self.default_save_folder
|
341 |
+
try:
|
342 |
+
if not os.path.exists(folder_path):
|
343 |
+
os.mkdir(folder_path)
|
344 |
+
# タイムスタンプ付きのファイル名を作成
|
345 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
346 |
+
file_name = f"image_{timestamp}"
|
347 |
+
# 重複回避処理(既存ファイルがある場合は "+" を追加)
|
348 |
+
file_path = os.path.join(folder_path, file_name + ".png").replace('\\', '/')
|
349 |
+
while os.path.exists(file_path):
|
350 |
+
file_name += "+"
|
351 |
+
file_path = os.path.join(folder_path, file_name + ".png").replace('\\', '/')
|
352 |
+
# 画像を保存
|
353 |
+
image_to_save.save(file_path, "PNG")
|
354 |
+
print(f"画像を保存しました: {file_path}")
|
355 |
+
messagebox.showinfo("確認", f"画像を保存しました:\n{file_path}", parent=self)
|
356 |
+
except Exception as e:
|
357 |
+
print(f"保存エラー: {e}\n{folder_path}")
|
358 |
+
else: # `event` がない場合(手動保存)
|
359 |
+
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
360 |
+
file_path = filedialog.asksaveasfilename(
|
361 |
+
defaultextension=".png",
|
362 |
+
filetypes=[("PNG 画像", "*.png"), ("JPEG 画像", "*.jpg"), ("BMP 画像", "*.bmp"), ("すべてのファイル", "*.*")],
|
363 |
+
initialfile=f"image_{timestamp}" # ここでデフォルト名を設定
|
364 |
+
)
|
365 |
+
if not file_path: # キャンセルされた場合は処理���中断
|
366 |
+
return
|
367 |
+
# 画像を保存
|
368 |
+
try:
|
369 |
+
image_to_save.save(file_path)
|
370 |
+
messagebox.showinfo("確認", f"画像を保存しました:/n{file_path}", parent=self)
|
371 |
+
except Exception as e:
|
372 |
+
print(f"保存エラー: {e}\n{file_path}")
|
373 |
+
|
374 |
+
def toggle_resize_image(self, event=None):
|
375 |
+
self.resize_mode = not self.resize_mode
|
376 |
+
self.display_image()
|
377 |
+
|
378 |
+
def on_drag(self, event):
|
379 |
+
if self.resize_mode:
|
380 |
+
winsound.PlaySound("C:/Windows/Media/Windows Exclamation.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
381 |
+
return
|
382 |
+
""" `nav_canvas` 上で矩形をドラッグして `canvas` をスクロール """
|
383 |
+
scale_x = self.img_width / 39
|
384 |
+
scale_y = self.img_height / 39
|
385 |
+
# `nav_canvas` の範囲内に収める
|
386 |
+
if event:
|
387 |
+
x = min(max(event.x, 0), 39)
|
388 |
+
y = min(max(event.y, 0), 39)
|
389 |
+
else:
|
390 |
+
x = y = 0
|
391 |
+
scroll_x = (x * scale_x) / self.img_width
|
392 |
+
scroll_y = (y * scale_y) / self.img_height
|
393 |
+
self.canvas.xview_moveto(scroll_x)
|
394 |
+
self.canvas.yview_moveto(scroll_y)
|
395 |
+
# `view_rect` を更新
|
396 |
+
self.update_view_rect()
|
397 |
+
|
398 |
+
def update_view_rect(self):
|
399 |
+
self.canvas.update()
|
400 |
+
""" `canvas` の表示範囲を `nav_canvas` に反映する """
|
401 |
+
canvas_x1 = self.canvas.xview()[0] * self.img_width
|
402 |
+
canvas_y1 = self.canvas.yview()[0] * self.img_height
|
403 |
+
canvas_x2 = self.canvas.xview()[1] * self.img_width
|
404 |
+
canvas_y2 = self.canvas.yview()[1] * self.img_height
|
405 |
+
# 縮小率を計算
|
406 |
+
scale_x = 40 / self.img_width
|
407 |
+
scale_y = 40 / self.img_height
|
408 |
+
# nav_canvas 上の矩形の位置を計算
|
409 |
+
x1 = canvas_x1 * scale_x
|
410 |
+
y1 = canvas_y1 * scale_y
|
411 |
+
x2 = canvas_x2 * scale_x
|
412 |
+
y2 = canvas_y2 * scale_y
|
413 |
+
# nav_canvas 上のビュー範囲を更新
|
414 |
+
self.nav_canvas.coords(self.view_rect, x1, y1, x2, y2)
|
415 |
+
|
416 |
+
def select_drawing_layer(self, event, idx):
|
417 |
+
index = self.reverse_number[idx]
|
418 |
+
for i in range(4):
|
419 |
+
self.layer_label[i].config(text= self.label_text[i], fg= "white")
|
420 |
+
self.drawing_layer = index
|
421 |
+
self.layer_label[idx].config(text= "●", fg= "orange")
|
422 |
+
self.layer_status[index] = True
|
423 |
+
self.layer_label[idx].config(bg="green")
|
424 |
+
winsound.PlaySound("C:\Windows\Media\Windows Notify Calendar.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
425 |
+
self.display_image()
|
426 |
+
|
427 |
+
def toggle_layer(self, event, idx):
|
428 |
+
index = self.reverse_number[idx]
|
429 |
+
self.layer_status[index] = not self.layer_status[index]
|
430 |
+
label_color = ["black", "green"]
|
431 |
+
self.layer_label[idx].config(bg=label_color[self.layer_status[index]])
|
432 |
+
winsound.PlaySound("C:/Windows/Media/chord.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
433 |
+
self.display_image()
|
434 |
+
|
435 |
+
def unite_layer(self, event, idx):
|
436 |
+
index = self.reverse_number[idx]
|
437 |
+
if index == 0:
|
438 |
+
return
|
439 |
+
result = messagebox.askyesno("レイヤー統合", f"レイヤー {idx + 1} を背景レイヤーに統合します。\nよろしいですか?", parent=self)
|
440 |
+
if result:
|
441 |
+
self.images[0] = Image.alpha_composite(self.images[0], self.images[index])
|
442 |
+
self.images[index] = Image.new("RGBA", (self.img_width, self.img_height), (0, 0, 0, 0))
|
443 |
+
self.display_image()
|
444 |
+
|
445 |
+
def calculate_basic_value(self):
|
446 |
+
self.canvas.update()
|
447 |
+
self.frm_width = self.canvas.winfo_width()
|
448 |
+
self.frm_height = self.canvas.winfo_height()
|
449 |
+
self.adjust_x = self.adjust_y = 0
|
450 |
+
if self.img_width <= self.frm_width:
|
451 |
+
self.adjust_x = int((self.frm_width - self.img_width) / 2)
|
452 |
+
if self.img_height <= self.frm_height:
|
453 |
+
self.adjust_y = int((self.frm_height - self.img_height) / 2)
|
454 |
+
self.img_aspect_ratio = self.img_width / max(self.img_height, 1)
|
455 |
+
self.frame_aspect_ratio = self.frm_width / max(self.frm_height, 1)
|
456 |
+
if self.img_aspect_ratio > self.frame_aspect_ratio:
|
457 |
+
self.resize_width = self.frm_width
|
458 |
+
self.resize_height = int(self.frm_width / self.img_aspect_ratio)
|
459 |
+
self.dx, self.dy = 0, (self.frm_height - self.resize_height) / 2
|
460 |
+
else:
|
461 |
+
self.resize_height = self.frm_height
|
462 |
+
self.resize_width = int(self.frm_height * self.img_aspect_ratio)
|
463 |
+
self.dx, self.dy = (self.frm_width - self.resize_width) / 2, 0
|
464 |
+
self.resize_width = max(self.resize_width, 1)
|
465 |
+
self.resize_height = max(self.resize_height, 1)
|
466 |
+
self.ratio = self.img_width / max(self.resize_width, 1)
|
467 |
+
self.xplot = self.frm_width // 2
|
468 |
+
self.yplot = self.frm_height // 2
|
469 |
+
|
470 |
+
def overlap_layer(self):
|
471 |
+
self.displayed_image = Image.new("RGBA", (self.img_width, self.img_height), (0, 0, 0, 0))
|
472 |
+
for i in range(4):
|
473 |
+
if self.layer_status[i]: # レイヤーが有効なら合成
|
474 |
+
self.displayed_image = Image.alpha_composite(self.displayed_image, self.images[i])
|
475 |
+
|
476 |
+
def display_image(self):
|
477 |
+
self.calculate_basic_value()
|
478 |
+
self.overlap_layer()
|
479 |
+
self.canvas.delete("all")
|
480 |
+
if self.resize_mode:
|
481 |
+
resized_image = self.displayed_image.resize((self.resize_width, self.resize_height), Image.LANCZOS)
|
482 |
+
self.photo_image = ImageTk.PhotoImage(resized_image)
|
483 |
+
self.canvas.create_image(self.xplot, self.yplot, anchor=tk.CENTER, image=self.photo_image)
|
484 |
+
self.canvas.image = self.photo_image # GC対策
|
485 |
+
self.canvas.config(scrollregion=(0, 0, self.frm_width, self.frm_height))
|
486 |
+
self.nav_canvas.config(bg="yellow")
|
487 |
+
else:
|
488 |
+
self.photo_image = ImageTk.PhotoImage(self.displayed_image)
|
489 |
+
self.canvas.create_image(self.adjust_x, self.adjust_y, anchor=tk.NW, image=self.photo_image)
|
490 |
+
self.canvas.image = self.photo_image # GC対策
|
491 |
+
self.canvas.config(scrollregion=(0, 0, self.img_width, self.img_height))
|
492 |
+
self.nav_canvas.config(bg="navy")
|
493 |
+
self.on_drag(None)
|
494 |
+
|
495 |
+
def display_brush_canvas(self):
|
496 |
+
if self.saturation < 0.1 and self.brightness > 0.9:
|
497 |
+
outcolor = "black"
|
498 |
+
else:
|
499 |
+
outcolor = self.color
|
500 |
+
self.brush_canvas.delete("all") # 以前の画像をクリア
|
501 |
+
x1, y1 = (60 - self.brush_size), (20 - self.brush_size)
|
502 |
+
x2, y2 = (60 + self.brush_size), (20 + self.brush_size)
|
503 |
+
self.brush_canvas.create_oval(x1, y1, x2, y2, fill=self.color, outline=outcolor)
|
504 |
+
self.brush_size_label.config(fg=self.color)
|
505 |
+
|
506 |
+
def display_grayscale_cursor(self):
|
507 |
+
self.grayscale_canvas.delete(self.cursor)
|
508 |
+
true_hue = self.hue * 360 # 0.0~1.0 を 0~360 に変換
|
509 |
+
inverse_hue = int(true_hue + 180) % 360 # 逆の色相を求める
|
510 |
+
r, g, b = hsv_to_rgb(inverse_hue / 360, 1, 1)
|
511 |
+
fill_color = f"#{int(r*255):02x}{int(g*255):02x}{int(b*255):02x}"
|
512 |
+
self.cursor = self.grayscale_canvas.create_text(self.saturation * 39, 39 - (self.brightness) * 39, text="◎", fill=fill_color)
|
513 |
+
self.display_brush_canvas()
|
514 |
+
|
515 |
+
def grayscale_canvas_init(self):
|
516 |
+
""" HUEの背景とアルファグラデーションを適用 """
|
517 |
+
# RGBA画像を作成(背景は完全に透明)
|
518 |
+
white = Image.new("RGBA", (40, 40), (0, 0, 0, 0))
|
519 |
+
draw_white = ImageDraw.Draw(white)
|
520 |
+
# アルファ値のグラデーション(左: 透明 → 右: 不透明)
|
521 |
+
for x in range(40):
|
522 |
+
alpha = int((1 - (x / 39)) * 255) # 0 (透明) ~ 255 (不透明)
|
523 |
+
for y in range(40):
|
524 |
+
draw_white.point((x, y), fill=(255, 255, 255, alpha))
|
525 |
+
# 黒のアルファグラデーション(上: 透明 → 下: 不透明)
|
526 |
+
black = Image.new("RGBA", (40, 40), (0, 0, 0, 0))
|
527 |
+
draw_black = ImageDraw.Draw(black)
|
528 |
+
for y in range(40):
|
529 |
+
alpha = int(y / 39 * 255) # 0 (透明) ~ 255 (不透明)
|
530 |
+
for x in range(40):
|
531 |
+
draw_black.point((x, y), fill=(0, 0, 0, alpha))
|
532 |
+
# 画像の合成(Imageオブジェクトを使用)
|
533 |
+
self.filter_image = Image.alpha_composite(white, black)
|
534 |
+
self.filter_tk = ImageTk.PhotoImage(self.filter_image)
|
535 |
+
# Canvasに適用
|
536 |
+
self.grayscale_canvas.create_image(0, 0, anchor=tk.NW, image=self.filter_tk)
|
537 |
+
|
538 |
+
def init_palette_line(self):
|
539 |
+
""" パレット上のHUEのラインを初期化 """
|
540 |
+
white_line = Image.new("RGBA", (1, 40), (0, 0, 0, 0))
|
541 |
+
draw_line = ImageDraw.Draw(white_line)
|
542 |
+
draw_line.line((0, 0, 0, 39), fill=(0, 0, 0, 255), width=1)
|
543 |
+
line_tk = ImageTk.PhotoImage(white_line)
|
544 |
+
self.hue_line_img = line_tk # GC対策
|
545 |
+
self.hue_line = self.palette_canvas.create_image(0, 0, anchor=tk.NW, image=self.hue_line_img)
|
546 |
+
|
547 |
+
def change_bgcolor_grayscale_canvas(self):
|
548 |
+
""" 背景色をHUEで変更し、グラデーションと合成 """
|
549 |
+
r, g, b = hsv_to_rgb(self.hue, 1, 1) # HUEをRGBに変換
|
550 |
+
color = f"#{int(r*255):02x}{int(g*255):02x}{int(b*255):02x}"
|
551 |
+
self.grayscale_canvas.config(bg=color)
|
552 |
+
self.display_grayscale_cursor()
|
553 |
+
hue_x = int(self.hue * 359) # HUEを0-39の範囲でスケール変換
|
554 |
+
self.palette_canvas.coords(self.hue_line, hue_x, 0)
|
555 |
+
|
556 |
+
def draw_palette_canvas(self):
|
557 |
+
"""HSVの色相 (H) と彩度 (S) を変化させたグラデーションを描画"""
|
558 |
+
for x in range(360): # x = 0 から x = 358 まで
|
559 |
+
hue = x / 359 # 色相を 0.0 ~ 1.0 に正規化
|
560 |
+
for y in range(40):
|
561 |
+
if y < 20:
|
562 |
+
saturation = y / 19 # 彩度 (0.0 ~ 1.0)
|
563 |
+
brightness = 1
|
564 |
+
else:
|
565 |
+
saturation = 1
|
566 |
+
brightness = 1 - 0.8 * ((y-20) / 19) # 明度 (0.0 ~ 1.0)
|
567 |
+
r, g, b = hsv_to_rgb(hue, saturation, brightness) # 明度は常に1.0(最大)
|
568 |
+
color = f"#{int(r*255):02x}{int(g*255):02x}{int(b*255):02x}"
|
569 |
+
self.palette_canvas.create_rectangle(x, y, x, y, outline=color, fill=color)
|
570 |
+
|
571 |
+
def hsv_to_hex_color(self, hue, saturation, brightness):
|
572 |
+
r, g, b = hsv_to_rgb(hue, saturation, brightness) # 明度は常に1.0(最大)
|
573 |
+
self.color = f"#{int(r*255):02x}{int(g*255):02x}{int(b*255):02x}"
|
574 |
+
|
575 |
+
def black(self, event):
|
576 |
+
self.brightness = 0
|
577 |
+
self.hsv_to_hex_color(self.hue, self.saturation, self.brightness)
|
578 |
+
self.black_and_white()
|
579 |
+
|
580 |
+
def white(self, event):
|
581 |
+
self.saturation = 0
|
582 |
+
self.brightness = 1
|
583 |
+
self.hsv_to_hex_color(self.hue, self.saturation, self.brightness)
|
584 |
+
self.black_and_white()
|
585 |
+
|
586 |
+
def black_and_white(self):
|
587 |
+
winsound.PlaySound("C:/Windows/Media/chord.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
588 |
+
self.display_grayscale_cursor()
|
589 |
+
if self.eraser_mode:
|
590 |
+
self.eraser_mode = False
|
591 |
+
self.eraser_button.config(bg="black")
|
592 |
+
self.brush_canvas.config(bg="white")
|
593 |
+
|
594 |
+
def Eyedropper(self, event):
|
595 |
+
"""右クリックでスポイト (カーソル下の色を取得)"""
|
596 |
+
x, y = self.winfo_rootx() + event.x, self.winfo_rooty() + event.y
|
597 |
+
img = ImageGrab.grab(bbox=(x, y, x + 1, y + 1))
|
598 |
+
color_rgb = img.getpixel((0, 0))
|
599 |
+
# RGBを0-1の範囲に変換
|
600 |
+
r, g, b = [x / 255.0 for x in color_rgb]
|
601 |
+
# RGBをHSVに変換
|
602 |
+
self.hue, self.saturation, self.brightness = colorsys.rgb_to_hsv(r, g, b)
|
603 |
+
self.hsv_to_hex_color(self.hue, self.saturation, self.brightness)
|
604 |
+
self.change_bgcolor_grayscale_canvas()
|
605 |
+
winsound.PlaySound("C:/Windows/Media/chord.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
606 |
+
if self.eraser_mode:
|
607 |
+
self.eraser_mode = False
|
608 |
+
self.eraser_button.config(bg="black")
|
609 |
+
self.brush_canvas.config(bg="white")
|
610 |
+
|
611 |
+
def pick_grayscale(self, event):
|
612 |
+
"""クリックした位置の色を取得し、ラベルに表示"""
|
613 |
+
x, y = event.x, event.y
|
614 |
+
self.saturation = min(max(x / 39, 0), 1)
|
615 |
+
self.brightness = min(max(1 - (y / 39), 0), 1)
|
616 |
+
self.hsv_to_hex_color(self.hue, self.saturation, self.brightness)
|
617 |
+
self.display_grayscale_cursor()
|
618 |
+
winsound.PlaySound("C:/Windows/Media/Windows Information Bar.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
619 |
+
if self.eraser_mode:
|
620 |
+
self.eraser_mode = False
|
621 |
+
self.eraser_button.config(bg="black")
|
622 |
+
self.brush_canvas.config(bg="white")
|
623 |
+
|
624 |
+
def pick_palette_color(self, event):
|
625 |
+
"""クリックした位置の色を取得し、ラベルに表示"""
|
626 |
+
x, y = event.x, event.y
|
627 |
+
self.hue = min(max(x / 359, 0), 1) # 色相 (0.0 ~ 1.0)
|
628 |
+
if event.num == 1:
|
629 |
+
if y < 20:
|
630 |
+
self.saturation = min(max(y / 19, 0), 1) # 彩度 (0.0 ~ 1.0)
|
631 |
+
self.brightness = 1
|
632 |
+
else:
|
633 |
+
y -= 20
|
634 |
+
self.saturation = 1
|
635 |
+
self.brightness = min(max(1 - (0.8 * y / 19), 0), 1) # 明度 (0.0 ~ 1.0)
|
636 |
+
else:
|
637 |
+
self.saturation = 1.0
|
638 |
+
self.brightness = 1.0
|
639 |
+
self.hsv_to_hex_color(self.hue, self.saturation, self.brightness)
|
640 |
+
self.change_bgcolor_grayscale_canvas()
|
641 |
+
winsound.PlaySound("C:/Windows/Media/Windows Information Bar.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
642 |
+
if self.eraser_mode:
|
643 |
+
self.eraser_mode = False
|
644 |
+
self.eraser_button.config(bg="black")
|
645 |
+
self.brush_canvas.config(bg="white")
|
646 |
+
|
647 |
+
def adjust_color_brightness(self, event):
|
648 |
+
"""マウスホイールで self.color の明度 (V) を調整"""
|
649 |
+
if self.eraser_mode:
|
650 |
+
return
|
651 |
+
old_color = self.color
|
652 |
+
delta = 0.05 # 1回のスクロールで増減する値
|
653 |
+
if event.delta > 0:
|
654 |
+
self.brightness = min(self.brightness + delta, 1)
|
655 |
+
else:
|
656 |
+
self.brightness = max(self.brightness - delta, 0)
|
657 |
+
self.hsv_to_hex_color(self.hue, self.saturation, self.brightness)
|
658 |
+
if not self.color == old_color:
|
659 |
+
self.display_grayscale_cursor()
|
660 |
+
winsound.PlaySound("C:/Windows/Media/Windows Information Bar.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
661 |
+
|
662 |
+
def change_brush_size(self, event):
|
663 |
+
widget = self.winfo_containing(event.x_root, event.y_root)
|
664 |
+
if widget == self.canvas:
|
665 |
+
if event.keysym == "z":
|
666 |
+
self.brush_size = min(max(1, self.brush_size - 10), 512)
|
667 |
+
elif event.keysym == "a":
|
668 |
+
self.brush_size = min(max(1, self.brush_size + 10), 512)
|
669 |
+
else:
|
670 |
+
self.brush_size = min(max(1, self.brush_size + (1 if event.delta > 0 else -1)), 512)
|
671 |
+
self.brush_size_label.config(text=self.brush_size) # ラベル更新
|
672 |
+
winsound.PlaySound("C:/Windows/Media/Windows Information Bar.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
673 |
+
self.display_brush_canvas()
|
674 |
+
else:
|
675 |
+
self.adjust_color_brightness(event)
|
676 |
+
|
677 |
+
def select_blush_size(self, event):
|
678 |
+
self.brush_size = abs(event.x - 60)
|
679 |
+
self.brush_size_label.config(text=self.brush_size) # ラベル更新
|
680 |
+
winsound.PlaySound("C:/Windows/Media/Windows Information Bar.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
681 |
+
self.display_brush_canvas()
|
682 |
+
|
683 |
+
def toggle_eraser(self, event=None):
|
684 |
+
"""消しゴムモードの切り替え"""
|
685 |
+
self.eraser_mode = not self.eraser_mode
|
686 |
+
if self.eraser_mode:
|
687 |
+
self.precolor = self.color
|
688 |
+
self.color = "white"
|
689 |
+
self.eraser_button.config(bg="red")
|
690 |
+
self.brush_canvas.config(bg="black")
|
691 |
+
winsound.PlaySound("C:/Windows/Media/Windows Exclamation.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
692 |
+
else:
|
693 |
+
self.color = self.precolor
|
694 |
+
self.eraser_button.config( bg="black")
|
695 |
+
self.brush_canvas.config(bg="white")
|
696 |
+
winsound.PlaySound("C:/Windows/Media/chord.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
697 |
+
self.display_brush_canvas() # ブラシの表示を更新
|
698 |
+
|
699 |
+
def paint(self, event):
|
700 |
+
if self.resize_mode:
|
701 |
+
x = self.canvas.canvasx(event.x) - self.dx
|
702 |
+
y = self.canvas.canvasy(event.y) - self.dy
|
703 |
+
# 縮尺計算(リサイズ後の描画座標を元の画像サイズにマッピング)
|
704 |
+
x_original = int(x * self.ratio)
|
705 |
+
y_original = int(y * self.ratio)
|
706 |
+
# 描画範囲を計算
|
707 |
+
x1, y1 = x_original - self.brush_size, y_original - self.brush_size
|
708 |
+
x2, y2 = x_original + self.brush_size, y_original + self.brush_size
|
709 |
+
x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
|
710 |
+
current_time = time.time()
|
711 |
+
time_difference = current_time - self.last_paint_time
|
712 |
+
if not self.click_off:
|
713 |
+
delta_x, delta_y = x_original - self.last_x, y_original - self.last_y
|
714 |
+
for i in range(0, self.points):
|
715 |
+
xx = self.last_x + (delta_x * i / self.points)
|
716 |
+
yy = self.last_y + (delta_y * i / self.points)
|
717 |
+
x1, y1 = (xx - self.brush_size), (yy - self.brush_size)
|
718 |
+
x2, y2 = (xx + self.brush_size), (yy + self.brush_size)
|
719 |
+
self.draw_point(x1, y1, x2, y2,)
|
720 |
+
self.draw_point(x1, y1, x2, y2)
|
721 |
+
if time_difference > self.refresh_time:
|
722 |
+
self.overlap_layer()
|
723 |
+
resized_image = self.displayed_image.resize((self.resize_width, self.resize_height), Image.LANCZOS)
|
724 |
+
self.photo_image = ImageTk.PhotoImage(resized_image)
|
725 |
+
self.canvas.create_image(self.xplot, self.yplot, anchor=tk.CENTER, image=self.photo_image)
|
726 |
+
self.last_paint_time = current_time
|
727 |
+
self.click_off = False
|
728 |
+
self.last_x, self.last_y = x_original, y_original
|
729 |
+
else:
|
730 |
+
"""描画処理 (スクロール対応)"""
|
731 |
+
x = self.canvas.canvasx(event.x) - self.adjust_x
|
732 |
+
y = self.canvas.canvasy(event.y) - self.adjust_y
|
733 |
+
x1, y1 = (x - self.brush_size), (y - self.brush_size)
|
734 |
+
x2, y2 = (x + self.brush_size), (y + self.brush_size)
|
735 |
+
x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
|
736 |
+
current_time = time.time()
|
737 |
+
time_difference = current_time - self.last_paint_time
|
738 |
+
if not self.click_off:
|
739 |
+
delta_x, delta_y = x - self.last_x, y - self.last_y
|
740 |
+
for i in range(0, self.points):
|
741 |
+
xx = self.last_x + (delta_x * i / self.points)
|
742 |
+
yy = self.last_y + (delta_y * i / self.points)
|
743 |
+
x1, y1 = (xx - self.brush_size), (yy - self.brush_size)
|
744 |
+
x2, y2 = (xx + self.brush_size), (yy + self.brush_size)
|
745 |
+
self.draw_point(x1, y1, x2, y2,)
|
746 |
+
self.draw_point(x1, y1, x2, y2)
|
747 |
+
if time_difference > self.refresh_time:
|
748 |
+
self.overlap_layer()
|
749 |
+
self.photo_image = ImageTk.PhotoImage(self.displayed_image)
|
750 |
+
self.canvas.create_image(self.adjust_x, self.adjust_y, anchor=tk.NW, image=self.photo_image)
|
751 |
+
self.last_paint_time = current_time
|
752 |
+
# 座標と時間を記録
|
753 |
+
self.click_off = False
|
754 |
+
self.last_x, self.last_y = x, y
|
755 |
+
|
756 |
+
def draw_point(self, x1, y1, x2, y2):
|
757 |
+
draw =ImageDraw.Draw(self.images[self.drawing_layer])
|
758 |
+
if self.eraser_mode:
|
759 |
+
draw.ellipse([x1, y1, x2, y2], fill=(0, 0, 0, 0)) # 完全な透明色
|
760 |
+
else:
|
761 |
+
draw.ellipse([x1, y1, x2, y2], fill=self.color)
|
762 |
+
|
763 |
+
def copy_to_clipboard(self, event):
|
764 |
+
"""キャンバス上に描かれた画像をクリップボードにコピー"""
|
765 |
+
if event is None:
|
766 |
+
clipboard_image = self.displayed_image
|
767 |
+
else:
|
768 |
+
mask_image = Image.new("RGBA", (self.img_width, self.img_height), (0, 0, 0, 0))
|
769 |
+
if event.num == 2:
|
770 |
+
for i in range(1, 4): # 1 から 3 までループ
|
771 |
+
if self.layer_status[i]: # レイヤーが有効なら合成
|
772 |
+
mask_image = Image.alpha_composite(mask_image, self.images[i])
|
773 |
+
else:
|
774 |
+
mask_image = Image.alpha_composite(mask_image, self.images[self.drawing_layer])
|
775 |
+
clipboard_image = mask_image.copy()
|
776 |
+
pixels = clipboard_image.load()
|
777 |
+
# 画像サイズを取得
|
778 |
+
width, height = clipboard_image.size
|
779 |
+
# アルファ値をチェックして黒に変換
|
780 |
+
for y in range(height):
|
781 |
+
for x in range(width):
|
782 |
+
r, g, b, a = pixels[x, y]
|
783 |
+
if a > 0: # アルファ値が0ではない部分
|
784 |
+
pixels[x, y] = (255, 255, 255, a)
|
785 |
+
else:
|
786 |
+
pixels[x, y] = (0, 0, 0, a)
|
787 |
+
output = io.BytesIO()
|
788 |
+
clipboard_image.convert("RGB").save(output, format="BMP")
|
789 |
+
data = output.getvalue()[14:]
|
790 |
+
try:
|
791 |
+
win32clipboard.OpenClipboard()
|
792 |
+
win32clipboard.EmptyClipboard()
|
793 |
+
win32clipboard.SetClipboardData(win32con.CF_DIB, data)
|
794 |
+
winsound.PlaySound("C:/Windows/Media/chimes.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
795 |
+
except Exception as e:
|
796 |
+
print(f"画像のコピーに失敗: {e}")
|
797 |
+
finally:
|
798 |
+
win32clipboard.CloseClipboard()
|
799 |
+
|
800 |
+
def get_clipboard_image(self, event=None):
|
801 |
+
win32clipboard.OpenClipboard()
|
802 |
+
try:
|
803 |
+
if win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_DIB):
|
804 |
+
data = win32clipboard.GetClipboardData(win32clipboard.CF_DIB)
|
805 |
+
else:
|
806 |
+
print("クリップボードに画像がありません。")
|
807 |
+
return None
|
808 |
+
finally:
|
809 |
+
win32clipboard.CloseClipboard()
|
810 |
+
|
811 |
+
# BMP のヘッダーを適切に追加して PIL 画像に変換
|
812 |
+
bmp_data = io.BytesIO()
|
813 |
+
bmp_data.write(b'BM') # BMP シグネチャ
|
814 |
+
bmp_data.write((len(data) + 14).to_bytes(4, 'little')) # ファイルサイズ
|
815 |
+
bmp_data.write(b'\x00\x00\x00\x00') # 予約領域
|
816 |
+
bmp_data.write((14 + 40).to_bytes(4, 'little')) # ピクセルデータまでのオフセット
|
817 |
+
bmp_data.write(data) # DIB データ
|
818 |
+
bmp_data.seek(0) # 先頭に戻す
|
819 |
+
|
820 |
+
# PIL 画像として開く
|
821 |
+
image = Image.open(bmp_data)
|
822 |
+
|
823 |
+
# 取得した画像を `self.images[0]` に設定
|
824 |
+
self.images[0] = image.convert("RGBA") # ここで RGBA に変換
|
825 |
+
self.img_width, self.img_height = self.images[0].size
|
826 |
+
self.reset_images()
|
827 |
+
|
828 |
+
# UI の更新
|
829 |
+
self.layer_mode = 0
|
830 |
+
self.layer_label.config(text="両", bg="blue")
|
831 |
+
self.resize_mode = True
|
832 |
+
self.display_image()
|
833 |
+
self.display_title()
|
834 |
+
|
835 |
+
# 効果音を鳴らす
|
836 |
+
winsound.PlaySound("C:/Windows/Media/chimes.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
|
837 |
+
|
838 |
+
# イベント処理
|
839 |
+
self.on_drag(None)
|
840 |
+
|
841 |
+
|
842 |
+
if __name__ == "__main__":
|
843 |
+
app = ImageDrawer()
|
844 |
+
app.mainloop()
|
i2i_4layer.png
ADDED
![]() |
Git LFS Details
|