当前位置:首页>python>Python | 超实用小工具,手机照片无损一键传到电脑!

Python | 超实用小工具,手机照片无损一键传到电脑!

  • 2026-10-11 08:12:48
Python | 超实用小工具,手机照片无损一键传到电脑!

写在前面

目前本人的工作需要借助手机拍摄大量照片,怎么无损发送到电脑成了一个大难题,不想登录第三方软件,直接传照片就行。市面上的各种软件都需要付费,或者有广告,均不能满足我的需求,刚好现在AI发展这么迅速,于是就有了这篇分享,一起来看看我做了一个什么小工具吧

注:移动终端跟PC端需同一个局域网,简单的说就是需要连接到同一个WIFI,不然用不了!

效果如下

上传界面,上传成功

一、生成代码

借助AI工具生成Python代码,并不断优化代码。(此处我们选择Python工具)

生成工具代码,生成打包命令

打包命名:

py -3 -m PyInstaller -F -w -i logo.ico AirPic.py

最终选用代码:

from flask import Flask, request, render_template_stringimport qrcodeimport socketimport osfrom datetime import datetimeimport threadingimport sysapp = Flask(__name__)# 路径处理if getattr(sys, 'frozen', False):    APP_FOLDER = os.path.dirname(sys.executable)else:    APP_FOLDER = os.path.dirname(os.path.abspath(__file__))SAVE_FOLDER = os.path.join(APP_FOLDER, "photos")os.makedirs(SAVE_FOLDER, exist_ok=True)# 前端:增加上传状态、加载提示、防重复点击 + 成功弹窗UPLOAD_PAGE = """<!DOCTYPE html><html><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>PicLink 图传</title><style>*{box-sizing:border-box;margin:0;padding:0}body{background:#f5f5f7;font-family:Arial;padding:30px}.container{max-width:400px;margin:0 auto;background:white;border-radius:20px;padding:30px;box-shadow:0 2px 15px rgba(0,0,0,0.05)}h2{text-align:center;margin-bottom:20px}input{width:100%;padding:12px;margin:15px 0;border:1px solid #ddd;border-radius:12px}.btn-row{display:flex;gap:10px;margin-top:10px}button{flex:1;background:#007aff;color:white;border:none;padding:14px;border-radius:12px;font-size:16px;transition:all 0.2s}button.clear{background:#ff3b30}button:disabled{background:#999;cursor:not-allowed;opacity:0.8}.tip{text-align:center;margin:10px 0;color:#333;font-size:14px}.status-text{text-align:center;margin:15px 0;font-size:14px;color:#666;height:20px}.copyright{text-align:center;margin-top:30px;color:#999;font-size:14px;cursor:pointer}</style></head><body><divclass="container"><h2>📤 PicLink 图传</h2><inputtype="file"id="photoFile"accept="image/*"multiple><divclass="tip"id="fileTip">未选择图片</div><divclass="status-text"id="statusText"></div><divclass="btn-row">    <buttonid="uploadBtn"onclick="uploadFile()">确认上传</button>    <buttonclass="clear"onclick="clearFile()">清除选择</button></div></div><divclass="copyright"onclick="window.open('https://blog.csdn.net/weixin_45386955','_blank')">©2026 周国波</div><script>let fileList = [];let isUploading = false;const fileInput = document.getElementById('photoFile');const fileTip = document.getElementById('fileTip');const statusText = document.getElementById('statusText');const uploadBtn = document.getElementById('uploadBtn');// 监听文件选择fileInput.addEventListener('change', function(){    fileList = Array.from(this.files);    if(fileList.length > 0){        fileTip.innerText = "已选中:" + fileList.length + " 张图片";    }else{        fileTip.innerText = "未选择图片";    }    statusText.innerText = "";});// 上传async function uploadFile(){    // 防止重复点击    if(isUploading) return;    if(fileList.length === 0){        alert("请先选择图片");        return;    }    // 上传中锁定按钮    isUploading = true;    uploadBtn.disabled = true;    statusText.innerText = "⏳ 上传中,请稍候...";    const formData = new FormData();    fileList.forEach(file=>{        formData.append("photo", file);    });    try {        const res = await fetch("/upload", {            method: "POST",            body: formData        });        if(res.ok){            // ✅ 成功弹窗 + 清空            alert("✅ 上传成功!");            clearFile();        }else{            alert("❌ 上传失败!");        }    } catch (err) {        alert("❌ 网络异常,上传失败");    }    // 解锁    isUploading = false;    uploadBtn.disabled = false;    statusText.innerText = "";}// 清除选中文件function clearFile(){    fileInput.value = '';    fileList = [];    fileTip.innerText = "未选择图片";    statusText.innerText = "";}</script></body></html>"""@app.route("/")def index():    return UPLOAD_PAGE@app.route("/upload", methods=["POST"])def upload():    files = request.files.getlist("photo")    for file in files:        if file and file.filename:            filename = datetime.now().strftime("%Y%m%d_%H%M%S_") + file.filename            save_path = os.path.join(SAVE_FOLDER, filename)            file.save(save_path)    return "ok"# 获取局域网IPdef get_ip():    try:        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)        s.connect(("8.8.8.8", 80))        ip = s.getsockname()[0]        s.close()        return ip    except:        return "127.0.0.1"# 显示二维码def show_qr():    import time    time.sleep(1)    ip = get_ip()    url = f"http://{ip}:5000"    print("="*50)    print("PicLink 图传服务已启动")    print("访问地址:", url)    print("图片保存路径:", SAVE_FOLDER)    print("="*50)    qrcode.make(url).show()if __name__ == "__main__":    threading.Thread(target=show_qr, daemon=True).start()    app.run(host="0.0.0.0", port=5000, debug=False, use_reloader=False)

二、软件打包

将上一步准备的代码保存为AirPic.py(AirPic可以替换你喜欢的名字),并按下图依次执行,即可获得安装包。

1.将代码和logo存到同一个文件夹

2.命令行跳转到该文件夹

3.执行“py -3 -m PyInstaller -F -w -i logo.ico AirPic.py”打包命令

我们需要的安装包就在dist文件夹里面

三、安装测试

将AirPic.exe安装包双击打开,此时会自动弹出一个二维码,并在同一位置生成“photos”文件夹(以后照片都会在这个文件夹里面)。然后手机扫描这个二维码,会弹出上传界面。

上传界面,上传成功

此时照片就可以在“photos”文件夹中查看

注:需连接到与电脑同一个局域网的WiFi

电脑端,手机端

至此,这个小工具就完成了,欢迎大家来体验!

声明:以上内容均为交流学习,不涉及商用

相关推荐

uniapp | 微信小程序玩法—激励广告

uniapp | 微信小程序通知、订阅消息—私发/群发

最新文章

随机文章