Y-CogniSketch Python GUI

👤 Sharing: AI
import tkinter as tk
from tkinter import ttk, colorchooser, filedialog, messagebox
from PIL import Image, ImageDraw, ImageTk
import random

class CogniSketch:
    def __init__(self, master):
        self.master = master
        master.title("CogniSketch: Creative AI-Assisted Drawing")
        master.geometry("1000x700")

        self.color = "black"
        self.line_width = 3
        self.drawing_tool = "pen"
        self.image = Image.new("RGB", (800, 600), "white")
        self.draw = ImageDraw.Draw(self.image)
        self.last_x, self.last_y = None, None

        self.canvas_width = 800
        self.canvas_height = 600

        self.canvas_frame = ttk.Frame(master)
        self.canvas_frame.pack(side=tk.LEFT, padx=10, pady=10)

        self.canvas = tk.Canvas(self.canvas_frame, width=self.canvas_width, height=self.canvas_height, bg='white')
        self.canvas.pack(expand=tk.YES, fill=tk.BOTH)
        self.canvas.bind('<B1-Motion>', self.paint)
        self.canvas.bind('<ButtonRelease-1>', self.reset)

        self.image_tk = ImageTk.PhotoImage(self.image)
        self.canvas.create_image(0, 0, anchor=tk.NW, image=self.image_tk)

        self.tools_frame = ttk.Frame(master)
        self.tools_frame.pack(side=tk.RIGHT, padx=10, pady=10, fill=tk.Y)

        ttk.Label(self.tools_frame, text="Tools:", font=('Arial', 12, 'bold')).pack(pady=5)
        ttk.Button(self.tools_frame, text="Pen", command=lambda: self.set_tool("pen")).pack(pady=3)
        ttk.Button(self.tools_frame, text="Eraser", command=lambda: self.set_tool("eraser")).pack(pady=3)
        ttk.Button(self.tools_frame, text="Spray", command=lambda: self.set_tool("spray")).pack(pady=3)
        ttk.Button(self.tools_frame, text="Rectangle", command=lambda: self.set_tool("rectangle")).pack(pady=3)
        ttk.Button(self.tools_frame, text="Ellipse", command=lambda: self.set_tool("ellipse")).pack(pady=3)

        ttk.Label(self.tools_frame, text="Options:", font=('Arial', 12, 'bold')).pack(pady=5)
        ttk.Button(self.tools_frame, text="Choose Color", command=self.choose_color).pack(pady=3)
        ttk.Label(self.tools_frame, text="Line Width:").pack()
        self.width_scale = tk.Scale(self.tools_frame, from_=1, to=20, orient=tk.HORIZONTAL, command=self.set_line_width)
        self.width_scale.set(self.line_width)
        self.width_scale.pack(pady=3)

        ttk.Button(self.tools_frame, text="Clear Canvas", command=self.clear_canvas).pack(pady=5)
        ttk.Button(self.tools_frame, text="Save Image", command=self.save_image).pack(pady=5)
        ttk.Button(self.tools_frame, text="Details", command=self.show_details).pack(pady=5)

        # AI-Assisted Features (Dummy for now)
        ttk.Label(self.tools_frame, text="AI Features:", font=('Arial', 12, 'bold')).pack(pady=5)
        ttk.Button(self.tools_frame, text="Auto-Smooth", command=self.auto_smooth).pack(pady=3)
        ttk.Button(self.tools_frame, text="Suggest Colors", command=self.suggest_colors).pack(pady=3)
        ttk.Button(self.tools_frame, text="Generate Art (Placeholder)", command=self.generate_art).pack(pady=3)

    def set_tool(self, tool):
        self.drawing_tool = tool

    def choose_color(self):
        color_code = colorchooser.askcolor(title="Choose drawing color")[1]
        if color_code:
            self.color = color_code

    def set_line_width(self, width):
        self.line_width = int(width)

    def clear_canvas(self):
        self.canvas.delete("all")
        self.image = Image.new("RGB", (self.canvas_width, self.canvas_height), "white")
        self.draw = ImageDraw.Draw(self.image)
        self.image_tk = ImageTk.PhotoImage(self.image)
        self.canvas.create_image(0, 0, anchor=tk.NW, image=self.image_tk)

    def save_image(self):
        file_path = filedialog.asksaveasfilename(defaultextension=".png")
        if file_path:
            self.image.save(file_path)

    def paint(self, event):
        if self.drawing_tool == "pen":
            self.draw_pen(event)
        elif self.drawing_tool == "eraser":
            self.draw_eraser(event)
        elif self.drawing_tool == "spray":
            self.draw_spray(event)
        elif self.drawing_tool == "rectangle":
            self.draw_rectangle(event)
        elif self.drawing_tool == "ellipse":
            self.draw_ellipse(event)

    def draw_pen(self, event):
        if self.last_x and self.last_y:
            self.canvas.create_line(self.last_x, self.last_y, event.x, event.y, width=self.line_width, fill=self.color, capstyle=tk.ROUND, smooth=tk.TRUE)
            self.draw.line((self.last_x, self.last_y, event.x, event.y), width=self.line_width, fill=self.color)
        self.last_x = event.x
        self.last_y = event.y

    def draw_eraser(self, event):
        eraser_width = self.line_width * 3  # Eraser is larger
        x1, y1 = (event.x - eraser_width), (event.y - eraser_width)
        x2, y2 = (event.x + eraser_width), (event.y + eraser_width)
        self.canvas.create_oval(x1, y1, x2, y2, fill='white', outline='white')
        self.draw.ellipse((x1, y1, x2, y2), fill='white', outline='white')

    def draw_spray(self, event):
        for i in range(30):
            x = event.x + random.randint(-15, 15)
            y = event.y + random.randint(-15, 15)
            self.canvas.create_line(x, y, x + 1, y + 1, width=1, fill=self.color, capstyle=tk.ROUND)
            self.draw.line((x, y, x + 1, y + 1), width=1, fill=self.color)

    def draw_rectangle(self, event):
        if self.last_x and self.last_y:
            self.canvas.delete("current_rect")
            x1, y1 = self.last_x, self.last_y
            x2, y2 = event.x, event.y
            self.canvas.create_rectangle(x1, y1, x2, y2, outline=self.color, width=self.line_width, tags="current_rect")
            #self.draw.rectangle((x1, y1, x2, y2), outline=self.color, width=self.line_width)

    def draw_ellipse(self, event):
        if self.last_x and self.last_y:
            self.canvas.delete("current_ellipse")
            x1, y1 = self.last_x, self.last_y
            x2, y2 = event.x, event.y
            self.canvas.create_oval(x1, y1, x2, y2, outline=self.color, width=self.line_width, tags="current_ellipse")
            #self.draw.ellipse((x1, y1, x2, y2), outline=self.color, width=self.line_width)


    def reset(self, event):
        self.last_x, self.last_y = None, None
        self.image_tk = ImageTk.PhotoImage(self.image)
        self.canvas.create_image(0, 0, anchor=tk.NW, image=self.image_tk)


    def auto_smooth(self):
        messagebox.showinfo("AI Feature", "Auto-Smooth: This feature would intelligently smooth out your lines (Placeholder).")

    def suggest_colors(self):
        messagebox.showinfo("AI Feature", "Suggest Colors: This feature would suggest complementary colors based on your current drawing (Placeholder).")

    def generate_art(self):
        messagebox.showinfo("AI Feature", "Generate Art: This feature would generate artwork based on your sketches (Placeholder).")

    def show_details(self):
        messagebox.showinfo("CogniSketch Details", "CogniSketch is a versatile drawing application that combines traditional drawing tools with AI-assisted features. It allows users to create digital art with ease, offering tools like pen, eraser, spray, and shape drawing. AI features like auto-smoothing and color suggestions aim to enhance the creative process. Save your creations as PNG files and explore the endless possibilities of digital art.")

root = tk.Tk()
app = CogniSketch(root)
root.mainloop()
👁️ Viewed: 5

Comments