diff --git a/UI/arrow.png b/UI/arrow.png new file mode 100644 index 0000000..3a53219 Binary files /dev/null and b/UI/arrow.png differ diff --git a/UI/background.png b/UI/background.png new file mode 100644 index 0000000..d4da235 Binary files /dev/null and b/UI/background.png differ diff --git a/UI/decoder.py b/UI/decoder.py new file mode 100644 index 0000000..c00bbad --- /dev/null +++ b/UI/decoder.py @@ -0,0 +1,50 @@ +import socket +import time +from io import BytesIO + +import matplotlib.pyplot as plt +from PIL import Image + +def receive_image_client(server_ip, server_port): + client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + client_socket.connect((server_ip, server_port)) + + start_time = time.time() + + image_data = b"" + while True: + chunk = client_socket.recv(1024) + if not chunk: + break + image_data += chunk + + end_time = time.time() + # 5 spaces(tab characters to separate image data from the start_time engraved in image) + + start_time = (image_data.split(b" ")[1]).decode() + image_data = image_data.split(b" ")[0] + + # Transfer time is calculated here + transfer_time = end_time - float(start_time) + received_encoded_output = BytesIO(image_data) + + print(f"Image received successfully in {transfer_time} seconds") + + + client_socket.close() + display_received_image(image_data) + + + +def display_received_image(image_data): + image = Image.open(BytesIO(image_data)) + plt.imshow(image) + plt.title("Received Image") + plt.show() + + +if __name__ == "__main__": + server_ip = "10.50.25.126" # Keep the server IP here + server_port = 55555 # Random port which is free at any time + + receive_image_client(server_ip, server_port) \ No newline at end of file diff --git a/UI/encoder.py b/UI/encoder.py new file mode 100644 index 0000000..60a5fbd --- /dev/null +++ b/UI/encoder.py @@ -0,0 +1,46 @@ +import socket +import time +import cv2 + +def send_image_server(ip, port, image_path): + server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + server_socket.bind((ip, port)) + server_socket.listen(1) + + print(f"Server listening on {ip}:{port}") + + while True: + data_connection, address = server_socket.accept() + print(f"Connection from {address}") + + # Open the image using cv2 and resize to 256x256 + try: + image = cv2.imread(image_path) + resized_image = cv2.resize(image, (256, 256)) # Use cv2 for resizing + + # Convert to RGB format for byte conversion (if necessary) + if resized_image.shape[2] == 3: # Already RGB + resized_image_data = resized_image.tobytes() + else: # Convert to RGB + resized_image_data = cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB).tobytes() + + except FileNotFoundError: + print(f"Error: File '{image_path}' not found.") + data_connection.close() + continue + + # Combine image data with start time + start_time = time.time() + image_data_with_time = resized_image_data + b" " + (str(start_time)).encode() + + + # Send the combined data directly + data_connection.sendall(image_data_with_time) + data_connection.close() + +if __name__ == "__main__": + server_ip = '10.50.25.126' + server_port = 55555 + image_path = "./Lake.jpg" + + send_image_server(server_ip, server_port, image_path) \ No newline at end of file diff --git a/UI/icon.png b/UI/icon.png new file mode 100644 index 0000000..57a4ee8 Binary files /dev/null and b/UI/icon.png differ diff --git a/UI/id.png b/UI/id.png new file mode 100644 index 0000000..61022b8 Binary files /dev/null and b/UI/id.png differ diff --git a/UI/main.py b/UI/main.py new file mode 100644 index 0000000..a900a25 --- /dev/null +++ b/UI/main.py @@ -0,0 +1,133 @@ +from tkinter import * +import socket +from tkinter import filedialog +from tkinter import messagebox +import os + +import encoder +from encoder import send_image_server + +import decoder +from decoder import receive_image_client + +root = Tk() +root.title("Auto Encoded File Transfer") +root.geometry("450x560+500+200") +root.configure(bg = "#f4fdfe") +root.resizable(False,False) + +def Send(): + window=Toplevel(root) + window.title("Send") + window.geometry("450x560+500+200") + window.configure(bg="#f4fdfe") + window.resizable(False,False) + + def select_file(): + global filename + filename = filedialog.askopenfilename(initialdir=os.getcwd(), + title='Select file', + filetypes = (('file_type','*.txt'),('all files','*.*'))) + + def sender(): + s=socket.socket() + host=socket.gethostname() + port=12345 + s.bind((host,port)) + s.listen(1) + print(host) + print('Waiting for incoming connections...') + conn,addr=s.accept() + file=open(filename,'rb') + file_data=file.read(4096) + send_image_server(file_data) + conn.send(file_data) + print("File has been transferred successfully") + + #icon + image_icon1 = PhotoImage(file = "/home/eshaiyer/tkinter/send.png") + window.iconphoto(False,image_icon1) + + Sbackground = PhotoImage(file = "/home/eshaiyer/tkinter/sender.png") + Label(window,image=Sbackground).place(x=2,y=0) + + Mbackground = PhotoImage(file = "/home/eshaiyer/tkinter/id.png") + Label(window,image=Mbackground,bg="#f4fdfe").place(x=100,y=260) + + host=socket.gethostname() + Label(window,text=f'ID:{host}',bg='white',fg='black').place(x=140,y=290) + + Button(window,text="+ select file",width=10,height=1,font='arial 14 bold',bg="#fff",fg="#000",command = select_file).place(x=160,y=150) + Button(window,text="SEND",width=8,height=1,font='arial 14 bold',bg='#000',fg='#fff',command = sender).place(x=300,y=150) + + window.mainloop() + +def Receive(): + main=Toplevel(root) + main.title("Receive") + main.geometry("450x560+500+200") + main.configure(bg="#f4fdfe") + main.resizable(False,False) + + def receiver(): + ID=SenderID.get() + filename1=incoming_file.get() + + s=socket.socket() + port=12345 + s.connect((ID,port)) + file=open(filename1,'wb') + file_data=s.recv(4096) + receive_image_client(file_data) + file.write(file_data) + file.close() + print("File has been received successfully") + + #icon + image_icon1 = PhotoImage(file = "/home/eshaiyer/tkinter/receive.png") + main.iconphoto(False,image_icon1) + + Hbackground=PhotoImage(file = "/home/eshaiyer/tkinter/receiver.png") + Label(main,image=Hbackground).place(x=-2,y=0) + + Label(main,text="Receive",font=('arial',20),bg="#f4fdfe").place(x=100,y=280) + + Label(main,text="Enter Sender ID",font=('arial',10,'bold'),bg="#f4fdfe").place(x=20,y=340) + SenderID = Entry(main,width=25,fg="black",border=2,bg='white',font=('arial',15)) + SenderID.place(x=20,y=370) + SenderID.focus() + + Label(main,text="Filename for incoming file",font=('arial',10,'bold'),bg="#f4fdfe").place(x=20,y=420) + incoming_file = Entry(main,width=25,fg="black",border=2,bg='white',font=('arial',15)) + incoming_file.place(x=20,y=450) + + imageicon = PhotoImage(file = "/home/eshaiyer/tkinter/arrow.png") + rr=Button(main,text="Receive",compound=LEFT,image=imageicon,width=130,bg="#39c790",font="arial 14 bold",command=receiver) + rr.place(x=20,y=500) + + main.mainloop() + +#icon +image_icon = PhotoImage(file = "/home/eshaiyer/tkinter/icon.png") +root.iconphoto(False,image_icon) + +Label(root,text="File Transfer",font=('Acumin Variable Concept',20,'bold'),bg="#f4fdfe").place(x=20,y=30) + +Frame(root,width=400,height=2,bg="#f3f5f6").place(x=25,y=80) + +send_image = PhotoImage(file = "/home/eshaiyer/tkinter/send.png") +send = Button(root,image = send_image,bg = "#f4fdfe", bd = 0,command = Send) +send.place(x=50,y=100) + +receive_image = PhotoImage(file = "/home/eshaiyer/tkinter/receive.png") +receive = Button(root,image = receive_image,bg = "#f4fdfe", bd = 0, command = Receive) +receive.place(x=300,y=100) + +#label +Label(root,text="Send",font=('Acumin Variable Concept',17,'bold'),bg = "#f4fdfe").place(x=65,y=200) +Label(root,text="Receive",font=('Acumin Variable Concept',17,'bold'),bg = "#f4fdfe").place(x=300,y=200) + +background = PhotoImage(file = "/home/eshaiyer/tkinter/background.png") +Label(root,image=background).place(x=2,y=323) + +root.mainloop() diff --git a/UI/receive.png b/UI/receive.png new file mode 100644 index 0000000..578d08c Binary files /dev/null and b/UI/receive.png differ diff --git a/UI/receiver.png b/UI/receiver.png new file mode 100644 index 0000000..b410c17 Binary files /dev/null and b/UI/receiver.png differ diff --git a/UI/send.png b/UI/send.png new file mode 100644 index 0000000..ecab818 Binary files /dev/null and b/UI/send.png differ diff --git a/UI/sender.png b/UI/sender.png new file mode 100644 index 0000000..57155ff Binary files /dev/null and b/UI/sender.png differ diff --git a/decoder.py b/decoder.py new file mode 100644 index 0000000..c00bbad --- /dev/null +++ b/decoder.py @@ -0,0 +1,50 @@ +import socket +import time +from io import BytesIO + +import matplotlib.pyplot as plt +from PIL import Image + +def receive_image_client(server_ip, server_port): + client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + client_socket.connect((server_ip, server_port)) + + start_time = time.time() + + image_data = b"" + while True: + chunk = client_socket.recv(1024) + if not chunk: + break + image_data += chunk + + end_time = time.time() + # 5 spaces(tab characters to separate image data from the start_time engraved in image) + + start_time = (image_data.split(b" ")[1]).decode() + image_data = image_data.split(b" ")[0] + + # Transfer time is calculated here + transfer_time = end_time - float(start_time) + received_encoded_output = BytesIO(image_data) + + print(f"Image received successfully in {transfer_time} seconds") + + + client_socket.close() + display_received_image(image_data) + + + +def display_received_image(image_data): + image = Image.open(BytesIO(image_data)) + plt.imshow(image) + plt.title("Received Image") + plt.show() + + +if __name__ == "__main__": + server_ip = "10.50.25.126" # Keep the server IP here + server_port = 55555 # Random port which is free at any time + + receive_image_client(server_ip, server_port) \ No newline at end of file diff --git a/encoder.py b/encoder.py new file mode 100644 index 0000000..60a5fbd --- /dev/null +++ b/encoder.py @@ -0,0 +1,46 @@ +import socket +import time +import cv2 + +def send_image_server(ip, port, image_path): + server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + server_socket.bind((ip, port)) + server_socket.listen(1) + + print(f"Server listening on {ip}:{port}") + + while True: + data_connection, address = server_socket.accept() + print(f"Connection from {address}") + + # Open the image using cv2 and resize to 256x256 + try: + image = cv2.imread(image_path) + resized_image = cv2.resize(image, (256, 256)) # Use cv2 for resizing + + # Convert to RGB format for byte conversion (if necessary) + if resized_image.shape[2] == 3: # Already RGB + resized_image_data = resized_image.tobytes() + else: # Convert to RGB + resized_image_data = cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB).tobytes() + + except FileNotFoundError: + print(f"Error: File '{image_path}' not found.") + data_connection.close() + continue + + # Combine image data with start time + start_time = time.time() + image_data_with_time = resized_image_data + b" " + (str(start_time)).encode() + + + # Send the combined data directly + data_connection.sendall(image_data_with_time) + data_connection.close() + +if __name__ == "__main__": + server_ip = '10.50.25.126' + server_port = 55555 + image_path = "./Lake.jpg" + + send_image_server(server_ip, server_port, image_path) \ No newline at end of file diff --git a/receiver.py b/receiver.py new file mode 100644 index 0000000..7ae4f94 --- /dev/null +++ b/receiver.py @@ -0,0 +1,40 @@ +import tkinter as tk +from tkinter import filedialog +import socket +import os +import decoder +from decoder import receive_image_client + +def receive_file(): + host = '0.0.0.0' + port = 5555 + + server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + server_socket.bind((host, port)) + server_socket.listen(1) + + conn, addr = server_socket.accept() + + data = conn.recv(1024).decode() + file_name, file_size = data.split(':') + + file_path = filedialog.asksaveasfilename(defaultextension='.*', initialfile=file_name) + if file_path: + with open(file_path, 'wb') as file: + while True: + file_data = conn.recv(1024) + if not file_data: + break + file.write(file_data) + + conn.close() + server_socket.close() + print('File received successfully') + +root = tk.Tk() +root.title('File Receiver') + +receive_file_button = tk.Button(root, text='Receive File', command=receive_file) +receive_file_button.pack(pady=20) + +root.mainloop() diff --git a/sender.py b/sender.py new file mode 100644 index 0000000..c7f89d7 --- /dev/null +++ b/sender.py @@ -0,0 +1,48 @@ +import tkinter as tk +from tkinter import filedialog +import socket +import os +import sys +sys.path.insert(0, '/home/eshaiyer/tkinter/encoder.py') +import encoder as en +from en import send_image_server + +def send_file(file_path): + host = '127.0.0.1' # Receiver's IP address + port = 5555 + + client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + client_socket.connect((host, port)) + + with open(file_path, 'rb') as file: + file_data = file.read() + file_name = os.path.basename(file_path) + file_size = len(file_data) + + client_socket.send(f"{file_name}:{file_size}".encode()) + client_socket.sendall(file_data) + + client_socket.close() + print('File sent successfully') + +def select_file_and_preview(): + file_path = filedialog.askopenfilename() + if file_path: + with open(file_path, 'rb') as file: + file_data = file.read() + file_preview.config(text=f"Selected File: {os.path.basename(file_path)}") + send_button.config(state=tk.NORMAL, command=lambda: send_file(file_path)) + +root = tk.Tk() +root.title('File Sender') + +select_button = tk.Button(root, text='Select File', command=select_file_and_preview) +select_button.pack(pady=20) + +file_preview = tk.Label(root, text='No file selected') +file_preview.pack(pady=20) + +send_button = tk.Button(root, text='Send File', state=tk.DISABLED) +send_button.pack(pady=20) + +root.mainloop()