Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import numpy as np
- import imageio.v2 as imageio
- import tkinter as tk
- from tkinter import filedialog, ttk
- from PIL import Image, ImageTk
- import rarfile
- import io
- header_len = 4 * 8 # uint32 bit length
- def read_image(img_path):
- img = np.array(imageio.imread(img_path), dtype=np.uint8)
- orig_shape = img.shape
- return img.flatten(), orig_shape
- def decode_data(encoded_data):
- out_mask = np.ones_like(encoded_data)
- output = np.bitwise_and(encoded_data, out_mask)
- return output
- def write_file(file_path, file_bit_array):
- bytes_data = np.packbits(file_bit_array)
- with open(file_path, 'wb') as f:
- f.write(bytes_data)
- def browse_rar_file():
- filename = filedialog.askopenfilename(initialdir="/", title="Select RAR File", filetypes=[("RAR files", "*.rar")])
- if filename:
- rar_entry.delete(0, tk.END)
- rar_entry.insert(0, filename)
- load_rar_file_images(filename)
- def load_rar_file_images(filename):
- try:
- with rarfile.RarFile(filename) as rf:
- rf.setpassword(password_entry.get()) # Set the password for the RAR file
- global image_files
- image_files = sorted([file for file in rf.namelist() if file.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))])
- image_dropdown['values'] = image_files
- if image_files:
- image_dropdown.current(0)
- show_image_from_rar(filename, image_files[0])
- except Exception as e:
- print(f"Failed to read RAR file: {e}")
- def show_image_from_rar(filename, image_name):
- try:
- with rarfile.RarFile(filename) as rf:
- rf.setpassword(password_entry.get()) # Set the password for the RAR file
- with rf.open(image_name) as file:
- image_data = file.read()
- image = Image.open(io.BytesIO(image_data))
- global original_image
- original_image = image
- update_zoom_image()
- except Exception as e:
- print(f"Failed to open image from RAR file: {e}")
- def show_original_image(image):
- try:
- image.thumbnail((400, 400)) # Resize if needed
- original_photo = ImageTk.PhotoImage(image)
- lbl.config(image=original_photo)
- lbl.image = original_photo
- except Exception as e:
- print(f"Failed to open image file: {e}")
- def update_zoom_image():
- try:
- zoom_factor = zoom_slider.get()
- width, height = original_image.size
- new_size = (int(width * zoom_factor), int(height * zoom_factor))
- zoomed_image = original_image.resize(new_size, Image.LANCZOS)
- zoomed_photo = ImageTk.PhotoImage(zoomed_image)
- lbl.config(image=zoomed_photo)
- lbl.image = zoomed_photo
- lbl.update_idletasks() # Update the label to reflect the new image size
- # Update the scroll region of the canvas to the new size of the image
- canvas.config(scrollregion=canvas.bbox(tk.ALL))
- except Exception as e:
- print(f"Failed to update zoom image: {e}")
- def show_next_image():
- current_index = image_files.index(image_dropdown.get())
- if current_index < len(image_files) - 1:
- next_index = current_index + 1
- else:
- next_index = 0 # Loop back to the first image
- image_dropdown.current(next_index)
- show_image_from_rar(rar_entry.get(), image_files[next_index])
- def show_previous_image():
- current_index = image_files.index(image_dropdown.get())
- if current_index > 0:
- previous_index = current_index - 1
- else:
- previous_index = len(image_files) - 1 # Loop back to the last image
- image_dropdown.current(previous_index)
- show_image_from_rar(rar_entry.get(), image_files[previous_index])
- root = tk.Tk()
- root.geometry("1000x670")
- root.title("Najeeb Image Viewer")
- # Password Entry
- tk.Label(root, text="Password:").place(x=10, y=6)
- password_entry = tk.Entry(root, show="*")
- password_entry.place(x=80, y=6)
- # Input for RAR File Selection
- tk.Label(root, text="Select RAR File:").place(x=220, y=6)
- rar_entry = tk.Entry(root)
- rar_entry.place(x=320, y=6)
- browse_rar_button = tk.Button(root, text="Browse", command=browse_rar_file)
- browse_rar_button.place(x=460, y=4)
- # Dropdown for RAR file images
- tk.Label(root, text="Select Image from RAR:").place(x=530, y=6)
- image_dropdown = ttk.Combobox(root, state="readonly")
- image_dropdown.place(x=670, y=6, width=200)
- image_dropdown.bind("<<ComboboxSelected>>", lambda e: show_image_from_rar(rar_entry.get(), image_dropdown.get()))
- # Frame for Original Image
- frame = tk.Frame(root, bd=3, bg="#2c3e50", width=960, height=570, relief=tk.GROOVE)
- frame.place(x=10, y=72)
- # Add scrollbars to the frame
- x_scroll = tk.Scrollbar(frame, orient=tk.HORIZONTAL)
- x_scroll.pack(side=tk.BOTTOM, fill=tk.X)
- y_scroll = tk.Scrollbar(frame, orient=tk.VERTICAL)
- y_scroll.pack(side=tk.RIGHT, fill=tk.Y)
- canvas = tk.Canvas(frame, bg="#2c3e50", width=950, height=560, xscrollcommand=x_scroll.set, yscrollcommand=y_scroll.set)
- canvas.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)
- x_scroll.config(command=canvas.xview)
- y_scroll.config(command=canvas.yview)
- lbl = tk.Label(canvas, bg="#2c3e50")
- canvas.create_window((0,0), window=lbl, anchor="nw")
- # Zoom Slider
- tk.Label(root, text="Zoom Image:").place(x=10, y=45)
- zoom_slider = tk.Scale(root, from_=0.1, to=10, orient=tk.HORIZONTAL, label="", resolution=0.1, command=lambda e: update_zoom_image())
- zoom_slider.set(0.3)
- zoom_slider.place(x=90, y=24)
- # Previous and Next buttons
- prev_button = tk.Button(root, text="Previous Picture", command=show_previous_image)
- prev_button.place(x=780, y=40)
- next_button = tk.Button(root, text="Next Picture", command=show_next_image)
- next_button.place(x=900, y=40)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement