From 93e6b7bad9b4b57a102eeba1c746f39cdd640a35 Mon Sep 17 00:00:00 2001 From: royalmustard <36129876+royalmustard@users.noreply.github.com> Date: Sat, 23 Nov 2019 08:19:25 +0100 Subject: [PATCH] INIT --- Converter.py | 46 +++++++++++++++++++++++ UI.py | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++ __main__.py | 14 +++++++ 3 files changed, 163 insertions(+) create mode 100644 Converter.py create mode 100644 UI.py create mode 100644 __main__.py diff --git a/Converter.py b/Converter.py new file mode 100644 index 0000000..76bb849 --- /dev/null +++ b/Converter.py @@ -0,0 +1,46 @@ +from PIL import Image +import time + +class Converter: + + def __init__(self): + self.image = None + self.name = "" + self.grayscale = None + self.final = None + self.threshold = 0 + self.bg_color = (0, 0, 0, 0) + + def load_image(self, filename): + self.image = Image.open(filename) + self.name = filename.split(".")[0] + self.grayscale = self.image.convert("L") + + def convert_image_to_png(self): + gs = self.grayscale + th = self.threshold + im = self.image + width, height = self.image.size + new_image = Image.new("RGBA", (width, height), color=self.bg_color) + start = time.time() + for x in range(width): + for y in range(height): + grey = gs.getpixel((x, y)) + if im.mode in ("L", "P", "RGBA"): + im = im.convert("RGB") + r, g, b = im.getpixel((x, y)) + if grey < 255 - th: + new_image.putpixel((x, y), (r, g, b, 255)) + #arr = np.array(gs) + #ima = np.array(im) + #print(ima[150][0][1]) + #RGB = np.where(arr < 255 - th, ima, 0) + #A = np.where(arr < 255 - th, 255, 0) + #pixels = np.dstack((RGB, A)) + #new_image = Image.fromarray(np.uint8(pixels)) + end = time.time() + print(end-start) + self.final = new_image + + def save_image(self, filename): + self.final.save(filename) diff --git a/UI.py b/UI.py new file mode 100644 index 0000000..26326be --- /dev/null +++ b/UI.py @@ -0,0 +1,103 @@ +from tkinter import filedialog as df +import Converter +from tkinter import * +from tkinter import ttk +from tkinter.colorchooser import askcolor +from PIL import ImageTk, Image + + +class UI: + + def __init__(self): + self.conv = Converter.Converter() + self.root = Tk() + self.root.title("JPG background remover") + self.frame = Frame(self.root) + self.frame.grid(column=0, row=0, sticky=(N, E, W, S)) + + self.in_filename = StringVar() + self.in_image = None + self.out_filename = StringVar() + self.out_image = None + self.last_out_filename = "" + self.threshold = IntVar() + self.use_color = IntVar() + self.conv_image = None + self.color = (255, 105, 180, 255) + + ttk.Label(self.frame, text="Input file").grid(row=1, column=1, sticky=N) + ttk.Entry(self.frame, textvariable=self.in_filename, width=50).grid(row=3, column=1, sticky=N) + ttk.Button(self.frame, command=self.get_path, text="Open file").grid(row=4, column=1, sticky=N) + self.cnv_in_image = Canvas(self.frame, width=300, height=300) + self.cnv_in_image.grid(row=2, column=1, sticky=N) + + ttk.Label(self.frame, text="Output file").grid(row=1, column=2, sticky=N) + ttk.Entry(self.frame, textvariable=self.out_filename, width=50).grid(row=3, column=2, sticky=N) + ttk.Button(self.frame, command=self.get_output_filename, text="Set output file").grid(row=4, column=2, sticky=N) + self.cnv_out_image = Canvas(self.frame, width=300, height=300) + self.cnv_out_image.grid(row=2, column=2, sticky=N) + + ttk.Label(self.frame, text="Filter threshold").grid(row=5, column=1, sticky=N) + ttk.Scale(self.frame, from_=0, to=254, orient=HORIZONTAL, length=280, variable=self.threshold)\ + .grid(row=5, column=2, sticky=N) + + ttk.Button(self.frame, command=self.convert, text="Convert image").grid(row=6, column=1, sticky=N) + ttk.Button(self.frame, command=self.save, text="Save as png").grid(row=6, column=2, sticky=N) + + ttk.Checkbutton(self.frame, text="Color Background", variable=self.use_color).grid(row=7, column=1, sticky=N) + ttk.Button(self.frame, command=self.get_color, text="Choose Color").grid(row=7, column=2, sticky=N) + + self.frame.mainloop() + + def get_path(self): + self.in_filename.set(df.askopenfilename()) + self.in_image = Image.open(self.in_filename.get()) + self.in_image = self.in_image.resize((300, 300), Image.ANTIALIAS) + self.in_image = ImageTk.PhotoImage(self.in_image) + self.cnv_in_image.create_image((0, 0), anchor=NW, image=self.in_image) + + def get_output_filename(self): + self.out_filename.set(df.asksaveasfilename(defaultextension="png")) + + def convert(self): + self.conv.load_image(self.in_filename.get()) + self.conv.threshold = self.threshold.get() + if self.use_color.get(): + self.conv.bg_color = self.color + else: + self.conv.bg_color = (0, 0, 0, 0) + self.conv.convert_image_to_png() + + self.conv_image = self.conv.final + #width, height = self.conv_image.size + oi = Image.new("RGB", (300, 300), color=self.color) + + conv_im = self.conv_image.resize((300, 300), Image.ANTIALIAS) + for x in range(300): + for y in range(300): + r, g, b, a = conv_im.getpixel((x, y)) + if a != 0: + oi.putpixel((x, y), (r, g, b)) + + self.out_image = oi + self.out_image = ImageTk.PhotoImage(self.out_image) + self.cnv_out_image.create_image((0, 0), anchor=NW, image=self.out_image) + + def save(self): + if self.out_filename.get() == self.last_out_filename: + self.get_output_filename() + """out_filename gets set in get_output filename + We set last_out_filename to avoid overriding the last converted image""" + self.last_out_filename = self.out_filename.get() + self.conv.save_image(self.out_filename.get()) + + def get_color(self): + lcl = askcolor() + if lcl[0] is not None: + lcl = list(lcl[0]) + lcl[0] = int(lcl[0]) + lcl[1] = int(lcl[1]) + lcl[2] = int(lcl[2]) + lcl.append(255) + lcl = tuple(lcl) + self.color = lcl diff --git a/__main__.py b/__main__.py new file mode 100644 index 0000000..49530d8 --- /dev/null +++ b/__main__.py @@ -0,0 +1,14 @@ +import sys +import UI + + +def main(args=None): + """The main routine.""" + if args is None: + args = sys.argv[1:] + + ui = UI.UI() + + +if __name__ == "__main__": + main() \ No newline at end of file