Ai
4 Star 4 Fork 0

Gitee 极速下载/pysimplegui

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/PySimpleGUI/PySimpleGUI
克隆/下载
Demo_Base64_Single_Image_Encoder.py 2.25 KB
一键复制 编辑 原始数据 按行查看 历史
timkay@not.com 提交于 2024-02-12 23:14 +08:00 . First release of PySimpleGUI 5!
import PySimpleGUI as sg
import base64
"""
Make base64 image from a file
This is usually done in order to create a Base64 image for use as an Ucon or a Button image
To use, either copy and paste the full path to the file or use the browse button to locate the file.
Once chosen, the conversion will happen automatically with the result placed on the clipboard.
When complete, a popup window is shown that tells you to paste the image before closing the window. This is because of a
tkinter problem on Linux. On Windows you can close the Window, but on Linux, you'll need to keep it open until the paste completes
NOTE - if you're replacing your ICO file for your window with a base64 image, you will first need to convert your icon from
an ICO file into a PNG file. Encode the PNG file and then you'll be able to pass that value in your call to Window:
window = sg.Window('Window Title', layout, icon=icon)
Where icon is a variable you created using the contents of the clipboard folowing running this program.
Input: a single image file
Output: clipboard will contain the Base64 Byte String of the source image
Copyright 2021-2023 PySimpleSoft, Inc. and/or its licensors. All rights reserved.
Redistribution, modification, or any other use of PySimpleGUI or any portion thereof is subject to the terms of the PySimpleGUI License Agreement available at https://eula.pysimplegui.com.
You may not redistribute, modify or otherwise use PySimpleGUI or its contents except pursuant to the PySimpleGUI License Agreement.
"""
def convert_file_to_base64(filename):
try:
contents = open(filename, 'rb').read()
encoded = base64.b64encode(contents)
sg.clipboard_set(encoded)
# pyperclip.copy(str(encoded))
sg.popup('Copied to your clipboard!', 'Keep window open until you have pasted the base64 bytestring')
except Exception as error:
sg.popup_error('Cancelled - An error occurred', error)
if __name__ == '__main__':
filename = sg.popup_get_file('Source Image will be encoded and results placed on clipboard', title='Base64 Encoder')
if filename:
convert_file_to_base64(filename)
else:
sg.popup_cancel('Cancelled - No valid file entered')
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/mirrors/pysimplegui.git
git@gitee.com:mirrors/pysimplegui.git
mirrors
pysimplegui
pysimplegui
master

搜索帮助