今天,我們將深入研究一個令人興奮的項目,該項目將圖像處理與基本加密技術相結合。我們將探索一個可以使用簡單而有效的方法加密和解密映像的 Python 程式。讓我們一步步分解吧!
要跟隨,您應該:
Pillow 函式庫,這是一個用於處理影像的 Python 成像庫。使用 pip installpillow 來安裝。
Tkinter 這是一個用於建立圖形使用者介面 (GUI) 的 Python 函式庫。使用 pip install tk 進行安裝。
程式建立了一個圖形使用者介面 (GUI),允許使用者:
加密過程根據種子金鑰對影像的像素進行混洗,使影像無法辨識。解密過程逆轉了這個過程,恢復了原始影像。
import os from tkinter import Tk, Button, Label, Entry, filedialog, messagebox from PIL import Image import random
def get_seeded_random(seed): """Returns a seeded random generator.""" return random.Random(seed)
get_seed_random 函數傳回一個隨機對象,如果給定相同的種子值,則該物件每次都可以以相同的方式對項目進行洗牌。
這是一致加密和解密影像的關鍵。
def encrypt_image(input_image_path, output_image_path, seed): """Encrypts the image by manipulating pixel values.""" image = Image.open(input_image_path) width, height = image.size # Get pixel data as a list pixels = list(image.getdata()) random_gen = get_seeded_random(seed) # Create a list of pixel indices indices = list(range(len(pixels))) # Shuffle the indices using the seeded random generator random_gen.shuffle(indices) # Reorder pixels based on shuffled indices encrypted_pixels = [pixels[i] for i in indices] # Create new image encrypted_image = Image.new(image.mode, (width, height)) # Apply encrypted pixels to the new image encrypted_image.putdata(encrypted_pixels) # Save the encrypted image encrypted_image.save(output_image_path) return True
在此 encrypt_image 函數中:
def decrypt_image(input_image_path, output_image_path, seed): """Decrypts the image by reversing the encryption process.""" image = Image.open(input_image_path) width, height = image.size # Get encrypted pixel data as a list encrypted_pixels = list(image.getdata()) random_gen = get_seeded_random(seed) # Create a new list to hold pixel indices in their original order indices = list(range(len(encrypted_pixels))) # Shuffle the indices again to get the original order random_gen.shuffle(indices) # Create a new image to hold the decrypted data decrypted_pixels = [None] * len(encrypted_pixels) # Restore original pixels using the shuffled indices for original_index, shuffled_index in enumerate(indices): decrypted_pixels[shuffled_index] = encrypted_pixels[original_index] # Save the decrypted image decrypted_image = Image.new(image.mode, (width, height)) decrypted_image.putdata(decrypted_pixels) decrypted_image.save(output_image_path) return True
decrypt_image 函數透過反轉加密過程來運作。它:
def select_input_image(): """Opens a file dialog to select an input image.""" input_image_path = filedialog.askopenfilename(title="Select Image") input_image_label.config(text=input_image_path) def select_output_image(): """Opens a file dialog to select an output image path.""" output_image_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png"),("JPEG files", "*.jpg;*.jpeg"),("All files", "*.*")], title="Save Encrypted/Decrypted Image") output_image_label.config(text=output_image_path)
select_input_image 函數允許使用者使用檔案對話方塊選擇他們想要加密或解密的影像。
選定的影像路徑將顯示在 GUI 上。
同樣,select_output_image函數允許使用者選擇保存輸出影像的位置。
def encrypt(): input_image_path = input_image_label.cget("text") output_image_path = output_image_label.cget("text") seed = seed_entry.get() if not input_image_path or not output_image_path: messagebox.showerror("Error", "Please select input and output images.") return if encrypt_image(input_image_path, output_image_path, seed): messagebox.showinfo("Success", "Image encrypted successfully!") def decrypt(): input_image_path = input_image_label.cget("text") output_image_path = output_image_label.cget("text") seed = seed_entry.get() if not input_image_path or not output_image_path: messagebox.showerror("Error", "Please select input and output images.") return if decrypt_image(input_image_path, output_image_path, seed): messagebox.showinfo("Success", "Image decrypted successfully!")
加密與解密函數:
root = Tk() root.title("Image Encryption Tool") # Create and place widgets Label(root, text="Select Image to Encrypt/Decrypt:").pack(pady=5) input_image_label = Label(root, text="No image selected") input_image_label.pack(pady=5) Button(root, text="Browse", command=select_input_image).pack(pady=5) Label(root, text="Output Image Path:").pack(pady=5) output_image_label = Label(root, text="No output path selected") output_image_label.pack(pady=5) Button(root, text="Save As", command=select_output_image).pack(pady=5) Label(root, text="Enter Seed Key:").pack(pady=5) seed_entry = Entry(root) seed_entry.pack(pady=5) Button(root, text="Encrypt Image", command=encrypt).pack(pady=5) Button(root, text="Decrypt Image", command=decrypt).pack(pady=5) root.mainloop()
標籤、按鈕和文字輸入欄位使用 pack() 放置。
root.mainloop 函數使視窗保持開啟狀態並回應使用者輸入。
該程式示範如何在像素層級操作數位影像以及如何使用偽隨機數產生器來執行基本加密任務。
祝您編碼愉快,並保持安全!
以上是使用 Python 建立簡單的圖像加密工具的詳細內容。更多資訊請關注PHP中文網其他相關文章!