♻️ Refactor generators and add an about window

This commit is contained in:
Clement Desmidt 2024-09-13 17:51:58 +02:00
parent 5112439712
commit 7a8ca677ea
4 changed files with 93 additions and 37 deletions

View File

@ -5,3 +5,4 @@ customtkinter~=5.1.3
configparser~=5.3.0
CTkToolTip~=0.4
requests~=2.32.3
CTkMenuBar~=0.8

41
src/entity/generator.py Normal file
View File

@ -0,0 +1,41 @@
from datetime import datetime
import os
import io
import tempfile
import requests
from PIL import Image
class Generator(object):
def __init__(self, generator=None, folder=None):
self.selected_folder = folder
if generator == 'poses':
self.generate_poses()
def generate_poses(self):
dt = datetime.now()
ts = int(datetime.timestamp(dt))
url = 'https://quickposes.com/ajax/getPosesForSlider/Chiaroscuro?_=' + str(ts)
resp_json = requests.get(url=url)
if resp_json.status_code == 200:
data = resp_json.json()
c = 0
for src in data:
if os.path.exists(os.path.join(self.selected_folder, src['src'])):
continue
if c >= 3:
break
buffer = tempfile.SpooledTemporaryFile(max_size=1e9)
r = requests.get('https://quickposes.com/assets/poses/' + src['src'], stream=True)
if r.status_code == 200:
downloaded = 0
for chunk in r.iter_content(chunk_size=1024):
downloaded += len(chunk)
buffer.write(chunk)
buffer.seek(0)
i = Image.open(io.BytesIO(buffer.read()))
i.save(os.path.join(self.selected_folder, src['src']), quality=85)
buffer.close()
c += 1

30
src/window/about.py Normal file
View File

@ -0,0 +1,30 @@
import webbrowser
import customtkinter
from customtkinter import CTkToplevel
class AboutWindow:
def __init__(self, app):
self.app = app
self.window = CTkToplevel(app.root)
self.window.title("À propos")
self.window.geometry("600x300")
self.window.protocol("WM_DELETE_WINDOW", self.on_closing)
link_gesture_drawing = customtkinter.CTkLabel(self.window, text="Open Source alternative to GestureDrawing!")
link_gesture_drawing.pack()
link_gesture_drawing.bind("<Button-1>",
lambda e: self.open_link("https://cubebrush.co/advanches/products/d9q6yq/gesturedrawing"))
link_shikiryu = customtkinter.CTkLabel(self.window, text="by Shikiryu")
link_shikiryu.pack()
link_shikiryu.bind("<Button-1>", lambda e: self.open_link("https://shikiryu.com"))
@staticmethod
def open_link(url):
webbrowser.open_new(url)
def on_closing(self):
self.window.destroy()

View File

@ -1,20 +1,15 @@
import io
import os
import tempfile
import tkinter
from pathlib import Path
from tkinter import *
from tkinter import filedialog
from datetime import datetime
import customtkinter
import requests
from PIL import Image
from src.entity.config import Config
from src.entity.generator import Generator
from src.util import Util
from src.window.about import AboutWindow
from src.window.image import ImageWindow
from src.window.session import SessionWindow
from CTkMenuBar import *
class App:
@ -30,13 +25,20 @@ class App:
self.selected_folder = ""
self.found_images = []
self.timer = 0
menu = CTkMenuBar(master=root)
generate_button = menu.add_cascade("Generate")
help_button = menu.add_cascade("Help")
dropdown = CustomDropdownMenu(widget=generate_button)
dropdown.add_option(option="Poses", command=lambda:self.download_poses('poses'))
about = CustomDropdownMenu(widget=help_button)
about.add_option(option="À propos", command=self.show_help)
self.title = customtkinter.CTkLabel(root, text="Drawing Training")
self.title.pack()
self.download_from_reference = customtkinter.CTkButton(root, text="Download 3 images from sketch daily",
command=self.download_from_reference)
self.download_from_reference.pack()
self.folder_selector = customtkinter.CTkButton(root, text="Select a folder", command=self.select_folder)
self.folder_selector.pack()
@ -79,6 +81,9 @@ class App:
except Exception:
pass
def show_help(self):
AboutWindow(self)
def lets_draw(self):
self.image_window = ImageWindow(self)
self.image_window.lets_draw(self.found_images.copy(), self.timer)
@ -96,30 +101,9 @@ class App:
self.find_images_in_folder()
self.check_lets_draw()
def download_from_reference(self):
dt = datetime.now()
ts = int(datetime.timestamp(dt))
url = 'https://quickposes.com/ajax/getPosesForSlider/Chiaroscuro?_=' + str(ts)
resp_json = requests.get(url=url)
if resp_json.status_code == 200:
data = resp_json.json()
c = 0
for src in data:
if c >= 3:
break
buffer = tempfile.SpooledTemporaryFile(max_size=1e9)
r = requests.get('https://quickposes.com/assets/poses/' + src['src'], stream=True)
if r.status_code == 200:
downloaded = 0
for chunk in r.iter_content(chunk_size=1024):
downloaded += len(chunk)
buffer.write(chunk)
buffer.seek(0)
i = Image.open(io.BytesIO(buffer.read()))
i.save(os.path.join(self.selected_folder, src['src']), quality=85)
buffer.close()
c += 1
def download_poses(self, generator=None):
Generator(generator=generator, folder=self.selected_folder)
self.find_images_in_folder()
def find_images_in_folder(self):
if Path(self.selected_folder).exists():