Advertisement
Najeebsk

RAR-FILE-IMAGES2.0.pyw

Jun 11th, 2024
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.78 KB | None | 0 0
  1. import os
  2. import numpy as np
  3. import imageio.v2 as imageio
  4. import tkinter as tk
  5. from tkinter import filedialog, ttk
  6. from PIL import Image, ImageTk
  7. import rarfile
  8. import io
  9.  
  10. header_len = 4 * 8  # uint32 bit length
  11.  
  12. def read_image(img_path):
  13.     img = np.array(imageio.imread(img_path), dtype=np.uint8)
  14.     orig_shape = img.shape
  15.     return img.flatten(), orig_shape
  16.  
  17. def decode_data(encoded_data):
  18.     out_mask = np.ones_like(encoded_data)
  19.     output = np.bitwise_and(encoded_data, out_mask)
  20.     return output
  21.  
  22. def write_file(file_path, file_bit_array):
  23.     bytes_data = np.packbits(file_bit_array)
  24.     with open(file_path, 'wb') as f:
  25.         f.write(bytes_data)
  26.  
  27. def browse_rar_file():
  28.     filename = filedialog.askopenfilename(initialdir="/", title="Select RAR File", filetypes=[("RAR files", "*.rar")])
  29.     if filename:
  30.         rar_entry.delete(0, tk.END)
  31.         rar_entry.insert(0, filename)
  32.         load_rar_file_images(filename)
  33.  
  34. def load_rar_file_images(filename):
  35.     try:
  36.         with rarfile.RarFile(filename) as rf:
  37.             rf.setpassword(password_entry.get())  # Set the password for the RAR file
  38.             global image_files
  39.             image_files = sorted([file for file in rf.namelist() if file.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))])
  40.             image_dropdown['values'] = image_files
  41.             if image_files:
  42.                 image_dropdown.current(0)
  43.                 show_image_from_rar(filename, image_files[0])
  44.     except Exception as e:
  45.         print(f"Failed to read RAR file: {e}")
  46.  
  47. def show_image_from_rar(filename, image_name):
  48.     try:
  49.         with rarfile.RarFile(filename) as rf:
  50.             rf.setpassword(password_entry.get())  # Set the password for the RAR file
  51.             with rf.open(image_name) as file:
  52.                 image_data = file.read()
  53.                 image = Image.open(io.BytesIO(image_data))
  54.                 global original_image
  55.                 original_image = image
  56.                 update_zoom_image()
  57.     except Exception as e:
  58.         print(f"Failed to open image from RAR file: {e}")
  59.  
  60. def show_original_image(image):
  61.     try:
  62.         image.thumbnail((400, 400))  # Resize if needed
  63.         original_photo = ImageTk.PhotoImage(image)
  64.         lbl.config(image=original_photo)
  65.         lbl.image = original_photo
  66.     except Exception as e:
  67.         print(f"Failed to open image file: {e}")
  68.  
  69. def update_zoom_image():
  70.     try:
  71.         zoom_factor = zoom_slider.get()
  72.         width, height = original_image.size
  73.         new_size = (int(width * zoom_factor), int(height * zoom_factor))
  74.         zoomed_image = original_image.resize(new_size, Image.LANCZOS)
  75.         zoomed_photo = ImageTk.PhotoImage(zoomed_image)
  76.         lbl.config(image=zoomed_photo)
  77.         lbl.image = zoomed_photo
  78.         lbl.update_idletasks()  # Update the label to reflect the new image size
  79.        
  80.         # Update the scroll region of the canvas to the new size of the image
  81.         canvas.config(scrollregion=canvas.bbox(tk.ALL))
  82.     except Exception as e:
  83.         print(f"Failed to update zoom image: {e}")
  84.  
  85. def show_next_image():
  86.     current_index = image_files.index(image_dropdown.get())
  87.     if current_index < len(image_files) - 1:
  88.         next_index = current_index + 1
  89.     else:
  90.         next_index = 0  # Loop back to the first image
  91.     image_dropdown.current(next_index)
  92.     show_image_from_rar(rar_entry.get(), image_files[next_index])
  93.  
  94. def show_previous_image():
  95.     current_index = image_files.index(image_dropdown.get())
  96.     if current_index > 0:
  97.         previous_index = current_index - 1
  98.     else:
  99.         previous_index = len(image_files) - 1  # Loop back to the last image
  100.     image_dropdown.current(previous_index)
  101.     show_image_from_rar(rar_entry.get(), image_files[previous_index])
  102.  
  103. root = tk.Tk()
  104. root.geometry("1000x670")
  105. root.title("Najeeb Image Viewer")
  106.  
  107. # Password Entry
  108. tk.Label(root, text="Password:").place(x=10, y=6)
  109. password_entry = tk.Entry(root, show="*")
  110. password_entry.place(x=80, y=6)
  111.  
  112. # Input for RAR File Selection
  113. tk.Label(root, text="Select RAR File:").place(x=220, y=6)
  114. rar_entry = tk.Entry(root)
  115. rar_entry.place(x=320, y=6)
  116. browse_rar_button = tk.Button(root, text="Browse", command=browse_rar_file)
  117. browse_rar_button.place(x=460, y=4)
  118.  
  119. # Dropdown for RAR file images
  120. tk.Label(root, text="Select Image from RAR:").place(x=530, y=6)
  121. image_dropdown = ttk.Combobox(root, state="readonly")
  122. image_dropdown.place(x=670, y=6, width=200)
  123. image_dropdown.bind("<<ComboboxSelected>>", lambda e: show_image_from_rar(rar_entry.get(), image_dropdown.get()))
  124.  
  125. # Frame for Original Image
  126. frame = tk.Frame(root, bd=3, bg="#2c3e50", width=960, height=570, relief=tk.GROOVE)
  127. frame.place(x=10, y=72)
  128.  
  129. # Add scrollbars to the frame
  130. x_scroll = tk.Scrollbar(frame, orient=tk.HORIZONTAL)
  131. x_scroll.pack(side=tk.BOTTOM, fill=tk.X)
  132.  
  133. y_scroll = tk.Scrollbar(frame, orient=tk.VERTICAL)
  134. y_scroll.pack(side=tk.RIGHT, fill=tk.Y)
  135.  
  136. canvas = tk.Canvas(frame, bg="#2c3e50", width=950, height=560, xscrollcommand=x_scroll.set, yscrollcommand=y_scroll.set)
  137. canvas.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)
  138.  
  139. x_scroll.config(command=canvas.xview)
  140. y_scroll.config(command=canvas.yview)
  141.  
  142. lbl = tk.Label(canvas, bg="#2c3e50")
  143. canvas.create_window((0,0), window=lbl, anchor="nw")
  144.  
  145. # Zoom Slider
  146. tk.Label(root, text="Zoom Image:").place(x=10, y=45)
  147. zoom_slider = tk.Scale(root, from_=0.1, to=10, orient=tk.HORIZONTAL, label="", resolution=0.1, command=lambda e: update_zoom_image())
  148. zoom_slider.set(0.3)
  149. zoom_slider.place(x=90, y=24)
  150.  
  151. # Previous and Next buttons
  152. prev_button = tk.Button(root, text="Previous Picture", command=show_previous_image)
  153. prev_button.place(x=780, y=40)
  154. next_button = tk.Button(root, text="Next Picture", command=show_next_image)
  155. next_button.place(x=900, y=40)
  156.  
  157. root.mainloop()
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement