欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python實(shí)現(xiàn)圖片和base64轉(zhuǎn)換工具

 更新時(shí)間:2025年02月08日 09:12:03   作者:mosquito_lover1  
這篇文章主要為大家詳細(xì)介紹了如何使用Python中的base64模塊編寫一個(gè)工具,可以實(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)文章

  • Python實(shí)現(xiàn)簡單的猜單詞小游戲

    Python實(shí)現(xiàn)簡單的猜單詞小游戲

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)簡單的猜單詞小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • 如何使用Python讀取xml文件

    如何使用Python讀取xml文件

    這篇文章主要介紹了如何使用Python讀取xml文件,關(guān)于python讀取xml文章很多,但大多文章都是貼一個(gè)xml文件,然后再貼個(gè)處理文件的代碼希望這篇文章可以更通俗易懂的教如何使用python 來讀取xml 文件
    2023-04-04
  • Django中使用CORS實(shí)現(xiàn)跨域請(qǐng)求過程解析

    Django中使用CORS實(shí)現(xiàn)跨域請(qǐng)求過程解析

    這篇文章主要介紹了Django中使用CORS實(shí)現(xiàn)跨域請(qǐng)求過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • django中的自定義分頁器的實(shí)現(xiàn)示例

    django中的自定義分頁器的實(shí)現(xiàn)示例

    本文主要介紹了django中的自定義分頁器的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 淺析Python字符串索引、切片、格式化

    淺析Python字符串索引、切片、格式化

    除了數(shù)字,Python中最常見的數(shù)據(jù)類型就是字符串,無論那種編程語言,字符串無處不在。本文將為大家詳細(xì)介紹Python中字符串的使用方法,需要的朋友可以參考一下
    2021-12-12
  • python日志記錄模塊實(shí)例及改進(jìn)

    python日志記錄模塊實(shí)例及改進(jìn)

    許多應(yīng)用程序中都會(huì)有日志模塊,用于記錄系統(tǒng)在運(yùn)行過程中的一些關(guān)鍵信息,以便于對(duì)系統(tǒng)的運(yùn)行狀況進(jìn)行跟蹤。在python中,我們不需要第三方的日志組件,因?yàn)樗呀?jīng)為我們提供了簡單易用、且功能強(qiáng)大的日志模塊:logging。
    2017-02-02
  • 對(duì)numpy數(shù)據(jù)寫入文件的方法講解

    對(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ū)別

    這篇文章主要介紹了Pytorch torch.cat與torch.stack的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python?3.12安裝庫報(bào)錯(cuò)解決方案

    Python?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ù)的用法詳解

    下面小編就為大家分享一篇對(duì)pandas中apply函數(shù)的用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04

最新評(píng)論