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