1、因网上的exe工具转码图片过大就会卡死,所以通过python+gradio实现转码web服务。
2、exe工具地址(启动后黑框为服务进程),压缩包中有chromedrive.exe 谷歌浏览器启动驱动,启动时需要将该驱动放入程序同目录下使用:
链接: https://pan.baidu.com/s/1DtSksVBtVvC0sPwgY4YZTw?pwd=base 提取码: base
3、服务代码:
import gradio as gr
import base64
import cv2
import os, requests, time
import threading
now = time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime())
UPLOAD_PIC_FILE = 'pic_uploads'
RESULT_PIC_FILE = 'pic_results'
UPLOAD_BS64_FILE = 'bs64_uploads'
RESULT_BS64_FILE = 'bs64_results'
os.makedirs(UPLOAD_PIC_FILE, exist_ok=True)
os.makedirs(RESULT_PIC_FILE, exist_ok=True)
os.makedirs(UPLOAD_BS64_FILE, exist_ok=True)
os.makedirs(RESULT_BS64_FILE, exist_ok=True)
def save_pic(image):
# 保存上传的图像
pic_filename = 'uploaded_image.jpg'
bs64_filename = 'base64.txt'
current = os.path.join(RESULT_BS64_FILE, bs64_filename)
file_path = os.path.join(UPLOAD_PIC_FILE, pic_filename)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 格式转换,
cv2.imwrite(f'{file_path}', image)
with open(file_path, 'rb') as f:
pic = f.read()
pic_base64 = str(base64.b64encode(pic))[2:-1]
with open(f"{current}", "w") as f:
f.write(pic_base64)
return pic_base64
def bs64_path():
filename = 'base64.txt'
current = os.path.join(RESULT_BS64_FILE, filename)
return current
def save_base64(bs64):
# 保存上传的图像
pic_filename = 'pic_result.jpg'
bs64_filename = 'upload_base64.txt'
pic_result = os.path.join(RESULT_PIC_FILE, pic_filename)
bs64_upload = os.path.join(UPLOAD_BS64_FILE, bs64_filename)
with open(f'{bs64_upload}', 'w') as bs:
bs.write(bs64)
with open(f'{bs64_upload}', 'rb') as bf:
read_base64 = bf.read()
base64_pic = base64.b64decode(read_base64)
print(base64_pic)
with open(f'{pic_result}', 'wb') as picture:
picture.write(base64_pic)
return pic_result
def pic_path():
pic_result_filename = 'pic_result.jpg'
pic_current = os.path.join(RESULT_PIC_FILE, pic_result_filename)
return pic_current
with gr.Blocks() as demo:
gr.Markdown("图片和base64互转,该上传图片页面支持上传、拍照和截图进行解码!") # 使用 Markdown 输出一句话
with gr.Tab("图片转BASE64"): # 新建一个 Tab
with gr.Row(): # 同一行排列
inputs = gr.Image(type="numpy", label="上传图片"),
outputs = gr.Textbox(label="Base64结果"),
title = "图片转base64",
description = "上传图片并转化为base64字符",
image_button = gr.Button("上传")
with gr.Row():
outputs_bs64 = gr.components.File(label="下载文件")
upload_button = gr.Button("刷新后点击右边进行下载")
with gr.Tab("BASE64转图片"): # 新建一个 Tab
with gr.Row(): # 同一行排列
bs64_inputs = gr.Textbox(label="Base64编码"),
bs64_outputs = gr.Image(type="numpy", label="查看图片"),
upload_bs64_button = gr.Button("上传")
with gr.Row():
outputs_pic = gr.components.File(label="下载文件")
download_button = gr.Button("刷新后点击右边进行下载图片")
image_button.click(fn=save_pic, inputs=inputs, outputs=outputs) # 按钮绑定相应的槽函数
upload_button.click(fn=bs64_path, outputs=outputs_bs64)
upload_bs64_button.click(fn=save_base64, inputs=bs64_inputs, outputs=bs64_outputs)
download_button.click(fn=pic_path, outputs=outputs_pic)
from selenium.webdriver import Chrome, ChromeOptions
import os, requests, time
def make_chrome_driver():
options = ChromeOptions()
browser = Chrome(options=options)
return browser
def run_server():
demo.launch(server_name='127.0.0.1', server_port=8080, share=True, debug=True)
def run(host, port):
url = "http://" + host + ":" + port
browser = make_chrome_driver()
browser.get(url)
browser.maximize_window()
is_get = input("按任意键退出....")
browser.close()
# 启动Gradio应用
if __name__ == '__main__':
thread1 = threading.Thread(target=run_server, args=())
thread1.start()
time.sleep(5)
thread2 = threading.Thread(target=run, args=("127.0.0.1", "8080"))
thread2.start()
thread1.join()
thread2.join()
访问127.0.0.1:8080
测试效果: