使用Python實(shí)現(xiàn)圖片和base64轉(zhuǎn)換工具
簡(jiǎn)介
使用Python的base64模塊來(lái)實(shí)現(xiàn)圖片和Base64編碼之間的轉(zhuǎn)換??梢詫D片轉(zhuǎn)換為Base64編碼,以及將Base64編碼轉(zhuǎn)換回圖片并保存。

依賴(lài)庫(kù)
該工具僅依賴(lài) Python 標(biāo)準(zhǔn)庫(kù)(tkinter 和 base64),無(wú)需安裝其他第三方庫(kù)。
完整代碼:
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()功能說(shuō)明
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)文章
Python 微信爬蟲(chóng)完整實(shí)例【單線(xiàn)程與多線(xiàn)程】
這篇文章主要介紹了Python 微信爬蟲(chóng),結(jié)合完整實(shí)例形式分析了Python基于單線(xiàn)程與多線(xiàn)程模式爬取微信信息相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
Python求出0~100以?xún)?nèi)的所有素?cái)?shù)
質(zhì)數(shù)又稱(chēng)素?cái)?shù)。一個(gè)大于1的自然數(shù),除了1和它自身外,不能被其他自然數(shù)整除的數(shù)叫做質(zhì)數(shù);否則稱(chēng)為合數(shù)。下面小編給大家?guī)?lái)了Python求出0~100以?xún)?nèi)的所有素?cái)?shù)實(shí)例代碼,需要的朋友參考下2018-01-01
Python元類(lèi)基礎(chǔ)知識(shí)示例深度剖析
這篇文章主要為大家介紹了Python元類(lèi)基礎(chǔ)知識(shí)深度剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
分享一下如何編寫(xiě)高效且優(yōu)雅的 Python 代碼
這篇文章主要介紹了分享一下如何編寫(xiě)高效且優(yōu)雅的 Python 代碼,需要的朋友可以參考下2017-09-09
Python+matplotlib實(shí)現(xiàn)堆疊圖的繪制
Matplotlib作為Python的2D繪圖庫(kù),它以各種硬拷貝格式和跨平臺(tái)的交互式環(huán)境生成出版質(zhì)量級(jí)別的圖形。本文將利用Matplotlib庫(kù)繪制堆疊圖,感興趣的可以了解一下2022-03-03
Python中的JSON?Pickle?Shelve模塊特性與區(qū)別實(shí)例探究
在Python中,處理數(shù)據(jù)序列化和持久化是極其重要的,JSON、Pickle和Shelve是三種常用的模塊,它們提供了不同的方法來(lái)處理數(shù)據(jù)的序列化和持久化,本文將深入研究這三個(gè)模塊,探討它們的特性、用法以及各自的優(yōu)缺點(diǎn)2024-01-01
使用Python實(shí)現(xiàn)自動(dòng)編寫(xiě)word文檔
這篇文章主要為大家詳細(xì)介紹了如何使用Python中的python-docx實(shí)現(xiàn)自動(dòng)編寫(xiě)word文檔,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2024-12-12
Pytorch實(shí)現(xiàn)簡(jiǎn)單自定義網(wǎng)絡(luò)層的方法
這篇文章主要給大家介紹了關(guān)于Pytorch實(shí)現(xiàn)簡(jiǎn)單自定義網(wǎng)絡(luò)層的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-05-05

