SHARE
    TWEET
    Najeebsk

    AI-GENERATE5.0.pyw

    Oct 7th, 2024 (edited)
    276
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    Python 6.46 KB | None | 0 0
    1. import os
    2. import numpy as np
    3. from imageio.v2 import imread, imwrite
    4. import tkinter as tk
    5. from tkinter import filedialog
    6. from PIL import Image, ImageTk
    7. max_value = 255 # max uint value per pixel per channel
    8. header_len = 4 * 8 # uint32 bit length
    9. def read_image(img_path):
    10. img = np.array(imread(img_path), dtype=np.uint8)
    11. orig_shape = img.shape
    12. return img.flatten(), orig_shape
    13. def write_image(img_path, img_data, shape):
    14. img_data = np.reshape(img_data, shape)
    15. imwrite(img_path, img_data)
    16. def bytes2array(byte_data):
    17. byte_array = np.frombuffer(byte_data, dtype=np.uint8)
    18. return np.unpackbits(byte_array)
    19. def array2bytes(bit_array):
    20. byte_array = np.packbits(bit_array)
    21. return byte_array.tobytes()
    22. def read_file(file_path):
    23. file_bytes = open(file_path, "rb").read()
    24. return bytes2array(file_bytes)
    25. def write_file(file_path, file_bit_array):
    26. bytes_data = array2bytes(file_bit_array)
    27. with open(file_path, 'wb') as f:
    28. f.write(bytes_data)
    29. def encode_data(image, file_data):
    30. or_mask = file_data
    31. and_mask = np.zeros_like(or_mask)
    32. and_mask = (and_mask + max_value - 1) + or_mask
    33. res = np.bitwise_or(image, or_mask)
    34. res = np.bitwise_and(res, and_mask)
    35. return res
    36. def decode_data(encoded_data):
    37. out_mask = np.ones_like(encoded_data)
    38. output = np.bitwise_and(encoded_data, out_mask)
    39. return output
    40. def update_zoom(event=None):
    41. if 'original_image' not in globals():
    42. print("No image loaded to zoom")
    43. return
    44. zoom_value = zoom_slider.get() / 100 # Scale the value down to a fraction
    45. resized_image = original_image.resize((int(original_image.width * zoom_value), int(original_image.height * zoom_value)))
    46. zoomed_photo = ImageTk.PhotoImage(resized_image)
    47. image_label.config(image=zoomed_photo)
    48. image_label.image = zoomed_photo
    49. canvas.config(scrollregion=canvas.bbox(tk.ALL))
    50. def unhide_images():
    51. global original_image # Ensure this is a global variable
    52. original_file = original_entry_unhide.get()
    53. save_file = save_file_entry_unhide.get()
    54. if not os.path.isfile(original_file):
    55. print("Image file does not exist")
    56. return
    57. # Read and decode the encoded image data
    58. encoded_data, shape_orig = read_image(original_file)
    59. data = decode_data(encoded_data)
    60. # Extract the length of the hidden file from the header
    61. el_array = np.packbits(data[:header_len])
    62. extracted_len = el_array.view(np.uint32)[0]
    63. # Extract the hidden file data based on the header length
    64. data = data[header_len:extracted_len+header_len]
    65. # Write the hidden file to the specified save location (as .jpg)
    66. save_file = save_file.replace('.jpg', '.png') # Force saving as .jpg
    67. write_file(save_file, data)
    68. print(f"File extracted and saved as {save_file}")
    69. # Load and display the original image
    70. original_image = Image.open(save_file)
    71. resized_image = original_image.resize((200, 400)) # First-time view as 200x400
    72. photo = ImageTk.PhotoImage(resized_image)
    73. # Update label and configure scroll region
    74. image_label.config(image=photo)
    75. image_label.image = photo
    76. canvas.config(scrollregion=canvas.bbox(tk.ALL))
    77. zoom_slider.set(30) # Reset the zoom slider to default
    78. # Create the root window
    79. root = tk.Tk()
    80. root.geometry("1000x700")
    81. root.title("Najeeb AI Generator Image Tool")
    82. root.configure(bg="#282c34")
    83. # Grid configuration
    84. root.grid_rowconfigure(2, weight=1)
    85. root.grid_columnconfigure(0, weight=1)
    86. root.grid_columnconfigure(1, weight=1)
    87. root.grid_columnconfigure(2, weight=4) # Make column 2 wider for the canvas and zoom slider
    88. # Create a frame for the left side (fields and buttons)
    89. left_frame = tk.Frame(root, bg="#282c34")
    90. left_frame.grid(row=0, column=0, padx=5, pady=5, sticky='nsew')
    91. # Styling for labels and buttons
    92. label_font = ('Arial', 12, 'bold')
    93. entry_font = ('Arial', 11)
    94. button_font = ('Arial', 12, 'bold')
    95. button_color = "#61afef"
    96. button_fg = "white"
    97. # Input for Encoded Image to unhide from
    98. tk.Label(left_frame, text="Select AI Image", font=label_font, bg="#282c34", fg="white").grid(row=0, column=0, sticky="e", padx=5)
    99. original_entry_unhide = tk.Entry(left_frame, width=20, font=entry_font)
    100. original_entry_unhide.grid(row=0, column=1, padx=5, pady=5)
    101. browse_button_unhide_image = tk.Button(left_frame, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: original_entry_unhide.insert(0, filedialog.askopenfilename()))
    102. browse_button_unhide_image.grid(row=0, column=2, padx=5, pady=5)
    103. # Input for Save Location of extracted file
    104. tk.Label(left_frame, text="Save AI(File.png):", font=label_font, bg="#282c34", fg="white").grid(row=0, column=3, sticky="e", padx=10)
    105. save_file_entry_unhide = tk.Entry(left_frame, width=20, font=entry_font)
    106. save_file_entry_unhide.grid(row=0, column=4, padx=5, pady=5)
    107. save_button_unhide_file = tk.Button(left_frame, text="Save As", font=button_font, bg=button_color, fg=button_fg, command=lambda: save_file_entry_unhide.insert(0, filedialog.asksaveasfilename()))
    108. save_button_unhide_file.grid(row=0, column=5, padx=10, pady=5)
    109. # Button for decoding
    110. unhide_button = tk.Button(left_frame, text="Generate AI Image", font=button_font, bg="#98c379", fg="white", command=unhide_images)
    111. unhide_button.grid(row=0, column=6, columnspan=3, padx=10, pady=5)
    112. # Create a canvas for image display with scrollbar
    113. canvas = tk.Canvas(root, bg="#000000")
    114. canvas.grid(row=2, column=0, columnspan=3, sticky="nsew")
    115. # Add vertical and horizontal scrollbars
    116. vertical_scroll = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
    117. vertical_scroll.grid(row=2, column=3, sticky="ns")
    118. canvas.configure(yscrollcommand=vertical_scroll.set)
    119. horizontal_scroll = tk.Scrollbar(root, orient="horizontal", command=canvas.xview)
    120. horizontal_scroll.grid(row=3, column=0, columnspan=3, sticky="ew")
    121. canvas.configure(xscrollcommand=horizontal_scroll.set)
    122. # Create an image label on the canvas
    123. image_label = tk.Label(canvas, bg="#000000")
    124. canvas.create_window((0, 0), window=image_label, anchor='nw')
    125. # Zoom slider
    126. zoom_slider = tk.Scale(root, from_=10, to=200, bg="lightgreen", orient='horizontal', command=update_zoom)
    127. zoom_slider.set(100) # Set default zoom level to 100%
    128. zoom_slider.grid(row=1, column=0, columnspan=3, sticky='ew', padx=10)
    129. # Set initial scroll region
    130. canvas.config(scrollregion=canvas.bbox(tk.ALL))
    131. # Run the application
    132. root.mainloop()
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /