{"id":83,"date":"2024-08-06T21:09:45","date_gmt":"2024-08-06T13:09:45","guid":{"rendered":"https:\/\/taupis.com\/?p=83"},"modified":"2026-08-06T21:13:49","modified_gmt":"2026-08-06T13:13:49","slug":"python%e5%8e%8b%e7%bc%a9%e5%9b%be%e7%89%87%e8%b4%a8%e9%87%8f","status":"publish","type":"post","link":"https:\/\/taupis.com\/?p=83","title":{"rendered":"python\u538b\u7f29\u56fe\u7247\u8d28\u91cf"},"content":{"rendered":"<pre><code>import tkinter as tk\r\nfrom tkinter import filedialog, messagebox\r\nimport cv2\r\nimport numpy as np\r\nimport os\r\nimport logging\r\n\r\n# \u914d\u7f6e\u65e5\u5fd7\r\nlogging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')\r\n\r\ndef normalize_path(path):\r\n    \"\"\"\u5c06\u8def\u5f84\u4e2d\u7684\u6240\u6709\u53cd\u659c\u6760\u66ff\u6362\u4e3a\u6b63\u659c\u6760\uff0c\u7528\u4e8e\u663e\u793a\u6216\u8bb0\u5f55\u65e5\u5fd7\"\"\"\r\n    return path.replace('\\\\', '\/')\r\n\r\ndef compress_image(img_path, quality):\r\n    try:\r\n        normalized_path = normalize_path(img_path)\r\n        logging.info(f\"\u5f00\u59cb\u538b\u7f29\u56fe\u7247: {normalized_path}\")\r\n        img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), cv2.IMREAD_UNCHANGED)\r\n        if img is None:\r\n            logging.error(f\"\u65e0\u6cd5\u4ece {normalized_path} \u52a0\u8f7d\u56fe\u7247\")\r\n            return f\"\u65e0\u6cd5\u4ece {normalized_path} \u52a0\u8f7d\u56fe\u7247\"\r\n        _, im_encoded = cv2.imencode('.jpg', img, [int(cv2.IMWRITE_JPEG_QUALITY), quality])\r\n        im_bytes = im_encoded.tobytes()\r\n        logging.info(f\"\u56fe\u7247 {normalized_path} \u538b\u7f29\u6210\u529f\")\r\n        return im_bytes\r\n    except Exception as e:\r\n        logging.error(f\"\u538b\u7f29\u56fe\u7247 {normalized_path} \u65f6\u53d1\u751f\u9519\u8bef: {e}\")\r\n        return f\"\u538b\u7f29\u56fe\u7247 {normalized_path} \u65f6\u53d1\u751f\u9519\u8bef: {e}\"\r\n\r\ndef save_compressed_image(img_bytes, output_path):\r\n    try:\r\n        normalized_path = normalize_path(output_path)\r\n        with open(normalized_path, 'wb') as f:\r\n            f.write(img_bytes)\r\n        logging.info(f\"\u56fe\u7247\u5df2\u538b\u7f29\u5e76\u4fdd\u5b58\u5230 {normalized_path}\")\r\n        return f\"\u56fe\u7247\u5df2\u538b\u7f29\u5e76\u4fdd\u5b58\u5230 {normalized_path}\"\r\n    except Exception as e:\r\n        logging.error(f\"\u4fdd\u5b58\u538b\u7f29\u540e\u7684\u56fe\u7247\u5230 {normalized_path} \u65f6\u53d1\u751f\u9519\u8bef: {e}\")\r\n        return f\"\u4fdd\u5b58\u538b\u7f29\u540e\u7684\u56fe\u7247\u5230 {normalized_path} \u65f6\u53d1\u751f\u9519\u8bef: {e}\"\r\n\r\ndef select_input_path():\r\n    global input_entry, input_path\r\n    input_path = filedialog.askdirectory(title=\"\u9009\u62e9\u56fe\u7247\u6587\u4ef6\u5939\")\r\n    if input_path:\r\n        input_entry.delete(0, tk.END)\r\n        input_entry.insert(0, normalize_path(input_path))\r\n        logging.info(f\"\u8f93\u5165\u8def\u5f84\u8bbe\u7f6e\u4e3a: {normalize_path(input_path)}\")\r\n\r\ndef select_output_path():\r\n    global output_entry, output_path\r\n    output_path = filedialog.askdirectory(title=\"\u9009\u62e9\u8f93\u51fa\u76ee\u5f55\")\r\n    if output_path:\r\n        output_entry.delete(0, tk.END)\r\n        output_entry.insert(0, normalize_path(output_path))\r\n        logging.info(f\"\u8f93\u51fa\u8def\u5f84\u8bbe\u7f6e\u4e3a: {normalize_path(output_path)}\")\r\n\r\ndef compress_files():\r\n    global quality_entry, input_path, output_path, process_text\r\n    quality = int(quality_entry.get())\r\n\r\n    if not input_path or not output_path:\r\n        messagebox.showerror(\"\u9519\u8bef\", \"\u8bf7\u9009\u62e9\u8f93\u5165\u6587\u4ef6\u5939\u548c\u8f93\u51fa\u76ee\u5f55\")\r\n        logging.error(\"\u672a\u9009\u62e9\u8f93\u5165\u6587\u4ef6\u5939\u6216\u8f93\u51fa\u76ee\u5f55\")\r\n        return\r\n\r\n    if quality &lt; 1 or quality &gt; 100:\r\n        messagebox.showerror(\"\u9519\u8bef\", \"\u538b\u7f29\u8d28\u91cf\u5fc5\u987b\u57281\u5230100\u4e4b\u95f4\")\r\n        logging.error(\"\u538b\u7f29\u8d28\u91cf\u8bbe\u7f6e\u9519\u8bef\")\r\n        return\r\n\r\n    process_text.delete(1.0, tk.END)\r\n    process_text.insert(tk.END, \"\u5f00\u59cb\u538b\u7f29\u56fe\u7247...\\n\")\r\n\r\n    img_list = [os.path.join(input_path, f) for f in os.listdir(input_path) if f.lower().endswith(('.jpg', '.png', '.jpeg'))]\r\n    for img_path in img_list:\r\n        img_bytes = compress_image(img_path, quality)\r\n        if isinstance(img_bytes, bytes):\r\n            file_name = os.path.basename(img_path)\r\n            output_img_path = os.path.join(output_path, file_name)\r\n            save_compressed_image(img_bytes, output_img_path)\r\n            process_text.insert(tk.END, f\"\u56fe\u7247 {normalize_path(file_name)} \u5df2\u538b\u7f29\u5e76\u4fdd\u5b58\u5230 {normalize_path(output_img_path)}\\n\")\r\n        else:\r\n            process_text.insert(tk.END, img_bytes + \"\\n\")\r\n\r\ndef main():\r\n    global input_entry, output_entry, quality_entry, process_text, input_path, output_path\r\n    input_path, output_path = \"\", \"\"\r\n    root = tk.Tk()\r\n    root.title(\"\u56fe\u7247\u6279\u91cf\u538b\u7f29\u5de5\u5177\")\r\n\r\n    # \u8bbe\u7f6e\u5b57\u4f53\u4e3a\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\r\n    root.option_add(\"*Font\", (\"SimHei\", 10))\r\n\r\n    tk.Label(root, text=\"\u8f93\u5165\u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84:\").pack()\r\n    input_entry = tk.Entry(root, width=50)\r\n    input_entry.pack()\r\n    input_button = tk.Button(root, text=\"\u6d4f\u89c8\", command=select_input_path)\r\n    input_button.pack()\r\n\r\n    tk.Label(root, text=\"\u8f93\u51fa\u76ee\u5f55:\").pack()\r\n    output_entry = tk.Entry(root, width=50)\r\n    output_entry.pack()\r\n    output_button = tk.Button(root, text=\"\u6d4f\u89c8\", command=select_output_path)\r\n    output_button.pack()\r\n\r\n    tk.Label(root, text=\"\u538b\u7f29\u8d28\u91cf (1-100):\").pack()\r\n    quality_entry = tk.Entry(root, width=10)\r\n    quality_entry.pack()\r\n\r\n    compress_button = tk.Button(root, text=\"\u5f00\u59cb\u538b\u7f29\", command=compress_files)\r\n    compress_button.pack()\r\n\r\n    process_text = tk.Text(root, height=10, width=50)\r\n    process_text.pack()\r\n\r\n    logging.info(\"\u7a0b\u5e8f\u542f\u52a8\")\r\n    root.mainloop()\r\n    logging.info(\"\u7a0b\u5e8f\u9000\u51fa\")\r\n\r\nif __name__ == \"__main__\":\r\n    main()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import tkinter as tk from tkinter import filedialog, me [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":87,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[13],"class_list":["post-83","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"en_md_date":"2024\u5e748\u67086\u65e5","pos_read":"","redbag":"","public_key":"","account_url":"","pos_notice":"","fr_videp_if":[],"fr_videp_integral":[],"if_rec_down_video":[],"if_rec_down_integral":[],"ad_number":"","fr_hide":"","fr_down_title":"","fr_down_style":"","fr_down_url":[],"fr_down_key":[],"fr_down_file":"","fr_down_format":"","wxvideo_id":"","wxvideo_feedid":"","bz_video_av":"","red_pay":[],"thumbnailurl":"https:\/\/taupis.com\/wp-content\/uploads\/2026\/08\/20260806131202124270-150x150.webp","post_views":"1","_links":{"self":[{"href":"https:\/\/taupis.com\/index.php?rest_route=\/wp\/v2\/posts\/83","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/taupis.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/taupis.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/taupis.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/taupis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=83"}],"version-history":[{"count":1,"href":"https:\/\/taupis.com\/index.php?rest_route=\/wp\/v2\/posts\/83\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/taupis.com\/index.php?rest_route=\/wp\/v2\/posts\/83\/revisions\/85"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/taupis.com\/index.php?rest_route=\/wp\/v2\/media\/87"}],"wp:attachment":[{"href":"https:\/\/taupis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/taupis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/taupis.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}