Advertisement
Najeebsk

AI-GENERATE2.pyw

May 4th, 2024
1,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.50 KB | None | 0 0
  1. import os
  2. import numpy as np
  3. from imageio.v2 import imread
  4. import tkinter as tk
  5. from tkinter import filedialog
  6. from PIL import Image, ImageTk
  7.  
  8. header_len = 4 * 8  # uint32 bit length
  9.  
  10. def read_image(img_path):
  11.     img = np.array(imread(img_path), dtype=np.uint8)
  12.     orig_shape = img.shape
  13.     return img.flatten(), orig_shape
  14.  
  15. def read_file(file_path):
  16.     file_bytes = open(file_path, "rb").read()
  17.     return np.unpackbits(file_bytes)
  18.  
  19. def decode_data(encoded_data):
  20.     out_mask = np.ones_like(encoded_data)
  21.     output = np.bitwise_and(encoded_data, out_mask)
  22.     return output
  23.  
  24. def write_file(file_path, file_bit_array):
  25.     bytes_data = np.packbits(file_bit_array)
  26.     with open(file_path, 'wb') as f:
  27.         f.write(bytes_data)
  28.  
  29. def browse_file():
  30.     filename = filedialog.askopenfilename(initialdir="/", title="Select Picture File")
  31.     original_entry_unhide.delete(0, tk.END)
  32.     original_entry_unhide.insert(0, filename)
  33.  
  34. def browse_save_location():
  35.     save_location = filedialog.asksaveasfilename(initialdir="/", title="Select Save Location", defaultextension=".jpg")
  36.     save_entry_unhide.delete(0, tk.END)
  37.     save_entry_unhide.insert(0, save_location)
  38.  
  39. def unhide_images():
  40.     original_file = original_entry_unhide.get()
  41.     save_file = save_entry_unhide.get()
  42.     if not os.path.isfile(original_file):
  43.         print("Image file does not exist")
  44.         return
  45.  
  46.     encoded_data, shape_orig = read_image(original_file)
  47.     data = decode_data(encoded_data)
  48.     el_array = np.packbits(data[:header_len])
  49.     extracted_len = el_array.view(np.uint32)[0]
  50.     data = data[header_len:extracted_len+header_len]
  51.     write_file(save_file, data)
  52.     print("Image decoded and saved")
  53.  
  54.     # Load and display the original image
  55.     original_image = Image.open(original_file)
  56.     original_image.thumbnail((shape_orig[0] // 3, shape_orig[1]))  # Resize if needed
  57.     original_photo = ImageTk.PhotoImage(original_image)
  58.     lbl.config(image=original_photo)
  59.     lbl.image = original_photo
  60.  
  61.     # Load and display the saved image
  62.     saved_image = Image.open(save_file)
  63.     saved_image.thumbnail((shape_orig[0] // 3, shape_orig[1]))  # Resize if needed
  64.     saved_photo = ImageTk.PhotoImage(saved_image)
  65.     lbl2.config(image=saved_photo)
  66.     lbl2.image = saved_photo
  67.  
  68. root = tk.Tk()
  69. root.geometry("1000x660")
  70. root.title("Najeeb Generate AI Images")
  71.  
  72. # Input for Picture Selection
  73. tk.Label(root, text="Select Picture File:").place(x=10, y=10)
  74. original_entry_unhide = tk.Entry(root)
  75. original_entry_unhide.place(x=140, y=10)
  76. browse_button = tk.Button(root, text="Browse", command=browse_file)
  77. browse_button.place(x=330, y=8)
  78.  
  79. # Input for Save Location
  80. tk.Label(root, text="Select Save Location:").place(x=10, y=40)
  81. save_entry_unhide = tk.Entry(root)
  82. save_entry_unhide.place(x=140, y=40)
  83. browse_save_button = tk.Button(root, text="Browse", command=browse_save_location)
  84. browse_save_button.place(x=330, y=38)
  85.  
  86. # Button to unhide images
  87. unhide_button = tk.Button(root, text="Generate AI Image", command=unhide_images, bg="#6699FF", fg="white")
  88. unhide_button.place(x=600, y=8)
  89.  
  90. # Frame for Original Image
  91. f = tk.Frame(root, bd=3, bg="#2c3e50", width=500, height=640, relief=tk.GROOVE)
  92. f.place(x=5, y=70)
  93.  
  94. lbl = tk.Label(f, bg="#2c3e50")
  95. lbl.place(x=10, y=140)
  96.  
  97. # Frame for Generated AI Image
  98. frame2 = tk.Frame(root, bd=3, width=500, height=640, relief=tk.GROOVE, bg="#34495e")
  99. frame2.place(x=505, y=70)
  100.  
  101. lbl2 = tk.Label(frame2, bg="#34495e")
  102. lbl2.place(x=10, y=100)
  103.  
  104. root.mainloop()
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement