-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 48373a0
Showing
4 changed files
with
415 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import tkinter as tk | ||
import socket | ||
import threading | ||
|
||
PORT = 12345 | ||
|
||
USAGE="""USAGE: To connect to existing server input IP-Address on the right | ||
USAGE: To start a server click on \"Listen\" Button""" | ||
|
||
class Connector(tk.Frame): | ||
def __init__(self, master): | ||
print(USAGE) | ||
super().__init__(master) | ||
|
||
self.master = master | ||
self.successful = False | ||
self.in_mainloop = True | ||
self.frm_connect = tk.Frame(self) | ||
self.frm_connect.columnconfigure(1, minsize=100) | ||
self.btn_connect = tk.Button(self.frm_connect, text="Connect", command=lambda: self.start(self.connect)) | ||
self.ent_ip = tk.Entry(self.frm_connect) | ||
self.btn_connect.grid(row=0, column=0, padx=5, pady=2.5) | ||
self.ent_ip.grid(row=0, column=1, padx=5) | ||
|
||
self.rowconfigure([0, 1], minsize=30) | ||
self.frm_connect.grid(row=0, columnspan=2) | ||
self.btn_listen = tk.Button(self, text="Listen", command=lambda: self.start(self.listen)) | ||
self.btn_listen.grid(row=1, column=0, padx=5, pady=2.5, sticky="w") | ||
self.btn_cancel = tk.Button(self, text="Cancel", command=self.stop) | ||
self.btn_cancel.grid(row=1, column=1, sticky="w") | ||
self.btn_cancel.grid_remove() | ||
|
||
self.thread = threading.Thread(target=lambda: "dummy") | ||
|
||
def enable_buttons(self): | ||
self.btn_connect["state"] = "active" | ||
self.btn_listen["state"] = "active" | ||
|
||
def disable_buttons(self): | ||
self.btn_connect["state"] = "disable" | ||
self.btn_listen["state"] = "disable" | ||
|
||
def start(self, target): | ||
self.disable_buttons() | ||
self.btn_cancel.grid() | ||
self.thread = threading.Thread(target=target) | ||
self.thread.start() | ||
|
||
def stop(self): | ||
if self.in_mainloop: | ||
self.enable_buttons() | ||
self.btn_cancel.grid_remove() | ||
self.sock.close() | ||
|
||
def listen(self): | ||
print(f"STATUS: Listening on port {PORT}") | ||
self.sock = socket.socket() | ||
try: | ||
self.sock.bind(("", PORT)) | ||
self.sock.listen(1) | ||
self.conn, addr = self.sock.accept() | ||
except OSError as e: | ||
print(e) | ||
self.stop() | ||
return | ||
print(f"STATUS: Got connection from {addr[0]}") | ||
self.successful = True | ||
self.role = "server" | ||
self.master.quit() | ||
|
||
def connect(self): | ||
self.sock = socket.socket() | ||
ip = self.ent_ip.get() | ||
#[DEBUG] | ||
if not ip: ip = "localhost" | ||
print(f"STATUS: Connecting to {ip}") | ||
try: self.sock.connect((ip, PORT)) | ||
except OSError as e: | ||
self.stop() | ||
print(e) | ||
return | ||
print("STATUS: Connection successful") | ||
self.successful = True | ||
self.conn = self.sock | ||
self.role = "client" | ||
self.master.quit() | ||
|
||
def cleanup(self): | ||
self.in_mainloop = False | ||
if self.thread.is_alive(): self.sock.close() | ||
|
||
def is_successful(self): | ||
return self.successful | ||
|
||
def get_results(self): | ||
return self.conn, self.role | ||
|
||
if __name__ == "__main__": | ||
connector = Connector() | ||
connector.mainloop() | ||
if connector.successful: | ||
connection = connector.conn | ||
role = connector.role | ||
connector.destroy() | ||
#WTF window has to be destroyed before communication otherwise very laggy | ||
if role == "server": | ||
connection.send(b"Hello from the server side!") | ||
print(connection.recv(50).decode()) | ||
elif role == "client": | ||
print(connection.recv(50).decode()) | ||
connection.send(b"Hello from the client side!") | ||
input("close...") | ||
connection.close() | ||
elif connector.thread.is_alive(): connector.sock.close() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import tkinter as tk | ||
from PIL import ImageTk, Image, ImageFilter, ImageDraw | ||
|
||
USAGE = """USAGE: Mousewheel Up = Blur+ | ||
USAGE: Mousewheel Down = Blur- | ||
USAGE: Mousebutton Left = Create Rectangle (Drag) | ||
USAGE: Mousebutton Right = Delete Rectangle (Click) | ||
USAGE: Enter = Confirm and Exchange""" | ||
|
||
class Editor(tk.Canvas): | ||
def __init__(self, master, filename, **kwargs): | ||
print(USAGE) | ||
self.successful = False | ||
self.master = master | ||
self.rects = {} | ||
self.current_rect = -1 | ||
self.blur = 0.0 | ||
self.image = Image.open(filename) | ||
self.blurred = self.image.copy() | ||
self.image_gui = ImageTk.PhotoImage(self.blurred) | ||
kwargs["width"] = self.image_gui.width() | ||
kwargs["height"] = self.image_gui.height() | ||
super().__init__(master, **kwargs) | ||
self.image_id = self.create_image(0, 0, image=self.image_gui, anchor="nw") | ||
self.bind("<MouseWheel>", self.change_blur) | ||
self.bind("<Button-1>", self.new_rect) | ||
self.bind("<B1-Motion>", self.resize_rect) | ||
self.bind("<ButtonRelease-1>", self.create_rect) | ||
self.bind("<Return>", self.finish) | ||
self.focus_force() | ||
|
||
def change_blur(self, event): | ||
self.blur += 0.5 * (event.delta / 120) | ||
self.blur = max(0.0, self.blur) | ||
tmp = self.blurred | ||
self.blurred = self.image.filter(ImageFilter.GaussianBlur(self.blur)) | ||
tmp.close() | ||
self.image_gui = ImageTk.PhotoImage(self.blurred) | ||
self.itemconfigure(self.image_id, image=self.image_gui) | ||
|
||
def new_rect(self, event): | ||
coords = [event.x, event.y, event.x, event.y] | ||
self.current_rect = self.create_rectangle(*coords) | ||
self.rects[self.current_rect] = coords | ||
|
||
|
||
def resize_rect(self, event): | ||
self.rects[self.current_rect][2] = event.x | ||
self.rects[self.current_rect][3] = event.y | ||
self.coords(self.current_rect, *self.rects[self.current_rect]) | ||
|
||
def create_rect(self, _): | ||
def make_deleter(handle): | ||
return lambda _: self.delete_rect(handle) | ||
self.itemconfigure(self.current_rect, fill="BLACK") | ||
self.tag_bind(self.current_rect, "<Button-3>", make_deleter(self.current_rect)) | ||
|
||
def delete_rect(self, rect): | ||
self.delete(rect) | ||
del self.rects[rect] | ||
|
||
def finish(self, _): | ||
print("STATUS: Image locked and sent") | ||
self.successful = True | ||
self.master.quit() | ||
|
||
def cleanup(self): | ||
self.image.close() | ||
self.blurred.close() | ||
|
||
def is_successful(self): | ||
return self.successful | ||
|
||
def get_results(self): | ||
imgs_with_rects = [self.blurred, self.image.copy()] | ||
for img in imgs_with_rects: | ||
for rect in self.rects.values(): | ||
draw = ImageDraw.Draw(img) | ||
draw.rectangle(rect, "BLACK", "BLACK") | ||
return imgs_with_rects[0], imgs_with_rects[1], self.image | ||
|
||
if __name__ == "__main__": | ||
window = tk.Tk() | ||
editor = Editor(window, "test.png") | ||
editor.pack() | ||
window.mainloop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from os import system | ||
import tkinter as tk | ||
from tkinter.filedialog import askopenfilename, asksaveasfilename | ||
from connector import Connector | ||
from editor import Editor | ||
from verifier import Verifier | ||
|
||
FILETYPES = [("Image Files", (".png", ".jpg", ".jpeg"))] | ||
|
||
def main(): | ||
window = tk.Tk() | ||
connector = Connector(window) | ||
connector.pack() | ||
window.mainloop() | ||
if not connector.is_successful(): | ||
# triggered only when exiting forcefully | ||
connector.cleanup() | ||
return | ||
conn, role = connector.get_results() | ||
connector.destroy() | ||
|
||
while True: | ||
filename = askopenfilename(parent=window, filetypes=FILETYPES) | ||
if not filename: | ||
conn.close() | ||
return | ||
editor = Editor(window, filename) | ||
editor.pack() | ||
window.mainloop() | ||
if not editor.is_successful(): | ||
conn.close() | ||
editor.cleanup() | ||
return | ||
images = editor.get_results() | ||
editor.destroy() | ||
|
||
verifier = Verifier(window, conn, role, images) | ||
if verifier.has_paniced(): | ||
verifier.cleanup() | ||
return | ||
verifier.pack() | ||
window.mainloop() | ||
if not verifier.is_successful(): | ||
verifier.cleanup() | ||
return | ||
image = verifier.get_results() | ||
|
||
filename = asksaveasfilename(parent=window, filetypes=[("Image Files", ".png")]) | ||
if filename: | ||
if not ".png" in filename: filename += ".png" | ||
image.save(filename) | ||
|
||
if not verifier.ask_resume(): | ||
print("INFO: One party wanted to stop") | ||
verifier.cleanup() | ||
break | ||
|
||
for img in images: | ||
img.close() | ||
verifier.destroy() | ||
system("cls") | ||
|
||
window.destroy() | ||
|
||
|
||
if __name__ == "__main__": main() |
Oops, something went wrong.