画像→Base64エンコーダ『礎陸拾肆』 ソースコード

以下は『礎陸拾肆』のソースコード全文です:

 
        import tkinter as tk
        from tkinter import filedialog, messagebox
        from PIL import Image
        import base64
        import io
        import os
        
        selected_file_path = None
        output_dir_path = None
        
        def select_file():
            global selected_file_path
            filepath = filedialog.askopenfilename(
                title="画像ファイルを選択",
                filetypes=[("画像ファイル", "*.png;*.jpg;*.jpeg;*.bmp;*.gif")]
            )
            if not filepath:
                return
            selected_file_path = filepath
            file_label.config(text=f"選択中: {filepath}")
        
        def select_output_directory():
            global output_dir_path
            directory = filedialog.askdirectory(title="出力先フォルダを選択")
            if directory:
                output_dir_path = directory
                output_dir_label.config(text=f"出力先: {directory}")
        
        def execute_conversion():
            if not selected_file_path:
                messagebox.showwarning("警告", "画像ファイルを選択してください。")
                return
        
            confirm = messagebox.askokcancel("確認", "変換を実行しますか?")
            if not confirm:
                return
        
            try:
                img = Image.open(selected_file_path)
                if img.width != img.height:
                    messagebox.showerror("エラー", "正方形の画像を選んでください。")
                    return
        
                # 画像を16x16にリサイズ
                img_resized = img.resize((16, 16), Image.Resampling.LANCZOS)
        
                # 出力先決定
                base_name = os.path.splitext(os.path.basename(selected_file_path))[0]
                output_dir = output_dir_path if output_dir_path else os.path.dirname(selected_file_path)
        
                txt_path = os.path.join(output_dir, f"{base_name}.txt")
                gif_path = os.path.join(output_dir, f"{base_name}.gif")
        
                # GIFをメモリに保存
                buffer = io.BytesIO()
                img_resized.save(buffer, format="GIF")
                gif_data = buffer.getvalue()
        
                # base64にエンコードしてtxtに保存
                encoded = base64.b64encode(gif_data).decode("utf-8")
                with open(txt_path, "w", encoding="utf-8") as f:
                    f.write(encoded)
        
                # チェックボックスがONならGIFファイルも保存
                if save_gif_var.get():
                    img_resized.save(gif_path, format="GIF")
        
                messagebox.showinfo("完了", f"{txt_path} に出力しました。")
        
            except Exception as e:
                messagebox.showerror("エラー", f"処理中にエラーが発生しました:\n{e}")
        
        # GUI構築
        root = tk.Tk()
        
        ##タイトルバーアイコン表示用
        data = '''R0lGODlhEAAQAIUAAAAAALHE2v///4mnx6S81XGWu5m00cjW5nibwLPK4/v8/PP4/vn5+maNtvD0+E58rLzQ5uXq8eTr86qqqvP3+32gw12Hs7jJ3dnk8O7w9O7u7t/q99/m7sXT4tPe6ar//7/P4Kqq/+Lo8H9//39/fwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAAAQABAAQAjMAAEInCBQgAIFAgYKBCAgAIIGAQgcCDAg4oABBgIkVEDAQsSJGScWgKCAIQAGGwIUaEBgwIMCARYwYCggBAAHBhA8eDAggU8HJgVcyEmAAEWMFglcSBghAUSJFwMEMDBSxMIPABQ4oCBBAgUHGkwuJHESw1SJGEqOKAig6cWWCAy0TJDBpAcDGQMcMFAxYgECHBgeRYBgAIICBSo0KFBRgAAQBP7qbRkxQAMDHWgmqFDAwgDGhf8ecGySwYIFEKQeOD0z4ULXjkmLBRAQADs=''' 
        root.tk.call('wm', 'iconphoto', root._w, tk.PhotoImage(data=data))
        ####
        
        root.title("Base64エンコーダ - 礎陸拾肆")
        root.geometry("450x300")
        
        # ファイル選択
        tk.Label(root, text="画像ファイルを選択してから実行").pack(pady=10)
        tk.Button(root, text="画像を選択", command=select_file).pack()
        file_label = tk.Label(root, text="選択中: なし", wraplength=400)
        file_label.pack(pady=5)
        
        # 出力先ディレクトリ選択
        tk.Button(root, text="出力先を選択", command=select_output_directory).pack(pady=5)
        output_dir_label = tk.Label(root, text="出力先: 未選択(画像と同じフォルダ)", wraplength=400)
        output_dir_label.pack()
        
        # GIF保存チェックボックス
        save_gif_var = tk.BooleanVar(value=False)
        gif_checkbox = tk.Checkbutton(root, text=".gifファイルを保存する", variable=save_gif_var)
        gif_checkbox.pack(pady=10)
        
        # 実行ボタン
        tk.Button(root, text="実行", command=execute_conversion).pack(pady=15)
        
        root.mainloop()
       
    

📦 ダウンロード 🏠 ホームへ