Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter.colorchooser import askcolor
- import threading
- import time
- import math
- import sys
- import os
- from PIL import ImageGrab
- sys.setrecursionlimit(10**6)
- threading.stack_size(10**8*2)
- _root = frame1
- _parentframe = parentframe
- def Paint():
- global _root
- global tk
- global askcolor
- global threading
- global time
- global math
- global sys
- global os
- global ImageGrab
- global _parentframe
- DEFAULT_PEN_SIZE = 5.0
- DEFAULT_COLOR = '#000000'
- filepath0 = ''
- def getnewpath():
- global filepath0
- global os
- filepath1 = 'T:/Storage/Images/Untitled'
- rep = 0
- while True:
- if os.path.exists(filepath1 + str(rep) + '.jpg'):
- rep += 1
- else:
- filepath0 = filepath1 + str(rep) + '.jpg'
- break
- getnewpath()
- mousespeed = 0
- def getmousespeed():
- global math
- # Start
- x1 = root.winfo_pointerx()
- y1 = root.winfo_pointery()
- time.sleep(0.1)
- # End
- x2 = root.winfo_pointerx()
- y2 = root.winfo_pointery()
- # Determine distance traveled
- dx = x2 - x1
- dy = y2 - y1
- dist = math.sqrt(math.pow(dx, 2) + math.pow(dy, 2)) # Distance between 2 points
- global mousespeed
- mousespeed = dist / 0.1
- getmousespeed()
- def __init__():
- global _root
- global threading
- global tk
- global threading
- global topbar
- global bottombar
- global brush_button
- global pen_button
- global color_button
- global eraser_button
- global clear_button
- global choose_size_button
- global save_button
- global c
- root = _root
- #root.title("Pixedit")
- #root.iconphoto(False, PhotoImage(file="T:/Programs/Pixedit/Attributes/Icon.png"))
- topbar = tk.Frame(_root)
- topbar.pack(side='top', anchor='n', expand=False, fill='x')
- #topbar.pack_propagate(0)
- bottombar = tk.Frame(_root)
- bottombar.pack(side='bottom', anchor='n', expand=True, fill='both')
- bottombar.pack_propagate(0)
- brush_button = tk.Button(topbar, text='Brush', command=use_brush)
- brush_button.pack(anchor='nw', padx=5, pady=5, side='left')
- pen_button = tk.Button(topbar, text='Pen', command=use_pen)
- pen_button.pack(anchor='nw', padx=5, pady=5, side='left')
- color_button = tk.Button(topbar, text='Color', command=choose_color)
- color_button.pack(anchor='nw', padx=5, pady=5, side='left')
- eraser_button = tk.Button(topbar, text='Eraser', command=use_eraser)
- eraser_button.pack(anchor='nw', padx=5, pady=5, side='left')
- clear_button = tk.Button(topbar, text='Clear', command=clear_canvas)
- clear_button.pack(anchor='nw', padx=5, pady=5, side='left')
- choose_size_button = tk.Scale(topbar, from_=1, to=100, orient='horizontal')
- choose_size_button.pack(anchor='nw', padx=5, pady=5, side='left')
- save_button = tk.Button(topbar, text='Save', command=save)
- save_button.pack(anchor='nw', padx=5, pady=5, side='left')
- c = tk.Canvas(bottombar, bg='white', cursor='none')
- c.pack(expand=True, fill='both')
- threading.Thread(target=getmousespeed, daemon=True).start()
- setup()
- #root.mainloop()
- def color_variant(hex_color, brightness_offset=1):
- """ takes a color like #87c95f and produces a lighter or darker variant """
- if len(hex_color) != 7:
- raise Exception("Passed %s into color_variant(), needs to be in #87c95f format." % hex_color)
- rgb_hex = [hex_color[x:x+2] for x in [1, 3, 5]]
- new_rgb_int = [int(hex_value, 16) + brightness_offset for hex_value in rgb_hex]
- new_rgb_int = [min([255, max([0, i])]) for i in new_rgb_int] # make sure new values are between 0 and 255
- # hex() produces "0x88", we want just "88"
- return "#" + "".join(["{:02x}".format(i) for i in new_rgb_int])
- def setup():
- global choose_size_button
- global brush_button
- global active_button
- global old_x
- global old_y
- global old_width
- global line_width
- global color
- global eraser_on
- global pen_on
- old_x = None
- old_y = None
- old_width = choose_size_button.get()
- choose_size_button.set(5)
- line_width = choose_size_button.get()
- color = DEFAULT_COLOR
- eraser_on = False
- pen_on = False
- active_button = brush_button
- activate_button(brush_button)
- c.bind('<B1-Motion>', paint)
- c.bind('<ButtonRelease-1>', reset)
- c.bind('<Motion>', brush_update)
- c.bind('<Button-1>', paint_circle)
- c.bind('<MouseWheel>', zoom)
- def use_pen():
- activate_button(pen_button, pen_mode=True)
- def use_brush():
- activate_button(brush_button)
- def choose_color():
- global color
- global askcolor
- use_brush()
- color = askcolor(color=color)[1]
- def use_eraser():
- activate_button(eraser_button, eraser_mode=True)
- def activate_button(some_button, eraser_mode=False, pen_mode=False):
- global active_button
- global eraser_on
- global pen_on
- active_button.config(relief='raised')
- some_button.config(relief='sunken')
- active_button = some_button
- eraser_on = eraser_mode
- pen_on = pen_mode
- def paint(event):
- global old_x
- global old_y
- global old_width
- line_width = choose_size_button.get()
- paint_color = '#ffffff' if eraser_on else color
- if pen_on == False:
- if old_x and old_y:
- c.create_line(old_x, old_y, event.x, event.y,
- width=line_width, fill=paint_color,
- capstyle='round', smooth=True, splinesteps=36)
- old_x = event.x
- old_y = event.y
- else:
- if old_x and old_y:
- global mousespeed
- target_width = line_width - (mousespeed * line_width/1500)
- min_width = line_width / 2
- if target_width < min_width:
- target_width = min_width
- if target_width == old_width:
- pen_width = target_width
- elif target_width > old_width:
- pen_width = old_width + 1
- elif target_width < old_width:
- pen_width = old_width - 1
- c.create_line(old_x, old_y, event.x, event.y,
- width=pen_width, fill=paint_color,
- capstyle='round', smooth=True, splinesteps=36, joinstyle='round')
- old_width = pen_width
- else:
- old_width = line_width
- old_x = event.x
- old_y = event.y
- brush_update(event)
- def paint_circle(event):
- paint_color = '#ffffff' if eraser_on else color
- halfwidth = choose_size_button.get() / 2
- c.create_oval(event.x - halfwidth, event.y - halfwidth, event.x + halfwidth - 1, event.y + halfwidth - 1, outline=paint_color, fill=paint_color)
- brush_update(event)
- def brush_update(event):
- halfwidth = choose_size_button.get() / 2
- global brush_outline
- global v_cross
- global h_cross
- try:
- c.delete(brush_outline)
- c.delete(v_cross)
- c.delete(h_cross)
- except:
- pass
- brush_outline = c.create_oval(event.x - halfwidth, event.y - halfwidth, event.x + halfwidth - 1, event.y + halfwidth - 1, outline=color_variant(color, 100))
- v_cross = c.create_line(event.x, event.y - 4, event.x, event.y + 5, fill=color_variant(color, 100))
- h_cross = c.create_line(event.x - 4, event.y, event.x + 5, event.y, fill=color_variant(color, 100))
- def zoom(event):
- if event.delta > 0:
- c.scale("all", event.x, event.y, 1.1, 1.1)
- elif event.delta < 0:
- c.scale("all", event.x, event.y, 0.9, 0.9)
- def clear_canvas():
- c.delete("all")
- def save():
- global brush_outline
- global v_cross
- global h_cross
- global filepath0
- global ImageGrab
- global _root
- global _parentframe
- try:
- c.delete(brush_outline)
- c.delete(v_cross)
- c.delete(h_cross)
- except:
- pass
- root.update()
- widget = c
- x = _parentframe.winfo_x() + _root.winfo_x() + widget.winfo_x() + 2
- y = _parentframe.winfo_x() + _root.winfo_y() + widget.winfo_y() + 54
- x1 = x + widget.winfo_width() - 4
- y1 = y + widget.winfo_height() - 4
- ImageGrab.grab().crop((x, y, x1, y1)).save(filepath0)
- def reset(event):
- global old_x
- global old_y
- global old_width
- old_x, old_y, old_width = None, None, choose_size_button.get()
- __init__()
- Paint()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement