Skip to content

Commit

Permalink
pack things better
Browse files Browse the repository at this point in the history
  • Loading branch information
hollowstrawberry committed May 11, 2023
1 parent e430ee5 commit a194628
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ __pycache__/
.Python
build/
develop-eggs/
#dist/
dist/
downloads/
eggs/
.eggs/
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Useful for Civitai, sharing on Discord, or messing with people.

### How to use

Download the `.exe` file in the dist folder and enjoy.
Download the `.pyw` file and run it with Python.

Alternatively, download the `.pyw` file and run it with Python.
Alternatively, download the `.exe` from the [Releases](https://github.com/hollowstrawberry/png_info_editor/releases).

![preview](preview.png)
![preview](images/screenshot.png)
Binary file removed dist/png_info_editor.exe
Binary file not shown.
File renamed without changes
Binary file added images/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file renamed dist/test.png → images/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
68 changes: 30 additions & 38 deletions png_info_editor.pyw
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,40 @@ import tkinter as tk
from tkinter import filedialog, messagebox
from PIL import Image, PngImagePlugin

START = "1.0"
ERROR = "Oops"

def select_source_file():
global source_label, textbox
filename = filedialog.askopenfilename(initialdir=os.getcwd(),
title="Select source file",
filetypes=(("PNG", "*.png"),))
filename = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select source file", filetypes=(("PNG", "*.png"),))
if filename:
with Image.open(filename, formats=("png",)) as source:
info = source.info.get("parameters", None)
if not info:
return messagebox.showerror(title="Oops", message="No PNG info found in this image.")
source_label.config(text=f"{filename.split('/')[-1]}")
source_file = filename
textbox.delete("1.0", "end")
textbox.insert("1.0", info)
textbox.delete(START, tk.END)
textbox.insert(START, info)
apply_color()

def select_target_file():
global target_file, target_label
filename = filedialog.askopenfilename(initialdir=os.getcwd(),
title="Select target file",
filetypes=(("PNG", "*.png"),))
filename = filedialog.askopenfilename( initialdir=os.getcwd(), title="Select target file", filetypes=(("PNG", "*.png"),))
if filename:
target_label.config(text=f"{filename.split('/')[-1]}")
target_file = filename

def copy_png_info():
global target_file, textbox
info = textbox.get("1.0",'end-1c').strip()
info = textbox.get(START, tk.END).strip()
if not info:
return messagebox.showerror(title="Oops", message="Please select a file to copy PNG info from, or write your own.")
return messagebox.showerror(title=ERROR, message="Please select a file to copy PNG info from, or write your own.")
if not target_file:
return messagebox.showerror(title="Oops", message="Please select a target image to save the PNG info to.")
return messagebox.showerror(title=ERROR, message="Please select a target image to save the PNG info to.")
try:
target = Image.open(target_file, formats=("png",))
except:
return messagebox.showerror(title="Oops", message="The target image might've been moved or is invalid.")
return messagebox.showerror(title=ERROR, message="The target image might've been moved or is invalid.")
pnginfo = PngImagePlugin.PngInfo()
pnginfo.add_text("parameters", info)
target.save(target_file, format="png", pnginfo=pnginfo)
Expand All @@ -48,17 +46,13 @@ def copy_png_info():
def apply_color(event=None):
global textbox, tags
for tag in tags:
textbox.tag_remove(tag, "1.0", "end")
lines = textbox.get("1.0", "end").splitlines()
textbox.tag_remove(tag, START, tk.END)
lines = textbox.get(START, tk.END).splitlines()
for i, line in enumerate(lines):
for tag, pattern in tags.items():
for match in re.finditer(pattern, line):
textbox.tag_add(tag, f"{i+1}.{match.start(1)}", f"{i+1}.{match.end(1)}")


WIDTH = 700
HEIGHT = 400

tags = {
"blue": r"(?:^|, )([\w ]+):",
"purple": r"(<\w+:[^>]+>)",
Expand All @@ -68,32 +62,30 @@ target_file = ""

window = tk.Tk()
window.title("PNG Info editor")
window.geometry(f"{WIDTH}x{HEIGHT}")

boxtop = tk.Frame(window, width=WIDTH, height=50)
boxmiddle = tk.Frame(window, width=WIDTH, height=100)
boxbottom = tk.Frame(window, width=WIDTH, height=250)
window.columnconfigure(0, minsize=WIDTH)
boxtop.grid(row=0, pady=10)
boxtop = tk.Frame(window)
boxmiddle = tk.Frame(window)
boxbottom = tk.Frame(window)
boxtop.grid(row=0)
boxmiddle.grid(row=1)
boxbottom.grid(row=2)
boxmiddle.grid_propagate(0)
boxmiddle.columnconfigure(0, minsize=WIDTH//2-40)
boxmiddle.columnconfigure(2, minsize=WIDTH//2-40)
boxbottom.grid(row=2, padx=5, pady=(0, 10))
for i in range(5):
boxmiddle.columnconfigure(i, minsize=100)

tk.Label(boxtop, text="This program lets you copy and edit Stable Diffusion Generation data between images.").pack()
tk.Button(boxmiddle, text="Copy from image", command=select_source_file).grid(row=0, column=0)
tk.Button(boxmiddle, text="Select target image", command=select_target_file).grid(row=0, column=2)
tk.Button(boxmiddle, text="Copy to target", command=copy_png_info).grid(row=2, column=1)
tk.Button(boxmiddle, text="Copy from image", command=select_source_file).grid(row=0, column=1)
tk.Button(boxmiddle, text="Select target image", command=select_target_file).grid(row=0, column=3)
source_label = tk.Label(boxmiddle, text="No file selected.")
target_label = tk.Label(boxmiddle, text="No file selected.")
source_label.grid(row=1, column=0)
target_label.grid(row=1, column=2)
scrollbar = tk.Scrollbar(boxbottom, orient='vertical')
scrollbar.pack(side="right", fill='y')
textbox = tk.Text(boxbottom, width=80, height=15, font=("Lucida Sans Typewriter",10), yscrollcommand=scrollbar.set)
source_label.grid(row=1, column=1)
target_label.grid(row=1, column=3)

tk.Button(boxbottom, text="Copy to target", command=copy_png_info).pack(side=tk.TOP, pady=5)
scrollbar = tk.Scrollbar(boxbottom, orient="vertical")
textbox = tk.Text(boxbottom, font=("Lucida Sans Typewriter", 10), yscrollcommand=scrollbar.set)
scrollbar.config(command=textbox.yview)
textbox.pack()
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
textbox.pack(expand=True, fill=tk.BOTH)

textbox.bind("<KeyRelease>", apply_color)
for tag in tags:
Expand Down

0 comments on commit a194628

Please sign in to comment.