{"id":964181,"date":"2024-12-27T04:25:52","date_gmt":"2024-12-26T20:25:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/964181.html"},"modified":"2024-12-27T04:25:54","modified_gmt":"2024-12-26T20:25:54","slug":"python%e5%a6%82%e4%bd%95%e8%af%86%e5%88%ab%e6%89%80%e6%9c%89%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/964181.html","title":{"rendered":"python\u5982\u4f55\u8bc6\u522b\u6240\u6709\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181115\/b6adf5c1-b3bf-4174-8c06-347b6ce2175b.webp\" alt=\"python\u5982\u4f55\u8bc6\u522b\u6240\u6709\u56fe\u7247\" \/><\/p>\n<p><p> <strong>Python\u8bc6\u522b\u6240\u6709\u56fe\u7247\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528PIL\u5e93\u52a0\u8f7d\u56fe\u50cf\u3001\u5229\u7528OpenCV\u5904\u7406\u56fe\u50cf\u3001\u501f\u52a9TensorFlow\u6216PyTorch\u8fdb\u884c\u6df1\u5ea6\u5b66\u4e60\u56fe\u50cf\u8bc6\u522b\u3001\u901a\u8fc7scikit-image\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u3002\u53ef\u4ee5\u901a\u8fc7PIL\u5e93\u6765\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u52a0\u8f7d\u548c\u8bc6\u522b\u56fe\u50cf\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5177\u6709\u4e30\u5bcc\u7684\u5e93\u548c\u5de5\u5177\u96c6\u6765\u5904\u7406\u56fe\u50cf\u8bc6\u522b\u4efb\u52a1\u3002\u5176\u4e2d\uff0cPIL\uff08Python Imaging Library\uff09\u6216\u5176\u884d\u751f\u7684Pillow\u5e93\u662f\u5904\u7406\u56fe\u50cf\u7684\u57fa\u672c\u5de5\u5177\u3002Pillow\u5141\u8bb8\u7528\u6237\u8f7b\u677e\u52a0\u8f7d\u3001\u64cd\u4f5c\u548c\u4fdd\u5b58\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u50cf\u3002\u4f8b\u5982\uff0c\u8981\u8bc6\u522b\u4e00\u7ec4\u56fe\u50cf\uff0c\u9996\u5148\u8981\u786e\u4fdd\u6240\u6709\u56fe\u50cf\u90fd\u53ef\u4ee5\u88ab\u7a0b\u5e8f\u52a0\u8f7d\u548c\u8bfb\u53d6\u3002\u901a\u8fc7\u4f7f\u7528Pillow\u5e93\uff0c\u4f60\u53ef\u4ee5\u7b80\u5355\u5730\u7528Image.open()\u51fd\u6570\u6253\u5f00\u56fe\u50cf\u6587\u4ef6\uff0c\u800c\u65e0\u9700\u5173\u6ce8\u5e95\u5c42\u7ec6\u8282\u3002\u7136\u540e\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6Python\u5e93\uff0c\u5982NumPy\uff0c\u5c06\u56fe\u50cf\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u7ec4\u683c\u5f0f\uff0c\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5206\u6790\u6216\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8Python\u8bc6\u522b\u56fe\u7247\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u6db5\u76d6\u4ece\u57fa\u7840\u7684PIL\u5e93\u5230\u9ad8\u7ea7\u7684\u6df1\u5ea6\u5b66\u4e60\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PIL\uff08Pillow\uff09\u5e93<\/h3>\n<\/p>\n<p><p>Pillow\u662fPython Imaging Library\uff08PIL\uff09\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u5e76\u5728PIL\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u4e86\u8bb8\u591a\u6539\u8fdb\u548c\u6269\u5c55\u3002\u5b83\u4e3a\u56fe\u50cf\u5904\u7406\u63d0\u4f9b\u4e86\u7b80\u5355\u800c\u5f3a\u5927\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Pillow\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\u3002\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u6765\u52a0\u8f7d\u3001\u663e\u793a\u548c\u4fdd\u5b58\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>image.show()<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u50cf\u7684\u5c3a\u5bf8<\/strong><\/h2>\n<p>width, height = image.size<\/p>\n<p>print(f&quot;Image size: {width}x{height}&quot;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&#39;output.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u56fe\u50cf\u5904\u7406\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>Pillow\u63d0\u4f9b\u4e86\u591a\u79cd\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u5305\u62ec\u56fe\u50cf\u88c1\u526a\u3001\u65cb\u8f6c\u3001\u6ee4\u955c\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u56fe\u50cf\u88c1\u526a<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>crop()<\/code>\u65b9\u6cd5\u88c1\u526a\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cropped_image = image.crop((100, 100, 400, 400))<\/p>\n<p>cropped_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56fe\u50cf\u65cb\u8f6c<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>rotate()<\/code>\u65b9\u6cd5\u65cb\u8f6c\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rotated_image = image.rotate(45)<\/p>\n<p>rotated_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e94\u7528\u6ee4\u955c<\/strong><\/p>\n<\/p>\n<p><p>Pillow\u652f\u6301\u591a\u79cd\u6ee4\u955c\u6548\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import ImageFilter<\/p>\n<p>blurred_image = image.filter(ImageFilter.BLUR)<\/p>\n<p>blurred_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u5229\u7528OpenCV\u8fdb\u884c\u56fe\u50cf\u5904\u7406<\/h3>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5OpenCV\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u53ef\u4ee5\u8f7b\u677e\u5730\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Image&#39;, 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><h4>2. \u56fe\u50cf\u5904\u7406\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u5982\u56fe\u50cf\u8f6c\u6362\u3001\u8fb9\u7f18\u68c0\u6d4b\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u56fe\u50cf\u8f6c\u6362<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<p>cv2.imshow(&#39;Gray Image&#39;, gray_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Canny\u8fb9\u7f18\u68c0\u6d4b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">edges = cv2.Canny(image, 100, 200)<\/p>\n<p>cv2.imshow(&#39;Edges&#39;, edges)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528TensorFlow\u6216PyTorch\u8fdb\u884c\u6df1\u5ea6\u5b66\u4e60\u56fe\u50cf\u8bc6\u522b<\/h3>\n<\/p>\n<p><p>\u6df1\u5ea6\u5b66\u4e60\u662f\u56fe\u50cf\u8bc6\u522b\u7684\u524d\u6cbf\u6280\u672f\uff0cTensorFlow\u548cPyTorch\u662f\u4e24\u4e2a\u6700\u6d41\u884c\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u3002<\/p>\n<\/p>\n<p><h4>1. TensorFlow\u56fe\u50cf\u8bc6\u522b<\/h4>\n<\/p>\n<p><p>TensorFlow\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u9884\u8bad\u7ec3\u6a21\u578b\u6765\u5904\u7406\u56fe\u50cf\u8bc6\u522b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5TensorFlow<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tensorflow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u52a0\u8f7d\u548c\u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<h2><strong>\u52a0\u8f7d\u9884\u8bad\u7ec3\u7684\u6a21\u578b\uff08\u4f8b\u5982\uff0cMobileNetV2\uff09<\/strong><\/h2>\n<p>model = tf.keras.applications.MobileNetV2(weights=&#39;imagenet&#39;)<\/p>\n<h2><strong>\u9884\u5904\u7406\u8f93\u5165\u56fe\u50cf<\/strong><\/h2>\n<p>from tensorflow.keras.preprocessing import image<\/p>\n<p>img = image.load_img(&#39;example.jpg&#39;, target_size=(224, 224))<\/p>\n<p>img_array = image.img_to_array(img)<\/p>\n<p>img_array = tf.keras.applications.mobilenet_v2.preprocess_input(img_array)<\/p>\n<p>img_array = tf.expand_dims(img_array, axis=0)<\/p>\n<h2><strong>\u9884\u6d4b\u56fe\u50cf\u7c7b\u522b<\/strong><\/h2>\n<p>predictions = model.predict(img_array)<\/p>\n<p>decoded_predictions = tf.keras.applications.mobilenet_v2.decode_predictions(predictions, top=3)<\/p>\n<p>print(decoded_predictions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>2. PyTorch\u56fe\u50cf\u8bc6\u522b<\/h4>\n<\/p>\n<p><p>PyTorch\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u5177\u6709\u7075\u6d3b\u7684\u6a21\u578b\u5b9a\u4e49\u548c\u8bad\u7ec3\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5PyTorch<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u7cfb\u7edf\u548cCUDA\u7248\u672c\u4ecePyTorch\u5b98\u7f51\u83b7\u53d6\u5b89\u88c5\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install torch torchvision<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u9884\u8bad\u7ec3\u7684ResNet\u6a21\u578b<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch<\/p>\n<p>from torchvision import models, transforms<\/p>\n<p>from PIL import Image<\/p>\n<h2><strong>\u52a0\u8f7d\u9884\u8bad\u7ec3\u7684ResNet\u6a21\u578b<\/strong><\/h2>\n<p>model = models.resnet50(pretrained=True)<\/p>\n<p>model.eval()<\/p>\n<h2><strong>\u56fe\u50cf\u9884\u5904\u7406<\/strong><\/h2>\n<p>preprocess = transforms.Compose([<\/p>\n<p>    transforms.Resize(256),<\/p>\n<p>    transforms.CenterCrop(224),<\/p>\n<p>    transforms.ToTensor(),<\/p>\n<p>    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),<\/p>\n<p>])<\/p>\n<p>img = Image.open(&#39;example.jpg&#39;)<\/p>\n<p>img_tensor = preprocess(img)<\/p>\n<p>img_tensor = img_tensor.unsqueeze(0)  # \u589e\u52a0\u6279\u6b21\u7ef4\u5ea6<\/p>\n<h2><strong>\u9884\u6d4b<\/strong><\/h2>\n<p>with torch.no_grad():<\/p>\n<p>    output = model(img_tensor)<\/p>\n<p>_, predicted = torch.max(output, 1)<\/p>\n<p>print(&#39;Predicted:&#39;, predicted.item())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u901a\u8fc7scikit-image\u8fdb\u884c\u56fe\u50cf\u5904\u7406<\/h3>\n<\/p>\n<p><p>scikit-image\u662f\u4e00\u4e2a\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u7684Python\u5e93\uff0c\u5efa\u7acb\u5728NumPy\u548cSciPy\u4e4b\u4e0a\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5scikit-image\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scikit-image<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage import io<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = io.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>io.imshow(image)<\/p>\n<p>io.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u56fe\u50cf\u5904\u7406\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>scikit-image\u63d0\u4f9b\u4e86\u591a\u79cd\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u5982\u989c\u8272\u7a7a\u95f4\u8f6c\u6362\u3001\u8fb9\u7f18\u68c0\u6d4b\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u989c\u8272\u7a7a\u95f4\u8f6c\u6362<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06RGB\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage.color import rgb2gray<\/p>\n<p>gray_image = rgb2gray(image)<\/p>\n<p>io.imshow(gray_image)<\/p>\n<p>io.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Sobel\u8fb9\u7f18\u68c0\u6d4b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from skimage.filters import sobel<\/p>\n<p>edges = sobel(gray_image)<\/p>\n<p>io.imshow(edges)<\/p>\n<p>io.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u5e94\u7528<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u6765\u8bc6\u522b\u548c\u5904\u7406\u56fe\u50cf\uff0c\u4ece\u57fa\u7840\u7684\u56fe\u50cf\u52a0\u8f7d\u548c\u5904\u7406\u5230\u9ad8\u7ea7\u7684\u6df1\u5ea6\u5b66\u4e60\u56fe\u50cf\u8bc6\u522b\u3002\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u4efb\u52a1\u9700\u6c42\u548c\u590d\u6742\u6027\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>Pillow<\/strong>\u9002\u5408\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0c\u5982\u52a0\u8f7d\u3001\u4fdd\u5b58\u548c\u7b80\u5355\u7684\u56fe\u50cf\u64cd\u4f5c\u3002<\/li>\n<li><strong>OpenCV<\/strong>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u9700\u8981\u590d\u6742\u56fe\u50cf\u5904\u7406\u7684\u573a\u666f\u3002<\/li>\n<li><strong>TensorFlow\u548cPyTorch<\/strong>\u662f\u8fdb\u884c\u6df1\u5ea6\u5b66\u4e60\u56fe\u50cf\u8bc6\u522b\u7684\u7406\u60f3\u9009\u62e9\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u9ad8\u7cbe\u5ea6\u548c\u590d\u6742\u6a21\u578b\u7684\u60c5\u51b5\u4e0b\u3002<\/li>\n<li><strong>scikit-image<\/strong>\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u9002\u5408\u4e0e\u5176\u4ed6\u79d1\u5b66\u8ba1\u7b97\u5e93\u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e9b\u5de5\u5177\uff0cPython\u80fd\u591f\u5e94\u5bf9\u4ece\u7b80\u5355\u5230\u590d\u6742\u7684\u5404\u79cd\u56fe\u50cf\u8bc6\u522b\u4efb\u52a1\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bc6\u522b\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u7247\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u56fe\u7247\u683c\u5f0f\u7684\u8bc6\u522b\uff0c\u5305\u62ecJPEG\u3001PNG\u3001GIF\u7b49\u3002\u4f7f\u7528PIL\uff08Pillow\uff09\u5e93\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u6253\u5f00\u548c\u5904\u7406\u8fd9\u4e9b\u683c\u5f0f\u7684\u56fe\u7247\u3002\u9996\u5148\uff0c\u5b89\u88c5Pillow\u5e93\uff0c\u901a\u8fc7\u547d\u4ee4<code>pip install Pillow<\/code>\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>Image.open()<\/code>\u65b9\u6cd5\u6765\u52a0\u8f7d\u56fe\u7247\uff0c\u5e76\u901a\u8fc7<code>format<\/code>\u5c5e\u6027\u6765\u8bc6\u522b\u683c\u5f0f\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\nimg = Image.open(&#39;example.jpg&#39;)\nprint(img.format)  # \u8f93\u51fa\uff1aJPEG\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u63d0\u53d6\u56fe\u7247\u7684\u5143\u6570\u636e\uff1f<\/strong><br \/>\u63d0\u53d6\u56fe\u7247\u7684\u5143\u6570\u636e\u53ef\u4ee5\u5e2e\u52a9\u60a8\u83b7\u53d6\u66f4\u591a\u5173\u4e8e\u56fe\u7247\u7684\u4fe1\u606f\uff0c\u4f8b\u5982\u62cd\u6444\u65f6\u95f4\u3001\u76f8\u673a\u578b\u53f7\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u914d\u5408<code>ExifTags<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image\nfrom PIL.ExifTags import TAGS\n\nimg = Image.open(&#39;example.jpg&#39;)\nexif_data = img._getexif()\n\nfor tag_id, value in exif_data.items():\n    tag = TAGS.get(tag_id, tag_id)\n    print(f&quot;{tag:25}: {value}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u67e5\u770b\u5230\u56fe\u7247\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p><strong>Python\u5982\u4f55\u5904\u7406\u6279\u91cf\u56fe\u7247\u8bc6\u522b\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u9700\u8981\u5904\u7406\u5927\u91cf\u56fe\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528os\u5e93\u7ed3\u5408Pillow\u6765\u6279\u91cf\u8bc6\u522b\u3002\u901a\u8fc7\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u56fe\u7247\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u8fdb\u884c\u7edf\u4e00\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\nfrom PIL import Image\n\ndirectory = &#39;path\/to\/images&#39;\nfor filename in os.listdir(directory):\n    if filename.endswith(&#39;.jpg&#39;) or filename.endswith(&#39;.png&#39;):\n        img_path = os.path.join(directory, filename)\n        img = Image.open(img_path)\n        print(f&quot;{filename}: {img.format}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u6587\u4ef6\u5939\u4e2d\u6240\u6709\u6307\u5b9a\u683c\u5f0f\u7684\u56fe\u7247\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bc6\u522b\u6240\u6709\u56fe\u7247\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528PIL\u5e93\u52a0\u8f7d\u56fe\u50cf\u3001\u5229\u7528OpenCV\u5904\u7406\u56fe\u50cf\u3001\u501f\u52a9TensorFlow\u6216 [&hellip;]","protected":false},"author":3,"featured_media":964186,"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\/964181"}],"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=964181"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/964181\/revisions"}],"predecessor-version":[{"id":964187,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/964181\/revisions\/964187"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/964186"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=964181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=964181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=964181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}