{"id":1094191,"date":"2025-01-08T14:38:41","date_gmt":"2025-01-08T06:38:41","guid":{"rendered":""},"modified":"2025-01-08T14:38:43","modified_gmt":"2025-01-08T06:38:43","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80%e5%9b%be%e5%83%8f%e6%96%87%e4%bb%b6%e5%a4%b9-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094191.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\u5939"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210418\/321a7dd3-9dd1-4617-a520-26041c505d73.webp\" alt=\"python\u5982\u4f55\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\u5939\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\u5939\u5e76\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u3001\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\u540d\u3001\u52a0\u8f7d\u56fe\u50cf\u7b49\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001glob\u6a21\u5757\u548cPIL\u5e93\u3002<\/strong> \u4f7f\u7528\u8fd9\u4e9b\u6a21\u5757\u548c\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u904d\u5386\u56fe\u50cf\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u5bf9\u8fd9\u4e9b\u56fe\u50cf\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u51e0\u79cd\u65b9\u6cd5\u6765\u6253\u5f00\u548c\u5904\u7406\u56fe\u50cf\u6587\u4ef6\u5939\u4e2d\u7684\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757<\/p>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u81ea\u5e26\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u53ef\u4ee5\u7528\u4e8e\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u5305\u62ec\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from PIL import Image<\/p>\n<p>def open_image_folder(folder_path):<\/p>\n<p>    # \u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/p>\n<p>    for filename in os.listdir(folder_path):<\/p>\n<p>        # \u6784\u5efa\u56fe\u50cf\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84<\/p>\n<p>        img_path = os.path.join(folder_path, filename)<\/p>\n<p>        try:<\/p>\n<p>            # \u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/p>\n<p>            with Image.open(img_path) as img:<\/p>\n<p>                # \u5904\u7406\u56fe\u50cf\uff0c\u4f8b\u5982\u663e\u793a\u56fe\u50cf\u4fe1\u606f<\/p>\n<p>                print(f&quot;Image: {filename}, Size: {img.size}, Format: {img.format}&quot;)<\/p>\n<p>        except IOError:<\/p>\n<p>            print(f&quot;Cannot open {filename}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>os.listdir<\/code>\u51fd\u6570\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>os.path.join<\/code>\u6784\u5efa\u56fe\u50cf\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528PIL\u5e93\u7684<code>Image.open<\/code>\u51fd\u6570\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u5bf9\u56fe\u50cf\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0cPIL\u5e93\uff08Pillow\uff09\u662f\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u7684<code>Image<\/code>\u7c7b\u6765\u6253\u5f00\u548c\u5904\u7406\u56fe\u50cf\u6587\u4ef6\u3002<code>Image.open<\/code>\u51fd\u6570\u7528\u4e8e\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u56fe\u50cf\u5bf9\u8c61\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u56fe\u50cf\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u6765\u83b7\u53d6\u56fe\u50cf\u7684\u4fe1\u606f\u548c\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u3002\u4f8b\u5982\uff0c<code>img.size<\/code>\u8fd4\u56de\u56fe\u50cf\u7684\u5c3a\u5bf8\uff0c<code>img.format<\/code>\u8fd4\u56de\u56fe\u50cf\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528glob\u6a21\u5757<\/p>\n<\/p>\n<p><p>glob\u6a21\u5757\u662fPython\u81ea\u5e26\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u7528\u4e8e\u67e5\u627e\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6\u8def\u5f84\u540d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528glob\u6a21\u5757\u904d\u5386\u56fe\u50cf\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<p>from PIL import Image<\/p>\n<p>def open_image_folder(folder_path):<\/p>\n<p>    # \u67e5\u627e\u56fe\u50cf\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u6587\u4ef6<\/p>\n<p>    for img_path in glob.glob(os.path.join(folder_path, &quot;*.jpg&quot;)):<\/p>\n<p>        try:<\/p>\n<p>            # \u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/p>\n<p>            with Image.open(img_path) as img:<\/p>\n<p>                # \u5904\u7406\u56fe\u50cf\uff0c\u4f8b\u5982\u663e\u793a\u56fe\u50cf\u4fe1\u606f<\/p>\n<p>                print(f&quot;Image: {os.path.basename(img_path)}, Size: {img.size}, Format: {img.format}&quot;)<\/p>\n<p>        except IOError:<\/p>\n<p>            print(f&quot;Cannot open {os.path.basename(img_path)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>glob.glob<\/code>\u51fd\u6570\u67e5\u627e\u56fe\u50cf\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>os.path.join<\/code>\u6784\u5efa\u56fe\u50cf\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528PIL\u5e93\u7684<code>Image.open<\/code>\u51fd\u6570\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u5bf9\u56fe\u50cf\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>glob\u6a21\u5757\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u901a\u914d\u7b26\u6a21\u5f0f\u6765\u67e5\u627e\u6587\u4ef6\u3002\u4f8b\u5982\uff0c<code>*.jpg<\/code>\u8868\u793a\u67e5\u627e\u6240\u6709\u4ee5<code>.jpg<\/code>\u7ed3\u5c3e\u7684\u6587\u4ef6\u3002<code>glob.glob<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u7b26\u5408\u6a21\u5f0f\u7684\u6587\u4ef6\u8def\u5f84\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u8fd9\u4e2a\u5217\u8868\uff0c\u5e76\u4f7f\u7528PIL\u5e93\u7684<code>Image.open<\/code>\u51fd\u6570\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528scandir\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>os.scandir<\/code>\u51fd\u6570\u662fPython 3.5\u5f15\u5165\u7684\u65b0\u51fd\u6570\uff0c\u7528\u4e8e\u9ad8\u6548\u5730\u904d\u5386\u76ee\u5f55\u4e2d\u7684\u6761\u76ee\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a<code>ScandirIterator<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7528\u4e8e\u8fed\u4ee3\u76ee\u5f55\u4e2d\u7684\u6761\u76ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from PIL import Image<\/p>\n<p>def open_image_folder(folder_path):<\/p>\n<p>    # \u4f7f\u7528os.scandir\u9ad8\u6548\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6<\/p>\n<p>    with os.scandir(folder_path) as entries:<\/p>\n<p>        for entry in entries:<\/p>\n<p>            if entry.is_file() and entry.name.endswith(&#39;.jpg&#39;):<\/p>\n<p>                try:<\/p>\n<p>                    # \u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/p>\n<p>                    with Image.open(entry.path) as img:<\/p>\n<p>                        # \u5904\u7406\u56fe\u50cf\uff0c\u4f8b\u5982\u663e\u793a\u56fe\u50cf\u4fe1\u606f<\/p>\n<p>                        print(f&quot;Image: {entry.name}, Size: {img.size}, Format: {img.format}&quot;)<\/p>\n<p>                except IOError:<\/p>\n<p>                    print(f&quot;Cannot open {entry.name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>os.scandir<\/code>\u51fd\u6570\u9ad8\u6548\u5730\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>entry.is_file()<\/code>\u548c<code>entry.name.endswith(&#39;.jpg&#39;)<\/code>\u6765\u5224\u65ad\u6587\u4ef6\u662f\u5426\u4e3a\u56fe\u50cf\u6587\u4ef6\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528PIL\u5e93\u7684<code>Image.open<\/code>\u51fd\u6570\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u5bf9\u56fe\u50cf\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>os.scandir<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a<code>ScandirIterator<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7528\u4e8e\u8fed\u4ee3\u76ee\u5f55\u4e2d\u7684\u6761\u76ee\u3002\u6bcf\u4e2a\u6761\u76ee\u90fd\u662f\u4e00\u4e2a<code>DirEntry<\/code>\u5bf9\u8c61\uff0c\u5177\u6709\u591a\u4e2a\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4f8b\u5982<code>name<\/code>\u3001<code>path<\/code>\u3001<code>is_file<\/code>\u3001<code>is_dir<\/code>\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\u6765\u5224\u65ad\u6761\u76ee\u662f\u5426\u4e3a\u6587\u4ef6\uff0c\u5e76\u83b7\u53d6\u6587\u4ef6\u7684\u540d\u79f0\u548c\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528Pathlib\u6a21\u5757<\/p>\n<\/p>\n<p><p>Pathlib\u6a21\u5757\u662fPython 3.4\u5f15\u5165\u7684\u65b0\u6a21\u5757\uff0c\u7528\u4e8e\u9762\u5411\u5bf9\u8c61\u5730\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pathlib\u6a21\u5757\u904d\u5386\u56fe\u50cf\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>from PIL import Image<\/p>\n<p>def open_image_folder(folder_path):<\/p>\n<p>    # \u4f7f\u7528Pathlib\u6a21\u5757\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u6587\u4ef6<\/p>\n<p>    folder = Path(folder_path)<\/p>\n<p>    for img_path in folder.glob(&quot;*.jpg&quot;):<\/p>\n<p>        try:<\/p>\n<p>            # \u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/p>\n<p>            with Image.open(img_path) as img:<\/p>\n<p>                # \u5904\u7406\u56fe\u50cf\uff0c\u4f8b\u5982\u663e\u793a\u56fe\u50cf\u4fe1\u606f<\/p>\n<p>                print(f&quot;Image: {img_path.name}, Size: {img.size}, Format: {img.format}&quot;)<\/p>\n<p>        except IOError:<\/p>\n<p>            print(f&quot;Cannot open {img_path.name}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pathlib\u6a21\u5757\u7684<code>Path<\/code>\u7c7b\u521b\u5efa\u6587\u4ef6\u5939\u8def\u5f84\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>glob<\/code>\u65b9\u6cd5\u67e5\u627e\u56fe\u50cf\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u6587\u4ef6\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528PIL\u5e93\u7684<code>Image.open<\/code>\u51fd\u6570\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u5bf9\u56fe\u50cf\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>Pathlib\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u5f55\u3002<code>Path<\/code>\u7c7b\u8868\u793a\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u8def\u5f84\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u6587\u4ef6\u5939\u8def\u5f84\u5bf9\u8c61\u3002<code>glob<\/code>\u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6\u8def\u5f84\u3002\u4f8b\u5982\uff0c<code>*.jpg<\/code>\u8868\u793a\u67e5\u627e\u6240\u6709\u4ee5<code>.jpg<\/code>\u7ed3\u5c3e\u7684\u6587\u4ef6\u3002<code>glob<\/code>\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u7b26\u5408\u6a21\u5f0f\u7684\u6587\u4ef6\u8def\u5f84\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u8fd9\u4e2a\u5217\u8868\uff0c\u5e76\u4f7f\u7528PIL\u5e93\u7684<code>Image.open<\/code>\u51fd\u6570\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u8bfb\u53d6\u56fe\u50cf\u5e76\u8fdb\u884c\u5904\u7406<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5bf9\u56fe\u50cf\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\uff0c\u4f8b\u5982\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\u3001\u88c1\u526a\u56fe\u50cf\u3001\u65cb\u8f6c\u56fe\u50cf\u7b49\u3002PIL\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>def process_image(img_path):<\/p>\n<p>    try:<\/p>\n<p>        # \u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/p>\n<p>        with Image.open(img_path) as img:<\/p>\n<p>            # \u663e\u793a\u56fe\u50cf\u4fe1\u606f<\/p>\n<p>            print(f&quot;Original Image: {img_path}, Size: {img.size}, Format: {img.format}&quot;)<\/p>\n<p>            # \u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/p>\n<p>            resized_img = img.resize((200, 200))<\/p>\n<p>            print(f&quot;Resized Image: {img_path}, Size: {resized_img.size}&quot;)<\/p>\n<p>            # \u88c1\u526a\u56fe\u50cf<\/p>\n<p>            cropped_img = img.crop((50, 50, 150, 150))<\/p>\n<p>            print(f&quot;Cropped Image: {img_path}, Size: {cropped_img.size}&quot;)<\/p>\n<p>            # \u65cb\u8f6c\u56fe\u50cf<\/p>\n<p>            rotated_img = img.rotate(45)<\/p>\n<p>            print(f&quot;Rotated Image: {img_path}, Size: {rotated_img.size}&quot;)<\/p>\n<p>    except IOError:<\/p>\n<p>        print(f&quot;Cannot open {img_path}&quot;)<\/p>\n<p>def open_image_folder(folder_path):<\/p>\n<p>    for img_path in glob.glob(os.path.join(folder_path, &quot;*.jpg&quot;)):<\/p>\n<p>        process_image(img_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>process_image<\/code>\u51fd\u6570\u6765\u5904\u7406\u56fe\u50cf\u3002\u8fd9\u4e2a\u51fd\u6570\u9996\u5148\u4f7f\u7528<code>Image.open<\/code>\u51fd\u6570\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u663e\u793a\u56fe\u50cf\u4fe1\u606f\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>resize<\/code>\u65b9\u6cd5\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\uff0c\u4f7f\u7528<code>crop<\/code>\u65b9\u6cd5\u88c1\u526a\u56fe\u50cf\uff0c\u4f7f\u7528<code>rotate<\/code>\u65b9\u6cd5\u65cb\u8f6c\u56fe\u50cf\uff0c\u5e76\u663e\u793a\u5904\u7406\u540e\u7684\u56fe\u50cf\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>PIL\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<code>resize<\/code>\u65b9\u6cd5\u7528\u4e8e\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\uff0c\u63a5\u53d7\u4e00\u4e2a\u5305\u542b\u65b0\u5c3a\u5bf8\u7684\u5143\u7ec4\u4f5c\u4e3a\u53c2\u6570\u3002<code>crop<\/code>\u65b9\u6cd5\u7528\u4e8e\u88c1\u526a\u56fe\u50cf\uff0c\u63a5\u53d7\u4e00\u4e2a\u5305\u542b\u5de6\u3001\u4e0a\u3001\u53f3\u3001\u4e0b\u5750\u6807\u7684\u5143\u7ec4\u4f5c\u4e3a\u53c2\u6570\u3002<code>rotate<\/code>\u65b9\u6cd5\u7528\u4e8e\u65cb\u8f6c\u56fe\u50cf\uff0c\u63a5\u53d7\u4e00\u4e2a\u65cb\u8f6c\u89d2\u5ea6\u4f5c\u4e3a\u53c2\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5bf9\u56fe\u50cf\u8fdb\u884c\u591a\u79cd\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u5230\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u3001glob\u6a21\u5757\u3001scandir\u51fd\u6570\u3001Pathlib\u6a21\u5757\u7b49\u591a\u79cd\u65b9\u5f0f\u6765\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\u5939\uff0c\u5e76\u4f7f\u7528PIL\u5e93\u5bf9\u56fe\u50cf\u8fdb\u884c\u5904\u7406\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u4e86\u89e3\u548c\u5904\u7406\u56fe\u50cf\u6587\u4ef6\u5939\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6253\u5f00\u5e76\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u50cf\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>os<\/code>\u6a21\u5757\u7ed3\u5408<code>PIL<\/code>\uff08Python Imaging Library\uff09\u5e93\u6765\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u3002\u9996\u5148\uff0c\u5229\u7528<code>os.listdir()<\/code>\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u540d\uff0c\u7136\u540e\u4f7f\u7528<code>PIL<\/code>\u7684<code>Image.open()<\/code>\u6765\u9010\u4e00\u6253\u5f00\u6bcf\u4e2a\u56fe\u50cf\u6587\u4ef6\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nfrom PIL import Image\n\nfolder_path = &#39;your\/image\/folder\/path&#39;\nfor filename in os.listdir(folder_path):\n    if filename.endswith((&#39;.png&#39;, &#39;.jpg&#39;, &#39;.jpeg&#39;, &#39;.gif&#39;)):  # \u6839\u636e\u9700\u8981\u6dfb\u52a0\u6587\u4ef6\u683c\u5f0f\n        img_path = os.path.join(folder_path, filename)\n        img = Image.open(img_path)\n        img.show()  # \u663e\u793a\u56fe\u50cf\n<\/code><\/pre>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u5904\u7406\u56fe\u50cf\u6587\u4ef6\u5939\u4e2d\u7684\u56fe\u50cf\uff1f<\/strong><br \/>\u5904\u7406\u56fe\u50cf\u6587\u4ef6\u7684\u5e38\u7528\u5e93\u5305\u62ec<code>PIL<\/code>\uff08Pillow\uff09\u3001<code>OpenCV<\/code>\u548c<code>matplotlib<\/code>\u3002<code>PIL<\/code>\u9002\u5408\u7b80\u5355\u7684\u56fe\u50cf\u64cd\u4f5c\uff0c\u5982\u6253\u5f00\u3001\u4fdd\u5b58\u548c\u57fa\u672c\u5904\u7406\uff1b<code>OpenCV<\/code>\u5219\u63d0\u4f9b\u4e86\u66f4\u4e3a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u529f\u80fd\uff1b<code>matplotlib<\/code>\u5219\u5e38\u7528\u4e8e\u56fe\u50cf\u7684\u53ef\u89c6\u5316\u548c\u6570\u636e\u5206\u6790\u3002\u6839\u636e\u4f60\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6279\u91cf\u5904\u7406\u56fe\u50cf\u6587\u4ef6\u5939\u4e2d\u7684\u56fe\u50cf\uff1f<\/strong><br \/>\u4f7f\u7528Python\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6279\u91cf\u5904\u7406\uff0c\u4f8b\u5982\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\u3001\u8f6c\u6362\u683c\u5f0f\u6216\u5e94\u7528\u6ee4\u955c\u3002\u53ef\u4ee5\u5728\u904d\u5386\u6587\u4ef6\u5939\u65f6\uff0c\u6dfb\u52a0\u5904\u7406\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Pillow\u5e93\u8c03\u6574\u6240\u6709\u56fe\u50cf\u5927\u5c0f\uff1a  <\/p>\n<pre><code class=\"language-python\">for filename in os.listdir(folder_path):\n    if filename.endswith((&#39;.png&#39;, &#39;.jpg&#39;, &#39;.jpeg&#39;)):\n        img_path = os.path.join(folder_path, filename)\n        img = Image.open(img_path)\n        img_resized = img.resize((800, 600))  # \u8c03\u6574\u5927\u5c0f\u4e3a800x600\n        img_resized.save(os.path.join(folder_path, &#39;resized_&#39; + filename))  # \u4fdd\u5b58\u4e3a\u65b0\u6587\u4ef6\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u5730\u5bf9\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u8fdb\u884c\u7edf\u4e00\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\u5939\u5e76\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u3001\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\u540d\u3001\u52a0\u8f7d\u56fe\u50cf\u7b49\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528os\u6a21 [&hellip;]","protected":false},"author":3,"featured_media":1094196,"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\/1094191"}],"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=1094191"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094191\/revisions"}],"predecessor-version":[{"id":1094197,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094191\/revisions\/1094197"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094196"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}