使用Python實(shí)現(xiàn)圖片和base64轉(zhuǎn)換工具
簡介
使用Python的base64
模塊來實(shí)現(xiàn)圖片和Base64編碼之間的轉(zhuǎn)換。可以將圖片轉(zhuǎn)換為Base64編碼,以及將Base64編碼轉(zhuǎn)換回圖片并保存。
依賴庫
該工具僅依賴 Python 標(biāo)準(zhǔn)庫(tkinter
和 base64
),無需安裝其他第三方庫。
完整代碼:
import tkinter as tk from tkinter import filedialog, messagebox import base64 class Base64ImageConverter: def __init__(self, root): self.root = root self.root.title("圖片與Base64轉(zhuǎn)換工具") self.root.geometry("500x400") # 上傳圖片按鈕 self.upload_button = tk.Button(root, text="上傳圖片", command=self.upload_image) self.upload_button.pack(pady=10) # 顯示圖片路徑 self.image_path_label = tk.Label(root, text="未選擇圖片", fg="gray") self.image_path_label.pack(pady=5) # Base64 輸入框 self.base64_input_label = tk.Label(root, text="輸入Base64字符串:") self.base64_input_label.pack(pady=5) self.base64_input = tk.Text(root, height=5, width=50) self.base64_input.pack(pady=5) # 轉(zhuǎn)換按鈕 self.convert_button = tk.Button(root, text="轉(zhuǎn)換為Base64", command=self.convert_to_base64) self.convert_button.pack(pady=10) self.convert_button2 = tk.Button(root, text="轉(zhuǎn)換為圖片", command=self.convert_to_image) self.convert_button2.pack(pady=10) # 狀態(tài)顯示 self.status_label = tk.Label(root, text="", fg="blue") self.status_label.pack(pady=10) def upload_image(self): """上傳圖片并顯示路徑""" file_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.jpg;*.jpeg;*.png;*.bmp")]) if file_path: self.image_path_label.config(text=file_path, fg="green") self.status_label.config(text="圖片已上傳", fg="blue") def convert_to_base64(self): """將圖片轉(zhuǎn)換為Base64""" image_path = self.image_path_label.cget("text") if image_path == "未選擇圖片": messagebox.showerror("錯(cuò)誤", "請(qǐng)先上傳圖片!") return try: with open(image_path, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()).decode('utf-8') self.base64_input.delete(1.0, tk.END) # 清空輸入框 self.base64_input.insert(tk.END, encoded_string) self.status_label.config(text="圖片已轉(zhuǎn)換為Base64", fg="green") except Exception as e: messagebox.showerror("錯(cuò)誤", f"轉(zhuǎn)換失敗: {str(e)}") def convert_to_image(self): """將Base64轉(zhuǎn)換為圖片并保存""" base64_string = self.base64_input.get(1.0, tk.END).strip() if not base64_string: messagebox.showerror("錯(cuò)誤", "Base64字符串不能為空!") return try: output_path = filedialog.asksaveasfilename(defaultextension=".jpg", filetypes=[("JPEG Files", "*.jpg"), ("PNG Files", "*.png")]) if output_path: image_data = base64.b64decode(base64_string) with open(output_path, "wb") as image_file: image_file.write(image_data) self.status_label.config(text=f"圖片已保存為: {output_path}", fg="green") except Exception as e: messagebox.showerror("錯(cuò)誤", f"轉(zhuǎn)換失敗: {str(e)}") if __name__ == "__main__": root = tk.Tk() app = Base64ImageConverter(root) root.mainloop()
功能說明
1.上傳圖片:
點(diǎn)擊“上傳圖片”按鈕,選擇本地圖片文件(支持 .jpg, .jpeg, .png, .bmp 格式)。
圖片路徑會(huì)顯示在界面上。
2.轉(zhuǎn)換為Base64:
點(diǎn)擊“轉(zhuǎn)換為Base64”按鈕,將上傳的圖片轉(zhuǎn)換為 Base64 字符串,并顯示在輸入框中。
3.轉(zhuǎn)換為圖片:
在輸入框中輸入 Base64 字符串,點(diǎn)擊“轉(zhuǎn)換為圖片”按鈕,選擇保存路徑,將 Base64 字符串解碼為圖片并保存。
4.狀態(tài)提示:
界面底部會(huì)顯示當(dāng)前操作的狀態(tài)(如“圖片已上傳”、“圖片已轉(zhuǎn)換為Base64”等)。
到此這篇關(guān)于使用Python實(shí)現(xiàn)圖片和base64轉(zhuǎn)換工具的文章就介紹到這了,更多相關(guān)Python圖片轉(zhuǎn)base64內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django中使用CORS實(shí)現(xiàn)跨域請(qǐng)求過程解析
這篇文章主要介紹了Django中使用CORS實(shí)現(xiàn)跨域請(qǐng)求過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08django中的自定義分頁器的實(shí)現(xiàn)示例
本文主要介紹了django中的自定義分頁器的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08對(duì)numpy數(shù)據(jù)寫入文件的方法講解
今天小編就為大家分享一篇對(duì)numpy數(shù)據(jù)寫入文件的方法講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07聊聊Pytorch torch.cat與torch.stack的區(qū)別
這篇文章主要介紹了Pytorch torch.cat與torch.stack的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Python?3.12安裝庫報(bào)錯(cuò)解決方案
這篇文章主要介紹了Python?3.12安裝庫報(bào)錯(cuò)的解決方案,講解了Python?3.12移除pkgutil.ImpImporter支持導(dǎo)致的AttributeError錯(cuò)誤,并提供了兩種解決方案,需要的朋友可以參考下2025-03-03對(duì)pandas中apply函數(shù)的用法詳解
下面小編就為大家分享一篇對(duì)pandas中apply函數(shù)的用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04