{"id":1100233,"date":"2025-01-08T15:38:03","date_gmt":"2025-01-08T07:38:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100233.html"},"modified":"2025-01-08T15:38:05","modified_gmt":"2025-01-08T07:38:05","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80%e5%9b%be%e7%89%87%e6%96%87%e4%bb%b6%e5%a4%b9%e8%b7%af%e5%be%84-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1100233.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f00\u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063555\/27445595-2edb-448c-873f-47a6d7353574.webp\" alt=\"python\u5982\u4f55\u6253\u5f00\u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84\" \/><\/p>\n<p><p> <strong>Python\u6253\u5f00\u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528os\u548cshutil\u5e93\u3001\u4f7f\u7528tkinter\u5e93\u3001\u4f7f\u7528glob\u5e93\u3002<\/strong>  <\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c\u4f7f\u7528tkinter\u5e93\u66f4\u4e3a\u65b9\u4fbf\u5feb\u6377\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u53ef\u4ee5\u8ba9\u7528\u6237\u9009\u62e9\u6587\u4ef6\u5939\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u548cshutil\u5e93<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528os\u548cshutil\u5e93\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u6587\u4ef6\u5939\u8def\u5f84\u7684\u64cd\u4f5c\u548c\u7ba1\u7406\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528os\u548cshutil\u5e93\u6253\u5f00\u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import shutil<\/p>\n<p>def open_image_folder(path):<\/p>\n<p>    if os.path.exists(path):<\/p>\n<p>        os.startfile(path)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;The specified path does not exist: {path}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u8def\u5f84<\/strong><\/h2>\n<p>image_folder_path = &quot;C:\/Users\/YourUsername\/Pictures&quot;<\/p>\n<p>open_image_folder(image_folder_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u68c0\u67e5\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u5b58\u5728\u5219\u4f7f\u7528<code>os.startfile()<\/code>\u6253\u5f00\u6587\u4ef6\u5939\uff0c\u5426\u5219\u6253\u5370\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528tkinter\u5e93<\/h3>\n<\/p>\n<p><p>tkinter\u662fPython\u5185\u7f6e\u7684\u6807\u51c6GUI\u5e93\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u521b\u5efa\u7b80\u5355\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u8ba9\u7528\u6237\u9009\u62e9\u6587\u4ef6\u5939\u8def\u5f84\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from tkinter import filedialog<\/p>\n<p>def select_image_folder():<\/p>\n<p>    root = tk.Tk()<\/p>\n<p>    root.withdraw()  # \u9690\u85cf\u4e3b\u7a97\u53e3<\/p>\n<p>    folder_selected = filedialog.askdirectory()<\/p>\n<p>    if folder_selected:<\/p>\n<p>        print(f&quot;Selected folder: {folder_selected}&quot;)<\/p>\n<p>        return folder_selected<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;No folder selected&quot;)<\/p>\n<p>        return None<\/p>\n<h2><strong>\u9009\u62e9\u6587\u4ef6\u5939<\/strong><\/h2>\n<p>image_folder_path = select_image_folder()<\/p>\n<p>if image_folder_path:<\/p>\n<p>    print(f&quot;Image folder path: {image_folder_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>filedialog.askdirectory()<\/code>\u51fd\u6570\u53ef\u4ee5\u5f39\u51fa\u6587\u4ef6\u5939\u9009\u62e9\u5bf9\u8bdd\u6846\uff0c\u7528\u6237\u9009\u62e9\u6587\u4ef6\u5939\u540e\u8fd4\u56de\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528glob\u5e93<\/h3>\n<\/p>\n<p><p>glob\u5e93\u662fPython\u5185\u7f6e\u7684\u6587\u4ef6\u540d\u6a21\u5f0f\u5339\u914d\u5e93\uff0c\u9002\u7528\u4e8e\u67e5\u627e\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<p>def list_image_files(path):<\/p>\n<p>    image_files = glob.glob(os.path.join(path, &quot;*.jpg&quot;)) + glob.glob(os.path.join(path, &quot;*.png&quot;))<\/p>\n<p>    for file in image_files:<\/p>\n<p>        print(f&quot;Found image file: {file}&quot;)<\/p>\n<p>    return image_files<\/p>\n<h2><strong>\u793a\u4f8b\u8def\u5f84<\/strong><\/h2>\n<p>image_folder_path = &quot;C:\/Users\/YourUsername\/Pictures&quot;<\/p>\n<p>image_files = list_image_files(image_folder_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>glob.glob()<\/code>\u51fd\u6570\u67e5\u627ejpg\u548cpng\u683c\u5f0f\u7684\u56fe\u7247\u6587\u4ef6\uff0c\u5e76\u5c06\u7ed3\u679c\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528PIL\u5e93<\/h3>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u6216Pillow\u5e93\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\u4e4b\u4e00\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\u5e76\u8fdb\u884c\u5904\u7406\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import os<\/p>\n<p>def open_and_show_image(image_path):<\/p>\n<p>    if os.path.exists(image_path):<\/p>\n<p>        with Image.open(image_path) as img:<\/p>\n<p>            img.show()<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;The specified image does not exist: {image_path}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u56fe\u7247\u8def\u5f84<\/strong><\/h2>\n<p>image_path = &quot;C:\/Users\/YourUsername\/Pictures\/sample.jpg&quot;<\/p>\n<p>open_and_show_image(image_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pillow\u5e93\u7684<code>Image.open()<\/code>\u51fd\u6570\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>img.show()<\/code>\u51fd\u6570\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\u548c\u65b9\u6cd5\u6253\u5f00\u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84\uff0c\u5e76\u5bf9\u56fe\u7247\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\u548c\u7ba1\u7406\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u52bf\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u4f7f\u7528tkinter\u5e93\u53ef\u4ee5\u63d0\u4f9b\u7528\u6237\u53cb\u597d\u7684\u754c\u9762\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7528\u6237\u9009\u62e9\u6587\u4ef6\u5939\u7684\u573a\u666f\uff1b\u4f7f\u7528os\u548cshutil\u5e93\u53ef\u4ee5\u8fdb\u884c\u6587\u4ef6\u5939\u8def\u5f84\u7684\u64cd\u4f5c\u548c\u7ba1\u7406\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u6587\u4ef6\u5939\u8fdb\u884c\u6279\u91cf\u5904\u7406\u7684\u573a\u666f\uff1b\u4f7f\u7528glob\u5e93\u53ef\u4ee5\u8fdb\u884c\u6587\u4ef6\u540d\u6a21\u5f0f\u5339\u914d\uff0c\u9002\u7528\u4e8e\u67e5\u627e\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6\uff1b\u4f7f\u7528Pillow\u5e93\u53ef\u4ee5\u8fdb\u884c\u56fe\u50cf\u6587\u4ef6\u7684\u5904\u7406\u548c\u663e\u793a\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u56fe\u50cf\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\u7684\u573a\u666f\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6253\u5f00\u6307\u5b9a\u7684\u56fe\u7247\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u5e93<code>os<\/code>\u6765\u6253\u5f00\u6307\u5b9a\u7684\u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84\u3002\u9996\u5148\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>os.listdir()<\/code>\u51fd\u6570\u5217\u51fa\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7<code>PIL<\/code>\uff08Python Imaging Library\uff09\u5e93\u6765\u5904\u7406\u548c\u663e\u793a\u56fe\u7247\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nfrom PIL import Image\n\nfolder_path = &#39;your_image_folder_path&#39;  # \u66ff\u6362\u4e3a\u4f60\u7684\u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84\nfor file_name in os.listdir(folder_path):\n    if file_name.endswith((&#39;jpg&#39;, &#39;png&#39;, &#39;jpeg&#39;)):  # \u68c0\u67e5\u6587\u4ef6\u7c7b\u578b\n        img_path = os.path.join(folder_path, file_name)\n        img = Image.open(img_path)\n        img.show()  # \u663e\u793a\u56fe\u7247\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u83b7\u53d6\u56fe\u7247\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u7247\u6587\u4ef6\u540d\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>os.listdir()<\/code>\u51fd\u6570\u83b7\u53d6\u6307\u5b9a\u6587\u4ef6\u5939\u5185\u7684\u6240\u6709\u6587\u4ef6\u540d\u3002\u4e3a\u4e86\u7b5b\u9009\u51fa\u56fe\u7247\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u7ed3\u5408<code>str.endswith()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u6587\u4ef6\u6269\u5c55\u540d\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfolder_path = &#39;your_image_folder_path&#39;  # \u66ff\u6362\u4e3a\u4f60\u7684\u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84\nimage_files = [f for f in os.listdir(folder_path) if f.endswith((&#39;jpg&#39;, &#39;png&#39;, &#39;jpeg&#39;))]\nprint(image_files)  # \u8f93\u51fa\u6240\u6709\u56fe\u7247\u6587\u4ef6\u540d\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u6253\u5f00\u56fe\u7247\u6587\u4ef6\u5939\u4e2d\u7684\u56fe\u7247\u5e76\u8fdb\u884c\u5904\u7406\uff1f<\/strong><br \/>\u6253\u5f00\u56fe\u7247\u6587\u4ef6\u5939\u4e2d\u7684\u56fe\u7247\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>PIL<\/code>\u5e93\u8fdb\u884c\u591a\u79cd\u5904\u7406\uff0c\u5982\u8c03\u6574\u5927\u5c0f\u3001\u65cb\u8f6c\u3001\u8fc7\u6ee4\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5904\u7406\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image\nimport os\n\nfolder_path = &#39;your_image_folder_path&#39;  # \u66ff\u6362\u4e3a\u4f60\u7684\u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84\nfor file_name in os.listdir(folder_path):\n    if file_name.endswith((&#39;jpg&#39;, &#39;png&#39;, &#39;jpeg&#39;)):\n        img_path = os.path.join(folder_path, file_name)\n        img = Image.open(img_path)\n        img = img.resize((100, 100))  # \u8c03\u6574\u56fe\u7247\u5927\u5c0f\n        img.save(os.path.join(folder_path, &#39;resized_&#39; + file_name))  # \u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u7247\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u6253\u5f00\u548c\u5904\u7406\u56fe\u7247\u6587\u4ef6\u5939\u4e2d\u7684\u56fe\u7247\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6253\u5f00\u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528os\u548cshutil\u5e93\u3001\u4f7f\u7528tkinter\u5e93\u3001\u4f7f\u7528glob\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1100238,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100233"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1100233"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100233\/revisions"}],"predecessor-version":[{"id":1100240,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100233\/revisions\/1100240"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100238"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1100233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1100233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1100233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}