Advertisement
Techpad

Pixedit 4

Mar 16th, 2021
1,671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
E 9.21 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter.colorchooser import askcolor
  3. import threading
  4. import time
  5. import math
  6. import sys
  7. import os
  8. from PIL import ImageGrab
  9.  
  10. sys.setrecursionlimit(10**6)
  11. threading.stack_size(10**8*2)
  12.  
  13. _root = frame1
  14. _parentframe = parentframe
  15.  
  16. def Paint():
  17.     global _root
  18.     global tk
  19.     global askcolor
  20.     global threading
  21.     global time
  22.     global math
  23.     global sys
  24.     global os
  25.     global ImageGrab
  26.     global _parentframe
  27.  
  28.     DEFAULT_PEN_SIZE = 5.0
  29.     DEFAULT_COLOR = '#000000'
  30.  
  31.     filepath0 = ''
  32.  
  33.     def getnewpath():
  34.         global filepath0
  35.         global os
  36.         filepath1 = 'T:/Storage/Images/Untitled'
  37.         rep = 0
  38.         while True:
  39.             if os.path.exists(filepath1 + str(rep) + '.jpg'):
  40.                 rep += 1
  41.             else:
  42.                 filepath0 = filepath1 + str(rep) + '.jpg'
  43.                 break
  44.  
  45.     getnewpath()
  46.  
  47.     mousespeed = 0
  48.  
  49.     def getmousespeed():
  50.         global math
  51.         # Start
  52.         x1 = root.winfo_pointerx()
  53.         y1 = root.winfo_pointery()
  54.  
  55.         time.sleep(0.1)
  56.  
  57.         # End
  58.         x2 = root.winfo_pointerx()
  59.         y2 = root.winfo_pointery()
  60.  
  61.         # Determine distance traveled
  62.         dx = x2 - x1
  63.         dy = y2 - y1
  64.         dist = math.sqrt(math.pow(dx, 2) + math.pow(dy, 2))  # Distance between 2 points
  65.  
  66.         global mousespeed
  67.         mousespeed = dist / 0.1
  68.  
  69.         getmousespeed()
  70.  
  71.     def __init__():
  72.         global _root
  73.         global threading
  74.         global tk
  75.         global threading
  76.         global topbar
  77.         global bottombar
  78.         global brush_button
  79.         global pen_button
  80.         global color_button
  81.         global eraser_button
  82.         global clear_button
  83.         global choose_size_button
  84.         global save_button
  85.         global c
  86.  
  87.         root = _root
  88.         #root.title("Pixedit")
  89.         #root.iconphoto(False, PhotoImage(file="T:/Programs/Pixedit/Attributes/Icon.png"))
  90.  
  91.         topbar = tk.Frame(_root)
  92.         topbar.pack(side='top', anchor='n', expand=False, fill='x')
  93.         #topbar.pack_propagate(0)
  94.  
  95.         bottombar = tk.Frame(_root)
  96.         bottombar.pack(side='bottom', anchor='n', expand=True, fill='both')
  97.         bottombar.pack_propagate(0)
  98.  
  99.         brush_button = tk.Button(topbar, text='Brush', command=use_brush)
  100.         brush_button.pack(anchor='nw', padx=5, pady=5, side='left')
  101.  
  102.         pen_button = tk.Button(topbar, text='Pen', command=use_pen)
  103.         pen_button.pack(anchor='nw', padx=5, pady=5, side='left')
  104.  
  105.         color_button = tk.Button(topbar, text='Color', command=choose_color)
  106.         color_button.pack(anchor='nw', padx=5, pady=5, side='left')
  107.  
  108.         eraser_button = tk.Button(topbar, text='Eraser', command=use_eraser)
  109.         eraser_button.pack(anchor='nw', padx=5, pady=5, side='left')
  110.  
  111.         clear_button = tk.Button(topbar, text='Clear', command=clear_canvas)
  112.         clear_button.pack(anchor='nw', padx=5, pady=5, side='left')
  113.  
  114.         choose_size_button = tk.Scale(topbar, from_=1, to=100, orient='horizontal')
  115.         choose_size_button.pack(anchor='nw', padx=5, pady=5, side='left')
  116.  
  117.         save_button = tk.Button(topbar, text='Save', command=save)
  118.         save_button.pack(anchor='nw', padx=5, pady=5, side='left')
  119.  
  120.         c = tk.Canvas(bottombar, bg='white', cursor='none')
  121.         c.pack(expand=True, fill='both')
  122.  
  123.         threading.Thread(target=getmousespeed, daemon=True).start()
  124.  
  125.         setup()
  126.  
  127.         #root.mainloop()
  128.  
  129.     def color_variant(hex_color, brightness_offset=1):
  130.         """ takes a color like #87c95f and produces a lighter or darker variant """
  131.         if len(hex_color) != 7:
  132.             raise Exception("Passed %s into color_variant(), needs to be in #87c95f format." % hex_color)
  133.         rgb_hex = [hex_color[x:x+2] for x in [1, 3, 5]]
  134.         new_rgb_int = [int(hex_value, 16) + brightness_offset for hex_value in rgb_hex]
  135.         new_rgb_int = [min([255, max([0, i])]) for i in new_rgb_int] # make sure new values are between 0 and 255
  136.         # hex() produces "0x88", we want just "88"
  137.         return "#" + "".join(["{:02x}".format(i) for i in new_rgb_int])
  138.  
  139.     def setup():
  140.         global choose_size_button
  141.         global brush_button
  142.         global active_button
  143.         global old_x
  144.         global old_y
  145.         global old_width
  146.         global line_width
  147.         global color
  148.         global eraser_on
  149.         global pen_on
  150.         old_x = None
  151.         old_y = None
  152.         old_width = choose_size_button.get()
  153.         choose_size_button.set(5)
  154.         line_width = choose_size_button.get()
  155.         color = DEFAULT_COLOR
  156.         eraser_on = False
  157.         pen_on = False
  158.         active_button = brush_button
  159.         activate_button(brush_button)
  160.         c.bind('<B1-Motion>', paint)
  161.         c.bind('<ButtonRelease-1>', reset)
  162.         c.bind('<Motion>', brush_update)
  163.         c.bind('<Button-1>', paint_circle)
  164.         c.bind('<MouseWheel>', zoom)
  165.  
  166.     def use_pen():
  167.         activate_button(pen_button, pen_mode=True)
  168.  
  169.     def use_brush():
  170.         activate_button(brush_button)
  171.  
  172.     def choose_color():
  173.         global color
  174.         global askcolor
  175.         use_brush()
  176.         color = askcolor(color=color)[1]
  177.  
  178.     def use_eraser():
  179.         activate_button(eraser_button, eraser_mode=True)
  180.  
  181.     def activate_button(some_button, eraser_mode=False, pen_mode=False):
  182.         global active_button
  183.         global eraser_on
  184.         global pen_on
  185.         active_button.config(relief='raised')
  186.         some_button.config(relief='sunken')
  187.         active_button = some_button
  188.         eraser_on = eraser_mode
  189.         pen_on = pen_mode
  190.  
  191.     def paint(event):
  192.         global old_x
  193.         global old_y
  194.         global old_width
  195.         line_width = choose_size_button.get()
  196.         paint_color = '#ffffff' if eraser_on else color
  197.         if pen_on == False:
  198.             if old_x and old_y:
  199.                 c.create_line(old_x, old_y, event.x, event.y,
  200.                                    width=line_width, fill=paint_color,
  201.                                    capstyle='round', smooth=True, splinesteps=36)
  202.             old_x = event.x
  203.             old_y = event.y
  204.         else:
  205.             if old_x and old_y:
  206.                 global mousespeed
  207.                 target_width = line_width - (mousespeed * line_width/1500)
  208.                 min_width = line_width / 2
  209.                 if target_width < min_width:
  210.                     target_width = min_width
  211.                 if target_width == old_width:
  212.                     pen_width = target_width
  213.                 elif target_width > old_width:
  214.                     pen_width = old_width + 1
  215.                 elif target_width < old_width:
  216.                     pen_width = old_width - 1
  217.                 c.create_line(old_x, old_y, event.x, event.y,
  218.                                    width=pen_width, fill=paint_color,
  219.                                    capstyle='round', smooth=True, splinesteps=36, joinstyle='round')
  220.                 old_width = pen_width
  221.             else:
  222.                 old_width = line_width
  223.             old_x = event.x
  224.             old_y = event.y
  225.         brush_update(event)
  226.  
  227.     def paint_circle(event):
  228.         paint_color = '#ffffff' if eraser_on else color
  229.         halfwidth = choose_size_button.get() / 2
  230.         c.create_oval(event.x - halfwidth, event.y - halfwidth, event.x + halfwidth - 1, event.y + halfwidth - 1, outline=paint_color, fill=paint_color)
  231.         brush_update(event)
  232.  
  233.     def brush_update(event):
  234.         halfwidth = choose_size_button.get() / 2
  235.         global brush_outline
  236.         global v_cross
  237.         global h_cross
  238.         try:
  239.             c.delete(brush_outline)
  240.             c.delete(v_cross)
  241.             c.delete(h_cross)
  242.         except:
  243.             pass
  244.         brush_outline = c.create_oval(event.x - halfwidth, event.y - halfwidth, event.x + halfwidth - 1, event.y + halfwidth - 1, outline=color_variant(color, 100))
  245.         v_cross = c.create_line(event.x, event.y - 4, event.x, event.y + 5, fill=color_variant(color, 100))
  246.         h_cross = c.create_line(event.x - 4, event.y, event.x + 5, event.y, fill=color_variant(color, 100))
  247.  
  248.     def zoom(event):
  249.         if event.delta > 0:
  250.             c.scale("all", event.x, event.y, 1.1, 1.1)
  251.         elif event.delta < 0:
  252.             c.scale("all", event.x, event.y, 0.9, 0.9)
  253.  
  254.     def clear_canvas():
  255.         c.delete("all")
  256.  
  257.     def save():
  258.         global brush_outline
  259.         global v_cross
  260.         global h_cross
  261.         global filepath0
  262.         global ImageGrab
  263.         global _root
  264.         global _parentframe
  265.         try:
  266.             c.delete(brush_outline)
  267.             c.delete(v_cross)
  268.             c.delete(h_cross)
  269.         except:
  270.             pass
  271.         root.update()
  272.         widget = c
  273.         x = _parentframe.winfo_x() + _root.winfo_x() + widget.winfo_x() + 2
  274.         y = _parentframe.winfo_x() + _root.winfo_y() + widget.winfo_y() + 54
  275.         x1 = x + widget.winfo_width() - 4
  276.         y1 = y + widget.winfo_height() - 4
  277.         ImageGrab.grab().crop((x, y, x1, y1)).save(filepath0)
  278.  
  279.     def reset(event):
  280.         global old_x
  281.         global old_y
  282.         global old_width
  283.         old_x, old_y, old_width = None, None, choose_size_button.get()
  284.  
  285.     __init__()
  286.  
  287. Paint()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement