{"id":1047007,"date":"2024-12-31T13:38:43","date_gmt":"2024-12-31T05:38:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1047007.html"},"modified":"2024-12-31T13:38:46","modified_gmt":"2024-12-31T05:38:46","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%9b%be%e7%89%87%e5%8f%98%e4%b8%ba%e6%95%b0%e6%8d%ae%e9%9b%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1047007.html","title":{"rendered":"python\u5982\u4f55\u5c06\u56fe\u7247\u53d8\u4e3a\u6570\u636e\u96c6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/90d6776d-2202-4ddd-809a-3f56c6c2d3f2.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06\u56fe\u7247\u53d8\u4e3a\u6570\u636e\u96c6\" \/><\/p>\n<p><p> <strong>Python\u5c06\u56fe\u7247\u53d8\u4e3a\u6570\u636e\u96c6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PIL\u5e93\u3001OpenCV\u5e93\u3001TensorFlow\u5e93\u7b49\uff0c\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u6570\u636e\u96c6\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u7528\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001PIL\u5e93<\/p>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u5177\u6709\u6253\u5f00\u3001\u64cd\u4f5c\u548c\u4fdd\u5b58\u8bb8\u591a\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u50cf\u6587\u4ef6\u7684\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u5c06\u56fe\u7247\u53d8\u4e3a\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5PIL\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5PIL\u5e93\u3002PIL\u5e93\u7684\u73b0\u4ee3\u66ff\u4ee3\u54c1\u662fPillow\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8bfb\u53d6\u548c\u5904\u7406\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528PIL\u5e93\u8bfb\u53d6\u548c\u5904\u7406\u56fe\u7247\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import numpy as np<\/p>\n<p>def load_image(image_path):<\/p>\n<p>    img = Image.open(image_path)<\/p>\n<p>    return np.array(img)<\/p>\n<p>image_path = &#39;path\/to\/your\/image.jpg&#39;<\/p>\n<p>image_data = load_image(image_path)<\/p>\n<p>print(image_data.shape)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528PIL\u5e93\u6253\u5f00\u4e00\u5f20\u56fe\u7247\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u6253\u5370\u56fe\u7247\u6570\u636e\u7684\u5f62\u72b6\u6765\u9a8c\u8bc1\u56fe\u7247\u662f\u5426\u6210\u529f\u52a0\u8f7d\u3002<\/p>\n<\/p>\n<p><h3>\u6279\u91cf\u8bfb\u53d6\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u7247\u8bfb\u53d6\u5e76\u8f6c\u6362\u4e3a\u6570\u636e\u96c6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def load_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((&#39;.png&#39;, &#39;.jpg&#39;, &#39;.jpeg&#39;)):<\/p>\n<p>            img = load_image(os.path.join(folder_path, filename))<\/p>\n<p>            if img is not None:<\/p>\n<p>                images.append(img)<\/p>\n<p>    return np.array(images)<\/p>\n<p>folder_path = &#39;path\/to\/your\/folder&#39;<\/p>\n<p>dataset = load_images_from_folder(folder_path)<\/p>\n<p>print(dataset.shape)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u7247\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u52a0\u8f7d\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u4e00\u4e2aNumPy\u6570\u7ec4\uff0c\u5f62\u6210\u6211\u4eec\u7684\u56fe\u7247\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001OpenCV\u5e93<\/p>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5177\u6709\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u5c06\u56fe\u7247\u53d8\u4e3a\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5OpenCV\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5OpenCV\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8bfb\u53d6\u548c\u5904\u7406\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528OpenCV\u5e93\u8bfb\u53d6\u548c\u5904\u7406\u56fe\u7247\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>def load_image(image_path):<\/p>\n<p>    img = cv2.imread(image_path)<\/p>\n<p>    return img<\/p>\n<p>image_path = &#39;path\/to\/your\/image.jpg&#39;<\/p>\n<p>image_data = load_image(image_path)<\/p>\n<p>print(image_data.shape)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528OpenCV\u5e93\u8bfb\u53d6\u4e00\u5f20\u56fe\u7247\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u6253\u5370\u56fe\u7247\u6570\u636e\u7684\u5f62\u72b6\u6765\u9a8c\u8bc1\u56fe\u7247\u662f\u5426\u6210\u529f\u52a0\u8f7d\u3002<\/p>\n<\/p>\n<p><h3>\u6279\u91cf\u8bfb\u53d6\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u7247\u8bfb\u53d6\u5e76\u8f6c\u6362\u4e3a\u6570\u636e\u96c6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def load_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((&#39;.png&#39;, &#39;.jpg&#39;, &#39;.jpeg&#39;)):<\/p>\n<p>            img = load_image(os.path.join(folder_path, filename))<\/p>\n<p>            if img is not None:<\/p>\n<p>                images.append(img)<\/p>\n<p>    return np.array(images)<\/p>\n<p>folder_path = &#39;path\/to\/your\/folder&#39;<\/p>\n<p>dataset = load_images_from_folder(folder_path)<\/p>\n<p>print(dataset.shape)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u7247\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u52a0\u8f7d\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u4e00\u4e2aNumPy\u6570\u7ec4\uff0c\u5f62\u6210\u6211\u4eec\u7684\u56fe\u7247\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001TensorFlow\u5e93<\/p>\n<\/p>\n<p><p>TensorFlow\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u5177\u6709\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528TensorFlow\u5e93\u5c06\u56fe\u7247\u53d8\u4e3a\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5TensorFlow\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5TensorFlow\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tensorflow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8bfb\u53d6\u548c\u5904\u7406\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528TensorFlow\u5e93\u8bfb\u53d6\u548c\u5904\u7406\u56fe\u7247\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>def load_image(image_path):<\/p>\n<p>    img = tf.io.read_file(image_path)<\/p>\n<p>    img = tf.image.decode_image(img)<\/p>\n<p>    return img<\/p>\n<p>image_path = &#39;path\/to\/your\/image.jpg&#39;<\/p>\n<p>image_data = load_image(image_path)<\/p>\n<p>print(image_data.shape)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528TensorFlow\u5e93\u8bfb\u53d6\u4e00\u5f20\u56fe\u7247\uff0c\u5e76\u5c06\u5176\u89e3\u7801\u4e3a\u5f20\u91cf\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u6253\u5370\u56fe\u7247\u6570\u636e\u7684\u5f62\u72b6\u6765\u9a8c\u8bc1\u56fe\u7247\u662f\u5426\u6210\u529f\u52a0\u8f7d\u3002<\/p>\n<\/p>\n<p><h3>\u6279\u91cf\u8bfb\u53d6\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u7247\u8bfb\u53d6\u5e76\u8f6c\u6362\u4e3a\u6570\u636e\u96c6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def load_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((&#39;.png&#39;, &#39;.jpg&#39;, &#39;.jpeg&#39;)):<\/p>\n<p>            img = load_image(os.path.join(folder_path, filename))<\/p>\n<p>            if img is not None:<\/p>\n<p>                images.append(img)<\/p>\n<p>    return tf.stack(images)<\/p>\n<p>folder_path = &#39;path\/to\/your\/folder&#39;<\/p>\n<p>dataset = load_images_from_folder(folder_path)<\/p>\n<p>print(dataset.shape)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u7247\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u52a0\u8f7d\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u4e00\u4e2aTensorFlow\u5f20\u91cf\uff0c\u5f62\u6210\u6211\u4eec\u7684\u56fe\u7247\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6570\u636e\u589e\u5f3a<\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u56fe\u7247\u6570\u636e\u96c6\u65f6\uff0c\u6570\u636e\u589e\u5f3a\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u6570\u636e\u589e\u5f3a\u53ef\u4ee5\u589e\u52a0\u6570\u636e\u96c6\u7684\u591a\u6837\u6027\uff0c\u4ece\u800c\u63d0\u9ad8\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528PIL\u3001OpenCV\u6216TensorFlow\u5e93\u8fdb\u884c\u6570\u636e\u589e\u5f3a\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528PIL\u8fdb\u884c\u6570\u636e\u589e\u5f3a<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528PIL\u5e93\u8fdb\u884c\u6570\u636e\u589e\u5f3a\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import ImageEnhance<\/p>\n<p>def augment_image(image):<\/p>\n<p>    enhancer = ImageEnhance.Contrast(image)<\/p>\n<p>    image = enhancer.enhance(2)  # \u589e\u5f3a\u5bf9\u6bd4\u5ea6<\/p>\n<p>    return image<\/p>\n<p>image_path = &#39;path\/to\/your\/image.jpg&#39;<\/p>\n<p>image = Image.open(image_path)<\/p>\n<p>augmented_image = augment_image(image)<\/p>\n<p>augmented_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528PIL\u5e93\u589e\u5f3a\u56fe\u7247\u7684\u5bf9\u6bd4\u5ea6\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u8fdb\u884c\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\u589e\u5f3a\uff0c\u5982\u65cb\u8f6c\u3001\u7f29\u653e\u3001\u7ffb\u8f6c\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528OpenCV\u8fdb\u884c\u6570\u636e\u589e\u5f3a<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528OpenCV\u5e93\u8fdb\u884c\u6570\u636e\u589e\u5f3a\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def augment_image(image):<\/p>\n<p>    image = cv2.flip(image, 1)  # \u6c34\u5e73\u7ffb\u8f6c<\/p>\n<p>    return image<\/p>\n<p>image_path = &#39;path\/to\/your\/image.jpg&#39;<\/p>\n<p>image = load_image(image_path)<\/p>\n<p>augmented_image = augment_image(image)<\/p>\n<p>cv2.imshow(&#39;Augmented Image&#39;, augmented_image)<\/p>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528OpenCV\u5e93\u6c34\u5e73\u7ffb\u8f6c\u56fe\u7247\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u8fdb\u884c\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\u589e\u5f3a\uff0c\u5982\u65cb\u8f6c\u3001\u7f29\u653e\u3001\u5bf9\u6bd4\u5ea6\u8c03\u6574\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528TensorFlow\u8fdb\u884c\u6570\u636e\u589e\u5f3a<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528TensorFlow\u5e93\u8fdb\u884c\u6570\u636e\u589e\u5f3a\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def augment_image(image):<\/p>\n<p>    image = tf.image.random_flip_left_right(image)  # \u968f\u673a\u6c34\u5e73\u7ffb\u8f6c<\/p>\n<p>    return image<\/p>\n<p>image_path = &#39;path\/to\/your\/image.jpg&#39;<\/p>\n<p>image_data = load_image(image_path)<\/p>\n<p>augmented_image = augment_image(image_data)<\/p>\n<p>print(augmented_image.shape)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528TensorFlow\u5e93\u968f\u673a\u6c34\u5e73\u7ffb\u8f6c\u56fe\u7247\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u8fdb\u884c\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\u589e\u5f3a\uff0c\u5982\u65cb\u8f6c\u3001\u7f29\u653e\u3001\u4eae\u5ea6\u8c03\u6574\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4fdd\u5b58\u6570\u636e\u96c6<\/p>\n<\/p>\n<p><p>\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u6570\u636e\u96c6\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u636e\u96c6\u4fdd\u5b58\u5230\u78c1\u76d8\u4e0a\uff0c\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u3001HDF5\u6216\u5176\u4ed6\u683c\u5f0f\u6765\u4fdd\u5b58\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528NumPy\u4fdd\u5b58\u6570\u636e\u96c6<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528NumPy\u4fdd\u5b58\u6570\u636e\u96c6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>np.save(&#39;dataset.npy&#39;, dataset)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u6570\u636e\u96c6\u4fdd\u5b58\u4e3aNumPy\u6570\u7ec4\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>np.load<\/code>\u51fd\u6570\u52a0\u8f7d\u4fdd\u5b58\u7684\u6570\u636e\u96c6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">loaded_dataset = np.load(&#39;dataset.npy&#39;)<\/p>\n<p>print(loaded_dataset.shape)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528HDF5\u4fdd\u5b58\u6570\u636e\u96c6<\/h3>\n<\/p>\n<p><p>HDF5\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5b58\u50a8\u683c\u5f0f\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528h5py\u5e93\u5c06\u6570\u636e\u96c6\u4fdd\u5b58\u4e3aHDF5\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5h5py\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install h5py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528h5py\u5e93\u4fdd\u5b58\u6570\u636e\u96c6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import h5py<\/p>\n<p>with h5py.File(&#39;dataset.h5&#39;, &#39;w&#39;) as hf:<\/p>\n<p>    hf.create_dataset(&#39;images&#39;, data=dataset)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u6570\u636e\u96c6\u4fdd\u5b58\u4e3aHDF5\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528h5py\u5e93\u52a0\u8f7d\u4fdd\u5b58\u7684\u6570\u636e\u96c6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with h5py.File(&#39;dataset.h5&#39;, &#39;r&#39;) as hf:<\/p>\n<p>    loaded_dataset = hf[&#39;images&#39;][:]<\/p>\n<p>    print(loaded_dataset.shape)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528PIL\u3001OpenCV\u3001TensorFlow\u7b49\u5e93\u5c06\u56fe\u7247\u53d8\u4e3a\u6570\u636e\u96c6\uff0c\u5e76\u8fdb\u884c\u4e86\u6570\u636e\u589e\u5f3a\u548c\u6570\u636e\u4fdd\u5b58\u7684\u64cd\u4f5c\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u60a8\u7684\u5177\u4f53\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u5e76\u80fd\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u6570\u636e\u96c6\uff1f<\/strong><br \/>\u8981\u5c06\u56fe\u7247\u8f6c\u6362\u4e3a\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u51e0\u4e2a\u6d41\u884c\u5e93\uff0c\u5982PIL\uff08Pillow\uff09\u3001OpenCV\u548cNumPy\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u52a0\u8f7d\u56fe\u50cf\u6587\u4ef6\uff0c\u7136\u540e\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u683c\u5f0f\uff0c\u65b9\u4fbf\u540e\u7eed\u5904\u7406\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image\nimport numpy as np\n\n# \u52a0\u8f7d\u56fe\u7247\nimage = Image.open(&#39;your_image.jpg&#39;)\n# \u8f6c\u6362\u4e3a\u6570\u7ec4\nimage_array = np.array(image)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5c06\u56fe\u50cf\u6570\u636e\u7528\u4e8e<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6216\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u3002<\/p>\n<p><strong>\u5728\u521b\u5efa\u6570\u636e\u96c6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u4e0d\u540c\u5c3a\u5bf8\u7684\u56fe\u7247\uff1f<\/strong><br \/>\u5904\u7406\u4e0d\u540c\u5c3a\u5bf8\u7684\u56fe\u7247\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u56fe\u50cf\u7f29\u653e\u6280\u672f\uff0c\u4ee5\u786e\u4fdd\u6240\u6709\u56fe\u50cf\u5177\u6709\u76f8\u540c\u7684\u7ef4\u5ea6\u3002\u8fd9\u5bf9\u4e8e\u6279\u91cf\u5904\u7406\u548c\u6a21\u578b\u8bad\u7ec3\u975e\u5e38\u91cd\u8981\u3002\u4f7f\u7528PIL\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\uff1a  <\/p>\n<pre><code class=\"language-python\">resized_image = image.resize((width, height))\n<\/code><\/pre>\n<p>\u9009\u62e9\u5408\u9002\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u4ee5\u4fdd\u8bc1\u56fe\u50cf\u7684\u8d28\u91cf\u548c\u4fe1\u606f\u5b8c\u6574\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u591a\u4e2a\u56fe\u7247\u6279\u91cf\u8f6c\u6362\u4e3a\u6570\u636e\u96c6\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684os\u5e93\u6765\u904d\u5386\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u6587\u4ef6\uff0c\u5e76\u5c06\u5b83\u4eec\u6279\u91cf\u52a0\u8f7d\u5230\u6570\u636e\u96c6\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\ndataset = []\nfor filename in os.listdir(&#39;your_directory&#39;):\n    if filename.endswith(&#39;.jpg&#39;):  # \u6839\u636e\u9700\u8981\u8c03\u6574\u6587\u4ef6\u7c7b\u578b\n        img_path = os.path.join(&#39;your_directory&#39;, filename)\n        image = Image.open(img_path)\n        image_array = np.array(image)\n        dataset.append(image_array)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u5c06\u6307\u5b9a\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u56fe\u50cf\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6570\u636e\u96c6\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u56fe\u7247\u53d8\u4e3a\u6570\u636e\u96c6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PIL\u5e93\u3001OpenCV\u5e93\u3001TensorFlow\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1047015,"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\/1047007"}],"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=1047007"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1047007\/revisions"}],"predecessor-version":[{"id":1047019,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1047007\/revisions\/1047019"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1047015"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1047007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1047007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1047007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}