From 19ac21aee0a5b7edcd210fc96c992769302235f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E8=AF=AD?= Date: Sun, 17 Aug 2025 08:38:44 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20draw=5Fpad.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- draw_pad.py | 157 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 139 insertions(+), 18 deletions(-) diff --git a/draw_pad.py b/draw_pad.py index b49c764..44e76fd 100644 --- a/draw_pad.py +++ b/draw_pad.py @@ -2,6 +2,99 @@ from tkinter.colorchooser import askcolor from tkinter import filedialog, messagebox import PIL.ImageGrab as ImageGrab +import math + + +# 处理颜色名称(如"white", "black"等) +color_names = { + 'white': (255, 255, 255), + 'black': (0, 0, 0), + 'red': (255, 0, 0), + 'green': (0, 255, 0), + 'blue': (0, 0, 255), + # 可以根据需要添加更多颜色名称 +} + + + + +#最大元归一 +def toone(lst): + hig = len(lst) + wid = len(lst[0]) + + if hig*wid == 1: + return [[1]] + + max = 0 + #获取列表中的最大元 + for i in range(hig): + for j in range(wid): + if lst[i][j]>max: + max = lst[i][j] + min = 0 + min = max + #获取列表中的最小元 + for i in range(hig): + for j in range(wid): + if lst[i][j]", self.paint) self.canvas.bind("", self.paint) self.canvas.bind("", self.on_canvas_resize) + def paint(self, event): # 保存当前颜色 @@ -69,7 +163,12 @@ def paint(self, event): center_y = event.y // self.pixel_size # 根据笔墨粗细绘制多个像素 + i = 0 + j = 0 radius = self.pen_size // 2 + true_pen_color = normal_2d_wg(radius*2+1) + print(true_pen_color) + print(toone(true_pen_color)) for dy in range(-radius, radius + 1): for dx in range(-radius, radius + 1): x = center_x + dx @@ -85,7 +184,11 @@ def paint(self, event): if self.is_eraser_mode: self._erase_pixel_at(screen_x, screen_y) else: - self._draw_pixel(screen_x, screen_y, self.pen_color) + #print(str_color_to_rgb(self.pen_color)[0]*true_pen_color[i][j]) + self._draw_pixel(screen_x, screen_y, rgb_to_str_color(round(str_color_to_rgb(self.pen_color)[0]*true_pen_color[i][j]),round(str_color_to_rgb(self.pen_color)[0]*true_pen_color[i][j]),round(str_color_to_rgb(self.pen_color)[0]*true_pen_color[i][j])),x,y) + j+=1 + j=0 + i+=1 # 恢复原来的颜色 self.pen_color = original_color @@ -110,6 +213,31 @@ def _draw_pixel(self, screen_x, screen_y, color): screen_x + self.pixel_size, screen_y + self.pixel_size, fill=color, outline=color, tags=self.PIXEL_TAG ) + + + def _draw_pixel(self, screen_x, screen_y, color,px,py): + (r,g,b) = str_color_to_rgb(color) + (_r,_g,_b) = self.get_pixel(px,py) + + r0 = r+_r + g0 = g+_g + b0 = b+_b + + + if r0 > 255: + r0 = 255 + if g0 > 255: + g0 = 255 + if b0 > 255: + b0 = 255 + + + """在指定位置绘制像素""" + self.canvas.create_rectangle( + screen_x, screen_y, + screen_x + self.pixel_size, screen_y + self.pixel_size, + fill=rgb_to_str_color(r0,g0,b0), outline=color, tags=self.PIXEL_TAG + ) def _get_current_drawing_color(self): """获取当前绘制颜色""" @@ -190,12 +318,12 @@ def draw_pixel_grid(self): # 绘制垂直线 for x in range(0, width + 1, self.pixel_size): - line = self.canvas.create_line(x, 0, x, height, fill="#e0e0e0", tags=self.GRID_TAG) + line = self.canvas.create_line(x, 0, x, height, fill="#707070", tags=self.GRID_TAG) self.grid_lines.append(line) # 绘制水平线 for y in range(0, height + 1, self.pixel_size): - line = self.canvas.create_line(0, y, width, y, fill="#e0e0e0", tags=self.GRID_TAG) + line = self.canvas.create_line(0, y, width, y, fill="#707070", tags=self.GRID_TAG) self.grid_lines.append(line) # 确保网格线在最底层,像素在最上层 @@ -248,15 +376,7 @@ def get_pixel(self, x, y): if not color: color = self.canvas.cget("bg") - # 处理颜色名称(如"white", "black"等) - color_names = { - 'white': (255, 255, 255), - 'black': (0, 0, 0), - 'red': (255, 0, 0), - 'green': (0, 255, 0), - 'blue': (0, 0, 255), - # 可以根据需要添加更多颜色名称 - } + if color in color_names: return color_names[color] @@ -372,5 +492,6 @@ def set_canvas(self, pixel_data): if __name__ == "__main__": root = Tk() # 可以在这里指定网格的宽高,例如 64x48 - app = PaintApp(root, grid_width=100, grid_height=100) - root.mainloop() \ No newline at end of file + app = PaintApp(root, grid_width=64, grid_height=48) + + root.mainloop() From 171525ebb8198075e1e3e09b3977048c1356f7ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E8=AF=AD?= Date: Sun, 17 Aug 2025 08:50:44 +0800 Subject: [PATCH 2/3] Update draw_pad.py --- draw_pad.py | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/draw_pad.py b/draw_pad.py index 44e76fd..e9e6dfb 100644 --- a/draw_pad.py +++ b/draw_pad.py @@ -4,7 +4,6 @@ import PIL.ImageGrab as ImageGrab import math - # 处理颜色名称(如"white", "black"等) color_names = { 'white': (255, 255, 255), @@ -15,9 +14,6 @@ # 可以根据需要添加更多颜色名称 } - - - #最大元归一 def toone(lst): hig = len(lst) @@ -49,10 +45,6 @@ def toone(lst): lst[i][j] = k*lst[i][j]+k_0 return lst - - - - #二维高斯分布 n:点阵边长 def normal_2d_wg(n): @@ -65,11 +57,7 @@ def normal_2d_wg(n): ds = toone(ds) return ds - - def str_color_to_rgb(color): - - if color in color_names: return color_names[color] @@ -95,7 +83,6 @@ def str_color_to_rgb(color): def rgb_to_str_color(r,g,b): return f"#{r:02x}{g:02x}{b:02x}" - class PaintApp: # 常量定义 PIXEL_TAG = "pixel" @@ -107,8 +94,8 @@ def __init__(self, root, grid_width=64, grid_height=48): self.root.geometry("800x600") self.root.configure(background="white") # 默认画笔颜色和大小 - self.pen_color = "white" - self.eraser_color = "black" + self.pen_color = "black" + self.eraser_color = "white" self.pen_size = 1 # 修改默认值为1,表示1个像素单位 # 像素网格设置 self.pixel_grid_enabled = True @@ -149,7 +136,6 @@ def __init__(self, root, grid_width=64, grid_height=48): self.canvas.bind("", self.paint) self.canvas.bind("", self.on_canvas_resize) - def paint(self, event): # 保存当前颜色 original_color = self.pen_color @@ -223,7 +209,6 @@ def _draw_pixel(self, screen_x, screen_y, color,px,py): g0 = g+_g b0 = b+_b - if r0 > 255: r0 = 255 if g0 > 255: @@ -231,7 +216,6 @@ def _draw_pixel(self, screen_x, screen_y, color,px,py): if b0 > 255: b0 = 255 - """在指定位置绘制像素""" self.canvas.create_rectangle( screen_x, screen_y, From 3b4e64fcceb46708db304969311fcb8393de53c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E8=AF=AD?= Date: Sun, 17 Aug 2025 08:51:59 +0800 Subject: [PATCH 3/3] Update draw_pad.py --- draw_pad.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/draw_pad.py b/draw_pad.py index e9e6dfb..3e40409 100644 --- a/draw_pad.py +++ b/draw_pad.py @@ -129,7 +129,7 @@ def __init__(self, root, grid_width=64, grid_height=48): self.size_scale.pack(side=LEFT, padx=10) # 创建画布 - self.canvas = Canvas(main_frame, bg="black") + self.canvas = Canvas(main_frame, bg="white") self.canvas.pack(fill=BOTH, expand=True) self.canvas.bind("", self.paint)