{"id":1175674,"date":"2025-01-15T17:35:20","date_gmt":"2025-01-15T09:35:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1175674.html"},"modified":"2025-01-15T17:35:23","modified_gmt":"2025-01-15T09:35:23","slug":"python%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e5%a4%9a%e5%bc%a0%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1175674.html","title":{"rendered":"python\u7a0b\u5e8f\u5982\u4f55\u8bfb\u53d6\u591a\u5f20\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25111048\/c9a01230-9f05-4c64-9ded-347c1eb369b2.webp\" alt=\"python\u7a0b\u5e8f\u5982\u4f55\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\" \/><\/p>\n<p><p> <strong>Python\u7a0b\u5e8f\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u904d\u5386\u6587\u4ef6\u5939\u3001\u4f7f\u7528glob\u6a21\u5757\u5339\u914d\u6587\u4ef6\u6a21\u5f0f\u3001\u4f7f\u7528Pillow\u5e93\u52a0\u8f7d\u56fe\u7247\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528os\u6a21\u5757\u904d\u5386\u6587\u4ef6\u5939<\/strong> \u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7os.listdir()\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u6307\u5b9a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u540d\uff0c\u7136\u540e\u901a\u8fc7\u5faa\u73af\u8bfb\u53d6\u56fe\u7247\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528Pillow\u5e93\u7684Image.open()\u51fd\u6570\u6253\u5f00\u548c\u5904\u7406\u56fe\u7247\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528os\u6a21\u5757\u904d\u5386\u6587\u4ef6\u5939\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757\u904d\u5386\u6587\u4ef6\u5939\u8bfb\u53d6\u591a\u5f20\u56fe\u7247<\/h3>\n<\/p>\n<p><p>os\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u975e\u5e38\u4e30\u5bcc\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528os.listdir()\u51fd\u6570\u83b7\u53d6\u6307\u5b9a\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\u540d\uff0c\u518d\u901a\u8fc7\u5faa\u73af\u8bfb\u53d6\u56fe\u7247\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from PIL import Image<\/p>\n<p>def read_images_from_folder(folder_path):<\/p>\n<p>    images = []<\/p>\n<p>    for filename in os.listdir(folder_path):<\/p>\n<p>        if filename.endswith(&quot;.jpg&quot;) or filename.endswith(&quot;.png&quot;):<\/p>\n<p>            img_path = os.path.join(folder_path, filename)<\/p>\n<p>            img = Image.open(img_path)<\/p>\n<p>            images.append(img)<\/p>\n<p>    return images<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>folder_path = &quot;path\/to\/your\/image\/folder&quot;<\/p>\n<p>images = read_images_from_folder(folder_path)<\/p>\n<p>print(f&quot;Total {len(images)} images read from folder.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86os\u548cPIL\uff08Python Imaging Library\uff09\u6a21\u5757\u3002\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>read_images_from_folder<\/code>\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u5939\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528os.listdir()\u51fd\u6570\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u76ee\u5f55\u540d\uff0c\u5e76\u901a\u8fc7\u5faa\u73af\u5224\u65ad\u6587\u4ef6\u540d\u662f\u5426\u4ee5\u201c.jpg\u201d\u6216\u201c.png\u201d\u7ed3\u5c3e\uff0c\u5982\u679c\u662f\uff0c\u5219\u4f7f\u7528Image.open()\u51fd\u6570\u6253\u5f00\u56fe\u7247\u5e76\u6dfb\u52a0\u5230images\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u8fd4\u56de\u5305\u542b\u6240\u6709\u56fe\u7247\u7684images\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528glob\u6a21\u5757\u5339\u914d\u6587\u4ef6\u6a21\u5f0f\u8bfb\u53d6\u591a\u5f20\u56fe\u7247<\/h3>\n<\/p>\n<p><p>glob\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5728\u76ee\u5f55\u4e2d\u67e5\u627e\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u4ef6\u7684\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528glob\u6a21\u5757\u6765\u5339\u914d\u56fe\u7247\u6587\u4ef6\u7684\u6a21\u5f0f\uff0c\u4ece\u800c\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import glob<\/p>\n<p>from PIL import Image<\/p>\n<p>def read_images_with_glob(folder_path):<\/p>\n<p>    images = []<\/p>\n<p>    image_files = glob.glob(f&quot;{folder_path}\/*.jpg&quot;) + glob.glob(f&quot;{folder_path}\/*.png&quot;)<\/p>\n<p>    for img_path in image_files:<\/p>\n<p>        img = Image.open(img_path)<\/p>\n<p>        images.append(img)<\/p>\n<p>    return images<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>folder_path = &quot;path\/to\/your\/image\/folder&quot;<\/p>\n<p>images = read_images_with_glob(folder_path)<\/p>\n<p>print(f&quot;Total {len(images)} images read from folder.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86glob\u548cPIL\u6a21\u5757\u3002\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>read_images_with_glob<\/code>\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u5939\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528glob.glob()\u51fd\u6570\u5339\u914d\u6587\u4ef6\u5939\u4e2d\u6240\u6709\u4ee5\u201c.jpg\u201d\u548c\u201c.png\u201d\u7ed3\u5c3e\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u5e76\u5c06\u5b83\u4eec\u5408\u5e76\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u901a\u8fc7\u5faa\u73af\u8bfb\u53d6\u6bcf\u4e2a\u56fe\u7247\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528Image.open()\u51fd\u6570\u6253\u5f00\u56fe\u7247\u5e76\u6dfb\u52a0\u5230images\u5217\u8868\u4e2d\uff0c\u8fd4\u56de\u5305\u542b\u6240\u6709\u56fe\u7247\u7684images\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pillow\u5e93\u52a0\u8f7d\u56fe\u7247<\/h3>\n<\/p>\n<p><p>Pillow\uff08PIL Fork\uff09\u662fPython\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u6253\u5f00\u3001\u64cd\u4f5c\u548c\u4fdd\u5b58\u8bb8\u591a\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u50cf\u6587\u4ef6\u3002Pillow\u5e93\u7684Image\u6a21\u5757\u63d0\u4f9b\u4e86\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>def read_images_with_pillow(image_paths):<\/p>\n<p>    images = []<\/p>\n<p>    for img_path in image_paths:<\/p>\n<p>        img = Image.open(img_path)<\/p>\n<p>        images.append(img)<\/p>\n<p>    return images<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>image_paths = [&quot;path\/to\/your\/image1.jpg&quot;, &quot;path\/to\/your\/image2.png&quot;, &quot;path\/to\/your\/image3.jpg&quot;]<\/p>\n<p>images = read_images_with_pillow(image_paths)<\/p>\n<p>print(f&quot;Total {len(images)} images read.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86PIL\u6a21\u5757\u3002\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>read_images_with_pillow<\/code>\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u56fe\u7247\u6587\u4ef6\u8def\u5f84\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u5faa\u73af\u8bfb\u53d6\u6bcf\u4e2a\u56fe\u7247\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528Image.open()\u51fd\u6570\u6253\u5f00\u56fe\u7247\u5e76\u6dfb\u52a0\u5230images\u5217\u8868\u4e2d\uff0c\u8fd4\u56de\u5305\u542b\u6240\u6709\u56fe\u7247\u7684images\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u591a\u4e2a\u65b9\u6cd5\u7684\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u591a\u4e2a\u65b9\u6cd5\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u8bfb\u6027\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u4f7f\u7528os\u6a21\u5757\u904d\u5386\u6587\u4ef6\u5939\u83b7\u53d6\u6240\u6709\u56fe\u7247\u6587\u4ef6\u8def\u5f84\uff0c\u7136\u540e\u4f7f\u7528Pillow\u5e93\u52a0\u8f7d\u56fe\u7247\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from PIL import Image<\/p>\n<p>def get_image_files(folder_path, extensions=[&quot;.jpg&quot;, &quot;.png&quot;]):<\/p>\n<p>    image_files = []<\/p>\n<p>    for filename in os.listdir(folder_path):<\/p>\n<p>        if any(filename.endswith(ext) for ext in extensions):<\/p>\n<p>            img_path = os.path.join(folder_path, filename)<\/p>\n<p>            image_files.append(img_path)<\/p>\n<p>    return image_files<\/p>\n<p>def read_images(image_files):<\/p>\n<p>    images = []<\/p>\n<p>    for img_path in image_files:<\/p>\n<p>        img = Image.open(img_path)<\/p>\n<p>        images.append(img)<\/p>\n<p>    return images<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>folder_path = &quot;path\/to\/your\/image\/folder&quot;<\/p>\n<p>image_files = get_image_files(folder_path)<\/p>\n<p>images = read_images(image_files)<\/p>\n<p>print(f&quot;Total {len(images)} images read from folder.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>get_image_files<\/code>\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u5939\u8def\u5f84\u548c\u4e00\u4e2a\u6587\u4ef6\u6269\u5c55\u540d\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\uff0c\u901a\u8fc7os.listdir()\u51fd\u6570\u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u540d\uff0c\u5e76\u5224\u65ad\u6587\u4ef6\u540d\u662f\u5426\u4ee5\u6307\u5b9a\u7684\u6269\u5c55\u540d\u7ed3\u5c3e\uff0c\u5982\u679c\u662f\uff0c\u5219\u5c06\u6587\u4ef6\u8def\u5f84\u6dfb\u52a0\u5230image_files\u5217\u8868\u4e2d\u3002\u63a5\u7740\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>read_images<\/code>\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u56fe\u7247\u6587\u4ef6\u8def\u5f84\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\uff0c\u901a\u8fc7\u5faa\u73af\u8bfb\u53d6\u6bcf\u4e2a\u56fe\u7247\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528Image.open()\u51fd\u6570\u6253\u5f00\u56fe\u7247\u5e76\u6dfb\u52a0\u5230images\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u7ed3\u5408\u4e24\u4e2a\u51fd\u6570\u5b9e\u73b0\u4ece\u6587\u4ef6\u5939\u4e2d\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u8bfb\u53d6\u56fe\u7247\u65f6\u7684\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u56fe\u7247\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6587\u4ef6\u683c\u5f0f\u4e0d\u6b63\u786e\u7b49\u3002\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u8fd9\u4e9b\u5f02\u5e38\u8fdb\u884c\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from PIL import Image<\/p>\n<p>def get_image_files(folder_path, extensions=[&quot;.jpg&quot;, &quot;.png&quot;]):<\/p>\n<p>    image_files = []<\/p>\n<p>    for filename in os.listdir(folder_path):<\/p>\n<p>        if any(filename.endswith(ext) for ext in extensions):<\/p>\n<p>            img_path = os.path.join(folder_path, filename)<\/p>\n<p>            image_files.append(img_path)<\/p>\n<p>    return image_files<\/p>\n<p>def read_images(image_files):<\/p>\n<p>    images = []<\/p>\n<p>    for img_path in image_files:<\/p>\n<p>        try:<\/p>\n<p>            img = Image.open(img_path)<\/p>\n<p>            images.append(img)<\/p>\n<p>        except (FileNotFoundError, IOError) as e:<\/p>\n<p>            print(f&quot;Error reading image {img_path}: {e}&quot;)<\/p>\n<p>    return images<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>folder_path = &quot;path\/to\/your\/image\/folder&quot;<\/p>\n<p>image_files = get_image_files(folder_path)<\/p>\n<p>images = read_images(image_files)<\/p>\n<p>print(f&quot;Total {len(images)} images read from folder.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728<code>read_images<\/code>\u51fd\u6570\u4e2d\u4f7f\u7528try-except\u8bed\u53e5\u6355\u83b7\u5e76\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u5982\u679c\u8bfb\u53d6\u56fe\u7247\u65f6\u53d1\u751fFileNotFoundError\u6216IOError\u5f02\u5e38\uff0c\u5219\u6253\u5370\u9519\u8bef\u4fe1\u606f\u5e76\u7ee7\u7eed\u5904\u7406\u4e0b\u4e00\u4e2a\u56fe\u7247\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5728\u6570\u636e\u5904\u7406\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\u5728\u6570\u636e\u5904\u7406\u548c\u673a\u5668\u5b66\u4e60\u4e2d\u975e\u5e38\u5e38\u89c1\u3002\u4f8b\u5982\uff0c\u5728\u56fe\u50cf\u5206\u7c7b\u3001\u76ee\u6807\u68c0\u6d4b\u3001\u56fe\u50cf\u5206\u5272\u7b49\u4efb\u52a1\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u8bfb\u53d6\u5927\u91cf\u7684\u56fe\u7247\u8fdb\u884c\u8bad\u7ec3\u548c\u6d4b\u8bd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528TensorFlow\u548cKeras\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\u8fdb\u884c\u56fe\u50cf\u5206\u7c7b\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import numpy as np<\/p>\n<p>from PIL import Image<\/p>\n<p>import tensorflow as tf<\/p>\n<p>from tensorflow.keras.preprocessing.image import img_to_array<\/p>\n<p>def get_image_files(folder_path, extensions=[&quot;.jpg&quot;, &quot;.png&quot;]):<\/p>\n<p>    image_files = []<\/p>\n<p>    for filename in os.listdir(folder_path):<\/p>\n<p>        if any(filename.endswith(ext) for ext in extensions):<\/p>\n<p>            img_path = os.path.join(folder_path, filename)<\/p>\n<p>            image_files.append(img_path)<\/p>\n<p>    return image_files<\/p>\n<p>def load_and_preprocess_images(image_files, target_size=(224, 224)):<\/p>\n<p>    images = []<\/p>\n<p>    for img_path in image_files:<\/p>\n<p>        try:<\/p>\n<p>            img = Image.open(img_path)<\/p>\n<p>            img = img.resize(target_size)<\/p>\n<p>            img_array = img_to_array(img)<\/p>\n<p>            img_array = np.expand_dims(img_array, axis=0)<\/p>\n<p>            img_array = tf.keras.applications.mobilenet_v2.preprocess_input(img_array)<\/p>\n<p>            images.append(img_array)<\/p>\n<p>        except (FileNotFoundError, IOError) as e:<\/p>\n<p>            print(f&quot;Error reading image {img_path}: {e}&quot;)<\/p>\n<p>    return np.vstack(images)<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>folder_path = &quot;path\/to\/your\/image\/folder&quot;<\/p>\n<p>image_files = get_image_files(folder_path)<\/p>\n<p>images = load_and_preprocess_images(image_files)<\/p>\n<p>print(f&quot;Total {images.shape[0]} images read and preprocessed.&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u548c\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = tf.keras.applications.MobileNetV2(weights=&quot;imagenet&quot;, include_top=False, input_shape=(224, 224, 3))<\/p>\n<p>model.summary()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86TensorFlow\u548cKeras\u5e93\u3002\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>load_and_preprocess_images<\/code>\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u56fe\u7247\u6587\u4ef6\u8def\u5f84\u5217\u8868\u548c\u76ee\u6807\u56fe\u50cf\u5927\u5c0f\u4f5c\u4e3a\u53c2\u6570\uff0c\u901a\u8fc7\u5faa\u73af\u8bfb\u53d6\u6bcf\u4e2a\u56fe\u7247\u6587\u4ef6\uff0c\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\uff0c\u5e76\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002\u7136\u540e\uff0c\u4f7f\u7528MobileNetV2\u6a21\u578b\u8fdb\u884c\u56fe\u50cf\u5206\u7c7b\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u52a0\u901f\u8bfb\u53d6\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u5927\u91cf\u56fe\u7247\u65f6\uff0c\u5355\u7ebf\u7a0b\u5904\u7406\u53ef\u80fd\u4f1a\u6bd4\u8f83\u6162\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u52a0\u901f\u8bfb\u53d6\u56fe\u7247\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u591a\u7ebf\u7a0b\u8bfb\u53d6\u56fe\u7247\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import threading<\/p>\n<p>from PIL import Image<\/p>\n<p>def get_image_files(folder_path, extensions=[&quot;.jpg&quot;, &quot;.png&quot;]):<\/p>\n<p>    image_files = []<\/p>\n<p>    for filename in os.listdir(folder_path):<\/p>\n<p>        if any(filename.endswith(ext) for ext in extensions):<\/p>\n<p>            img_path = os.path.join(folder_path, filename)<\/p>\n<p>            image_files.append(img_path)<\/p>\n<p>    return image_files<\/p>\n<p>def read_image(img_path, images):<\/p>\n<p>    try:<\/p>\n<p>        img = Image.open(img_path)<\/p>\n<p>        images.append(img)<\/p>\n<p>    except (FileNotFoundError, IOError) as e:<\/p>\n<p>        print(f&quot;Error reading image {img_path}: {e}&quot;)<\/p>\n<p>def read_images_multithreaded(image_files):<\/p>\n<p>    images = []<\/p>\n<p>    threads = []<\/p>\n<p>    for img_path in image_files:<\/p>\n<p>        thread = threading.Thread(target=read_image, args=(img_path, images))<\/p>\n<p>        threads.append(thread)<\/p>\n<p>        thread.start()<\/p>\n<p>    for thread in threads:<\/p>\n<p>        thread.join()<\/p>\n<p>    return images<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>folder_path = &quot;path\/to\/your\/image\/folder&quot;<\/p>\n<p>image_files = get_image_files(folder_path)<\/p>\n<p>images = read_images_multithreaded(image_files)<\/p>\n<p>print(f&quot;Total {len(images)} images read from folder.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>read_image<\/code>\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u56fe\u7247\u6587\u4ef6\u8def\u5f84\u548c\u4e00\u4e2a\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c1d\u8bd5\u8bfb\u53d6\u56fe\u7247\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u63a5\u7740\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>read_images_multithreaded<\/code>\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u56fe\u7247\u6587\u4ef6\u8def\u5f84\u5217\u8868\uff0c\u901a\u8fc7\u591a\u7ebf\u7a0b\u8bfb\u53d6\u56fe\u7247\u3002\u5728\u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u4e3a\u6bcf\u4e2a\u56fe\u7247\u6587\u4ef6\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5e76\u542f\u52a8\u7ebf\u7a0b\u3002\u6700\u540e\uff0c\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210\u540e\uff0c\u8fd4\u56de\u5305\u542b\u6240\u6709\u56fe\u7247\u7684images\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86Python\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u904d\u5386\u6587\u4ef6\u5939\u3001\u4f7f\u7528glob\u6a21\u5757\u5339\u914d\u6587\u4ef6\u6a21\u5f0f\u3001\u4f7f\u7528Pillow\u5e93\u52a0\u8f7d\u56fe\u7247\u7b49\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u5904\u7406\u8bfb\u53d6\u56fe\u7247\u65f6\u7684\u5f02\u5e38\u3001\u5728\u6570\u636e\u5904\u7406\u548c\u673a\u5668\u5b66\u4e60\u4e2d\u7684\u5e94\u7528\u3001\u4ee5\u53ca\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u52a0\u901f\u8bfb\u53d6\u56fe\u7247\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5e93\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecPIL\uff08Pillow\uff09\u3001OpenCV\u548cMatplotlib\u7b49\u3002\u4ee5PIL\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u56fe\u50cf\u6587\u4ef6\u7684\u8def\u5f84\u5217\u8868\uff0c\u4f7f\u7528<code>Image.open()<\/code>\u65b9\u6cd5\u9010\u4e00\u8bfb\u53d6\u6bcf\u5f20\u56fe\u7247\u3002\u786e\u4fdd\u5728\u8bfb\u53d6\u4e4b\u524d\u5df2\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\uff0c\u5e76\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757\u3002<\/p>\n<p><strong>\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6587\u4ef6\u683c\u5f0f\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\u65f6\uff0c\u5e38\u89c1\u7684\u6587\u4ef6\u683c\u5f0f\u5305\u62ecJPEG\u3001PNG\u3001BMP\u548cGIF\u7b49\u3002\u786e\u4fdd\u6240\u4f7f\u7528\u7684\u5e93\u652f\u6301\u8fd9\u4e9b\u683c\u5f0f\u3002Pillow\u5e93\u652f\u6301\u5927\u591a\u6570\u5e38\u89c1\u683c\u5f0f\uff0c\u800cOpenCV\u5219\u66f4\u9002\u5408\u5904\u7406\u89c6\u9891\u548c\u56fe\u50cf\u6d41\u3002\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\u548c\u5e93\uff0c\u53ef\u4ee5\u786e\u4fdd\u56fe\u7247\u8bfb\u53d6\u7684\u987a\u5229\u8fdb\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\u540e\u7684\u6570\u636e\uff1f<\/strong><br \/>\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\u540e\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002\u53ef\u4ee5\u5c06\u56fe\u50cf\u6570\u636e\u5b58\u50a8\u5728\u5217\u8868\u6216\u6570\u7ec4\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u64cd\u4f5c\uff0c\u4f8b\u5982\u56fe\u50cf\u589e\u5f3a\u3001\u7279\u5f81\u63d0\u53d6\u6216\u6a21\u578b\u8bad\u7ec3\u3002\u5982\u679c\u4f7f\u7528NumPy\u6570\u7ec4\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u548c\u56fe\u50cf\u5904\u7406\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u5728\u5904\u7406\u8fc7\u7a0b\u4e2d\u5173\u6ce8\u5185\u5b58\u7ba1\u7406\uff0c\u4ee5\u9632\u6b62\u5927\u89c4\u6a21\u56fe\u50cf\u5904\u7406\u65f6\u51fa\u73b0\u6027\u80fd\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7a0b\u5e8f\u8bfb\u53d6\u591a\u5f20\u56fe\u7247\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528os\u6a21\u5757\u904d\u5386\u6587\u4ef6\u5939\u3001\u4f7f\u7528glob\u6a21\u5757\u5339\u914d\u6587\u4ef6\u6a21\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":1175677,"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\/1175674"}],"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=1175674"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175674\/revisions"}],"predecessor-version":[{"id":1175680,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175674\/revisions\/1175680"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1175677"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1175674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1175674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1175674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}