Advertisement
Najeebsk

RAR-FILE-IMAGES.pyw

May 24th, 2024 (edited)
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.83 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.             file_list = rf.namelist()
  39.             image_files = sorted([file for file in file_list 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. root = tk.Tk()
  86. root.geometry("1000x670")
  87. root.title("Najeeb Image Viewer")
  88.  
  89. # Password Entry
  90. tk.Label(root, text="Password:").place(x=10, y=6)
  91. password_entry = tk.Entry(root, show="*")
  92. password_entry.place(x=80, y=6)
  93.  
  94. # Input for RAR File Selection
  95. tk.Label(root, text="Select RAR File:").place(x=220, y=6)
  96. rar_entry = tk.Entry(root)
  97. rar_entry.place(x=320, y=6)
  98. browse_rar_button = tk.Button(root, text="Browse", command=browse_rar_file)
  99. browse_rar_button.place(x=460, y=4)
  100.  
  101. # Dropdown for RAR file images
  102. tk.Label(root, text="Select Image from RAR:").place(x=530, y=6)
  103. image_dropdown = ttk.Combobox(root, state="readonly")
  104. image_dropdown.place(x=670, y=6, width=200)
  105. image_dropdown.bind("<<ComboboxSelected>>", lambda e: show_image_from_rar(rar_entry.get(), image_dropdown.get()))
  106.  
  107. # Frame for Original Image
  108. frame = tk.Frame(root, bd=3, bg="#2c3e50", width=960, height=570, relief=tk.GROOVE)
  109. frame.place(x=10, y=72)
  110.  
  111. # Add scrollbars to the frame
  112. x_scroll = tk.Scrollbar(frame, orient=tk.HORIZONTAL)
  113. x_scroll.pack(side=tk.BOTTOM, fill=tk.X)
  114.  
  115. y_scroll = tk.Scrollbar(frame, orient=tk.VERTICAL)
  116. y_scroll.pack(side=tk.RIGHT, fill=tk.Y)
  117.  
  118. canvas = tk.Canvas(frame, bg="#2c3e50", width=950, height=560, xscrollcommand=x_scroll.set, yscrollcommand=y_scroll.set)
  119. canvas.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)
  120.  
  121. x_scroll.config(command=canvas.xview)
  122. y_scroll.config(command=canvas.yview)
  123.  
  124. lbl = tk.Label(canvas, bg="#2c3e50")
  125. canvas.create_window((0,0), window=lbl, anchor="nw")
  126.  
  127. # Zoom Slider
  128. tk.Label(root, text="Zoom Image:").place(x=10, y=45)
  129. zoom_slider = tk.Scale(root, from_=0.1, to=10, orient=tk.HORIZONTAL, label="", resolution=0.1, command=lambda e: update_zoom_image())
  130. zoom_slider.set(1)
  131. zoom_slider.place(x=90, y=24)
  132.  
  133. root.mainloop()
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement