SHARE
    TWEET
    Najeebsk

    AI-GENERATE2.1.pyw

    Sep 29th, 2024
    293
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    Python 8.53 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. # Update the displayed image based on the zoom scale value
    45. zoom_value = zoom_slider.get() / 100 # Scale the value down to a fraction
    46. resized_image = original_image.resize((int(original_image.width * zoom_value), int(original_image.height * zoom_value)))
    47. zoomed_photo = ImageTk.PhotoImage(resized_image)
    48. lbl.config(image=zoomed_photo)
    49. lbl.image = zoomed_photo
    50. def hide_images():
    51. global original_image # Ensure this is a global variable
    52. img_path = original_entry_hide.get()
    53. file_path = hide_file_entry.get()
    54. output_path = save_file_entry_hide.get()
    55. if not os.path.isfile(img_path):
    56. print("Original image file does not exist")
    57. return
    58. if not os.path.isfile(file_path):
    59. print("File to hide does not exist")
    60. return
    61. image, shape_orig = read_image(img_path)
    62. file = read_file(file_path)
    63. file_len = file.shape[0]
    64. len_array = np.array([file_len], dtype=np.uint32).view(np.uint8)
    65. len_array = np.unpackbits(len_array)
    66. img_len = image.shape[0]
    67. if file_len >= img_len - header_len:
    68. print("File too big, error")
    69. return
    70. else:
    71. tmp = file
    72. file = np.random.randint(2, size=img_len, dtype=np.uint8)
    73. file[header_len:header_len+file_len] = tmp
    74. file[:header_len] = len_array
    75. encoded_data = encode_data(image, file)
    76. write_image(output_path, encoded_data, shape_orig)
    77. print("Image encoded")
    78. # Update the preview image
    79. original_image = Image.fromarray(encoded_data.reshape(shape_orig))
    80. photo = ImageTk.PhotoImage(original_image)
    81. lbl.config(image=photo)
    82. lbl.image = photo
    83. zoom_slider.set(100) # Reset the zoom slider to default
    84. def unhide_images():
    85. global original_image # Ensure this is a global variable
    86. original_file = original_entry_unhide.get()
    87. save_file = save_file_entry_unhide.get()
    88. if not os.path.isfile(original_file):
    89. print("Image file does not exist")
    90. return
    91. encoded_data, shape_orig = read_image(original_file)
    92. data = decode_data(encoded_data)
    93. el_array = np.packbits(data[:header_len])
    94. extracted_len = el_array.view(np.uint32)[0]
    95. data = data[header_len:extracted_len+header_len]
    96. write_file(save_file, data)
    97. print("File extracted and saved")
    98. # Load and display the original image
    99. original_image = Image.open(original_file)
    100. original_image.thumbnail((shape_orig[1], shape_orig[0])) # Resize if needed
    101. original_photo = ImageTk.PhotoImage(original_image)
    102. lbl.config(image=original_photo)
    103. lbl.image = original_photo
    104. zoom_slider.set(100) # Reset the zoom slider to default
    105. # Create the root window
    106. root = tk.Tk()
    107. root.geometry("1000x700")
    108. root.title("Image Steganography Tool")
    109. root.configure(bg="#282c34")
    110. # Styling for labels and buttons
    111. label_font = ('Arial', 12, 'bold')
    112. entry_font = ('Arial', 11)
    113. button_font = ('Arial', 12, 'bold')
    114. button_color = "#61afef"
    115. button_fg = "white"
    116. # Hiding Section
    117. tk.Label(root, text="Hide a File Inside an Image", font=('Arial', 14, 'bold'), bg="#282c34", fg="white").grid(row=0, column=0, columnspan=2, pady=20)
    118. # Input for Original Image to hide file in
    119. tk.Label(root, text="Select Original Image:", font=label_font, bg="#282c34", fg="white").grid(row=1, column=0, sticky="e", padx=10)
    120. original_entry_hide = tk.Entry(root, width=40, font=entry_font)
    121. original_entry_hide.grid(row=1, column=1, padx=10, pady=5)
    122. browse_button_hide_image = tk.Button(root, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: original_entry_hide.insert(0, filedialog.askopenfilename()))
    123. browse_button_hide_image.grid(row=1, column=2, padx=10, pady=5)
    124. # Input for File to hide
    125. tk.Label(root, text="Select File to Hide:", font=label_font, bg="#282c34", fg="white").grid(row=2, column=0, sticky="e", padx=10)
    126. hide_file_entry = tk.Entry(root, width=40, font=entry_font)
    127. hide_file_entry.grid(row=2, column=1, padx=10, pady=5)
    128. browse_button_hide_file = tk.Button(root, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: hide_file_entry.insert(0, filedialog.askopenfilename()))
    129. browse_button_hide_file.grid(row=2, column=2, padx=10, pady=5)
    130. # Input for Save Location of encoded image
    131. tk.Label(root, text="Save Hidden Image As:", font=label_font, bg="#282c34", fg="white").grid(row=3, column=0, sticky="e", padx=10)
    132. save_file_entry_hide = tk.Entry(root, width=40, font=entry_font)
    133. save_file_entry_hide.grid(row=3, column=1, padx=10, pady=5)
    134. browse_button_save_hide = tk.Button(root, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: save_file_entry_hide.insert(0, filedialog.asksaveasfilename(defaultextension=".png")))
    135. browse_button_save_hide.grid(row=3, column=2, padx=10, pady=5)
    136. # Button to hide the file
    137. hide_button = tk.Button(root, text="Hide and Save Image", font=button_font, bg="#98c379", fg="black", command=hide_images)
    138. hide_button.grid(row=4, column=1, pady=20)
    139. # Unhiding Section
    140. tk.Label(root, text="Unhide File from Image", font=('Arial', 14, 'bold'), bg="#282c34", fg="white").grid(row=5, column=0, columnspan=2, pady=20)
    141. # Input for Encoded Image to unhide file from
    142. tk.Label(root, text="Select Encoded Image:", font=label_font, bg="#282c34", fg="white").grid(row=6, column=0, sticky="e", padx=10)
    143. original_entry_unhide = tk.Entry(root, width=40, font=entry_font)
    144. original_entry_unhide.grid(row=6, column=1, padx=10, pady=5)
    145. browse_button_unhide_image = tk.Button(root, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: original_entry_unhide.insert(0, filedialog.askopenfilename()))
    146. browse_button_unhide_image.grid(row=6, column=2, padx=10, pady=5)
    147. # Input for Save Location of extracted file
    148. tk.Label(root, text="Save Extracted File As:", font=label_font, bg="#282c34", fg="white").grid(row=7, column=0, sticky="e", padx=10)
    149. save_file_entry_unhide = tk.Entry(root, width=40, font=entry_font)
    150. save_file_entry_unhide.grid(row=7, column=1, padx=10, pady=5)
    151. browse_button_save_unhide = tk.Button(root, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: save_file_entry_unhide.insert(0, filedialog.asksaveasfilename()))
    152. browse_button_save_unhide.grid(row=7, column=2, padx=10, pady=5)
    153. # Button to unhide the file
    154. unhide_button = tk.Button(root, text="Extract and Save File", font=button_font, bg="#e06c75", fg="black", command=unhide_images)
    155. unhide_button.grid(row=8, column=1, pady=20)
    156. # Preview Section for Original and Encoded Images
    157. preview_frame = tk.Frame(root, relief="groove", bg="white")
    158. preview_frame.grid(row=0, column=3, rowspan=10, padx=20, pady=10)
    159. lbl = tk.Label(preview_frame, text="Preview", bg="white", font=label_font)
    160. lbl.grid(row=0, column=0)
    161. # Image Zoom Slider
    162. zoom_slider = tk.Scale(root, from_=10, to=200, orient='horizontal', label="Zoom Image (%)", font=label_font, bg="#282c34", fg="white", command=update_zoom)
    163. zoom_slider.grid(row=9, column=3, padx=20, pady=10)
    164. zoom_slider.set(100) # Default zoom is 100%
    165. 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 によって変換されたページ (->オリジナル) /