{"id":1161635,"date":"2025-01-13T19:18:43","date_gmt":"2025-01-13T11:18:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1161635.html"},"modified":"2025-01-13T19:18:45","modified_gmt":"2025-01-13T11:18:45","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%af%bc%e5%87%bajpg%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1161635.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5bfc\u51fajpg\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202837\/0630a42d-3c11-4642-ac13-6edab1f6f07a.webp\" alt=\"python\u4e2d\u5982\u4f55\u5bfc\u51fajpg\u56fe\u7247\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5bfc\u51faJPG\u56fe\u7247\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4f7f\u7528PIL\u5e93\u3001OpenCV\u5e93\u3001Matplotlib\u5e93\u7b49\u65b9\u5f0f<\/strong>\u3002\u8fd9\u4e9b\u5e93\u5404\u6709\u5176\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u6bd4\u5982<strong>PIL\u5e93\uff08Pillow\uff09\u64cd\u4f5c\u7b80\u5355\u9002\u5408\u5904\u7406\u57fa\u672c\u56fe\u7247\u64cd\u4f5c\u3001OpenCV\u529f\u80fd\u5f3a\u5927\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3001Matplotlib\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u4e14\u652f\u6301\u591a\u79cd\u56fe\u7247\u683c\u5f0f<\/strong>\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u5bfc\u51faJPG\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001PIL\u5e93\uff08Pillow\uff09<\/h3>\n<\/p>\n<p><p>PIL\uff08Python Imaging Library\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0cPillow\u662fPIL\u7684\u4e00\u4e2a\u6d3e\u751f\u5206\u652f\uff0c\u517c\u5bb9\u5e76\u589e\u5f3a\u4e86PIL\u7684\u529f\u80fd\u3002\u4f7f\u7528Pillow\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u6253\u5f00\u3001\u64cd\u4f5c\u548c\u4fdd\u5b58\u56fe\u7247\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Pillow<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Pillow\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\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><h4>\u4f7f\u7528Pillow\u5bfc\u51faJPG\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Pillow\u5e93\u5bfc\u51faJPG\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.new(&#39;RGB&#39;, (100, 100), color = (73, 109, 137))<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u4e3aJPG\u683c\u5f0f<\/strong><\/h2>\n<p>image.save(&#39;example.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf\u5bf9\u8c61\uff0c\u5e76\u8bbe\u7f6e\u4e86\u56fe\u50cf\u7684\u5927\u5c0f\u548c\u989c\u8272\u3002\u7136\u540e\u4f7f\u7528<code>save<\/code>\u65b9\u6cd5\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3aJPG\u683c\u5f0f\u3002Pillow\u5e93\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u56fe\u50cf\u6587\u4ef6\uff0c\u5305\u62ecPNG\u3001JPEG\u3001GIF\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001OpenCV\u5e93<\/h3>\n<\/p>\n<p><p>OpenCV\uff08Open Source Computer Vision Library\uff09\u662f\u4e00\u4e2a\u5f00\u6e90\u8ba1\u7b97\u673a\u89c6\u89c9\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u8f6f\u4ef6\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002OpenCV\u5e93\u5728\u5904\u7406\u56fe\u50cf\u65f6\u7684\u6027\u80fd\u975e\u5e38\u9ad8\uff0c\u9002\u5408\u4e8e\u5904\u7406\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5OpenCV<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5OpenCV\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528OpenCV\u5bfc\u51faJPG\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528OpenCV\u5e93\u5bfc\u51faJPG\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.zeros((100, 100, 3), dtype=np.uint8)<\/p>\n<p>image[:] = (73, 109, 137)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u4e3aJPG\u683c\u5f0f<\/strong><\/h2>\n<p>cv2.imwrite(&#39;example.jpg&#39;, image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528NumPy\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf\u5bf9\u8c61\uff0c\u5e76\u8bbe\u7f6e\u4e86\u56fe\u50cf\u7684\u5927\u5c0f\u548c\u989c\u8272\u3002\u7136\u540e\u4f7f\u7528OpenCV\u5e93\u7684<code>imwrite<\/code>\u65b9\u6cd5\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3aJPG\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001Matplotlib\u5e93<\/h3>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u7528\u4e8e\u7ed8\u5236\u6570\u636e\u53ef\u89c6\u5316\u56fe\u8868\u7684Python\u5e93\uff0c\u652f\u6301\u591a\u79cd\u56fe\u7247\u683c\u5f0f\u3002\u867d\u7136Matplotlib\u7684\u4e3b\u8981\u529f\u80fd\u662f\u6570\u636e\u53ef\u89c6\u5316\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Matplotlib<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Matplotlib\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Matplotlib\u5bfc\u51faJPG\u56fe\u7247<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Matplotlib\u5e93\u5bfc\u51faJPG\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.random.rand(100, 100, 3)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(image)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u5173\u95ed\u5750\u6807\u8f74<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u4e3aJPG\u683c\u5f0f<\/strong><\/h2>\n<p>plt.savefig(&#39;example.jpg&#39;, format=&#39;jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528NumPy\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u968f\u673a\u56fe\u50cf\u5bf9\u8c61\u3002\u7136\u540e\u4f7f\u7528Matplotlib\u5e93\u663e\u793a\u56fe\u50cf\uff0c\u5e76\u4f7f\u7528<code>savefig<\/code>\u65b9\u6cd5\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3aJPG\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4fdd\u5b58\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u9664\u4e86JPG\u683c\u5f0f\u5916\uff0c\u8fd9\u4e9b\u5e93\u8fd8\u652f\u6301\u4fdd\u5b58\u5176\u4ed6\u683c\u5f0f\u7684\u56fe\u50cf\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Pillow\u3001OpenCV\u548cMatplotlib\u5e93\u4fdd\u5b58\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528Pillow\u4fdd\u5b58\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u50cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.new(&#39;RGB&#39;, (100, 100), color = (73, 109, 137))<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u4e3aPNG\u683c\u5f0f<\/strong><\/h2>\n<p>image.save(&#39;example.png&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u4e3aBMP\u683c\u5f0f<\/strong><\/h2>\n<p>image.save(&#39;example.bmp&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528OpenCV\u4fdd\u5b58\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u50cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.zeros((100, 100, 3), dtype=np.uint8)<\/p>\n<p>image[:] = (73, 109, 137)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u4e3aPNG\u683c\u5f0f<\/strong><\/h2>\n<p>cv2.imwrite(&#39;example.png&#39;, image)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u4e3aBMP\u683c\u5f0f<\/strong><\/h2>\n<p>cv2.imwrite(&#39;example.bmp&#39;, image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Matplotlib\u4fdd\u5b58\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u50cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.random.rand(100, 100, 3)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(image)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u5173\u95ed\u5750\u6807\u8f74<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u4e3aPNG\u683c\u5f0f<\/strong><\/h2>\n<p>plt.savefig(&#39;example.png&#39;, format=&#39;png&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u4e3aBMP\u683c\u5f0f<\/strong><\/h2>\n<p>plt.savefig(&#39;example.bmp&#39;, format=&#39;bmp&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u56fe\u50cf\u5904\u7406\u4e0e\u8c03\u6574<\/h3>\n<\/p>\n<p><p>\u5728\u4fdd\u5b58\u56fe\u50cf\u4e4b\u524d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u56fe\u50cf\u8fdb\u884c\u5904\u7406\u548c\u8c03\u6574\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Pillow\u3001OpenCV\u548cMatplotlib\u5e93\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u548c\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528Pillow\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u548c\u8c03\u6574<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageFilter<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u73b0\u6709\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/strong><\/h2>\n<p>resized_image = image.resize((200, 200))<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/h2>\n<p>rotated_image = image.rotate(45)<\/p>\n<h2><strong>\u5e94\u7528\u6a21\u7cca\u6ee4\u955c<\/strong><\/h2>\n<p>blurred_image = image.filter(ImageFilter.BLUR)<\/p>\n<h2><strong>\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>resized_image.save(&#39;resized_example.jpg&#39;)<\/p>\n<p>rotated_image.save(&#39;rotated_example.jpg&#39;)<\/p>\n<p>blurred_image.save(&#39;blurred_example.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528OpenCV\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u548c\u8c03\u6574<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u73b0\u6709\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/strong><\/h2>\n<p>resized_image = cv2.resize(image, (200, 200))<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/h2>\n<p>(h, w) = image.shape[:2]<\/p>\n<p>center = (w \/\/ 2, h \/\/ 2)<\/p>\n<p>M = cv2.getRotationMatrix2D(center, 45, 1.0)<\/p>\n<p>rotated_image = cv2.warpAffine(image, M, (w, h))<\/p>\n<h2><strong>\u5e94\u7528\u6a21\u7cca\u6ee4\u955c<\/strong><\/h2>\n<p>blurred_image = cv2.GaussianBlur(image, (15, 15), 0)<\/p>\n<h2><strong>\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imwrite(&#39;resized_example.jpg&#39;, resized_image)<\/p>\n<p>cv2.imwrite(&#39;rotated_example.jpg&#39;, rotated_image)<\/p>\n<p>cv2.imwrite(&#39;blurred_example.jpg&#39;, blurred_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Matplotlib\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u548c\u8c03\u6574<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<p>from skimage import transform, filters<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2a\u73b0\u6709\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>image = plt.imread(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u8c03\u6574\u56fe\u50cf\u5927\u5c0f<\/strong><\/h2>\n<p>resized_image = transform.resize(image, (200, 200))<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/h2>\n<p>rotated_image = transform.rotate(image, 45)<\/p>\n<h2><strong>\u5e94\u7528\u6a21\u7cca\u6ee4\u955c<\/strong><\/h2>\n<p>blurred_image = filters.gaussian(image, sigma=1, multichannel=True)<\/p>\n<h2><strong>\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imsave(&#39;resized_example.jpg&#39;, resized_image)<\/p>\n<p>plt.imsave(&#39;rotated_example.jpg&#39;, rotated_image)<\/p>\n<p>plt.imsave(&#39;blurred_example.jpg&#39;, blurred_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5bfc\u51faJPG\u56fe\u7247\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002<strong>Pillow\u5e93\u9002\u5408\u5904\u7406\u57fa\u672c\u7684\u56fe\u50cf\u64cd\u4f5c\u3001OpenCV\u5e93\u529f\u80fd\u5f3a\u5927\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3001Matplotlib\u5e93\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u4e14\u652f\u6301\u591a\u79cd\u56fe\u7247\u683c\u5f0f<\/strong>\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u4f7f\u7528\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u56fe\u50cf\u5bfc\u51fa\u548c\u5904\u7406\u4efb\u52a1\u3002\u5e0c\u671b\u4ee5\u4e0a\u5185\u5bb9\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u4e86\u89e3Python\u4e2d\u5bfc\u51faJPG\u56fe\u7247\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u6765\u5bfc\u51faJPG\u56fe\u7247\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u51e0\u4e2a\u5e38\u7528\u7684\u5e93\u53ef\u4ee5\u7528\u6765\u5bfc\u51faJPG\u56fe\u7247\u3002\u5176\u4e2d\uff0cPillow\uff08PIL\u7684\u4e00\u4e2a\u5206\u652f\uff09\u662f\u6700\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u52a0\u8f7d\u3001\u4fee\u6539\u548c\u4fdd\u5b58\u56fe\u7247\u3002\u5176\u4ed6\u5e93\u5982OpenCV\u548cMatplotlib\u4e5f\u652f\u6301\u5bfc\u51faJPG\u683c\u5f0f\u7684\u56fe\u7247\uff0c\u9002\u5408\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Pillow\u5e93\u5bfc\u51faJPG\u56fe\u7247\uff1f<\/strong><br \/>\u4f7f\u7528Pillow\u5e93\u5bfc\u51faJPG\u56fe\u7247\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u901a\u8fc7<code>Image.open()<\/code>\u51fd\u6570\u52a0\u8f7d\u56fe\u7247\uff0c\u7136\u540e\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u6216\u76f4\u63a5\u4f7f\u7528<code>save()<\/code>\u65b9\u6cd5\u4fdd\u5b58\u4e3aJPG\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u52a0\u8f7d\u56fe\u7247\nimage = Image.open(&#39;input_image.png&#39;)\n# \u4fdd\u5b58\u4e3aJPG\u683c\u5f0f\nimage.save(&#39;output_image.jpg&#39;, &#39;JPEG&#39;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u8fd0\u884c\u4ee3\u7801\u4e4b\u524d\u5b89\u88c5\u4e86Pillow\u5e93\uff0c\u4f7f\u7528\u547d\u4ee4<code>pip install Pillow<\/code>\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5bfc\u51faJPG\u56fe\u7247\u65f6\u5982\u4f55\u8c03\u6574\u56fe\u7247\u8d28\u91cf\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Pillow\u5e93\u5bfc\u51faJPG\u56fe\u7247\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>quality<\/code>\u53c2\u6570\u6765\u8c03\u6574\u56fe\u7247\u7684\u8d28\u91cf\u3002\u8be5\u53c2\u6570\u7684\u503c\u8303\u56f4\u4ece1\u5230100\uff0c\u6570\u503c\u8d8a\u9ad8\uff0c\u56fe\u7247\u8d28\u91cf\u8d8a\u597d\uff0c\u4f46\u6587\u4ef6\u5927\u5c0f\u4e5f\u4f1a\u589e\u52a0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">image.save(&#39;output_image.jpg&#39;, &#39;JPEG&#39;, quality=85)\n<\/code><\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u8bbe\u7f6e\u4e8685\u7684\u8d28\u91cf\u503c\uff0c\u53ef\u4ee5\u5728\u8d28\u91cf\u548c\u6587\u4ef6\u5927\u5c0f\u4e4b\u95f4\u627e\u5230\u4e00\u4e2a\u5408\u9002\u7684\u5e73\u8861\u3002<\/p>\n<p><strong>\u5bfc\u51faJPG\u56fe\u7247\u540e\u5982\u4f55\u67e5\u770b\u56fe\u7247\u7684\u5143\u6570\u636e\uff1f<\/strong><br \/>\u5bfc\u51faJPG\u56fe\u7247\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u67e5\u770b\u56fe\u7247\u7684\u5143\u6570\u636e\u3002\u901a\u8fc7<code>_getexif()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u83b7\u5f97EXIF\u4fe1\u606f\uff0c\u5176\u4e2d\u5305\u542b\u62cd\u6444\u8bbe\u5907\u3001\u65f6\u95f4\u7b49\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">exif_data = image._getexif()\nfor tag, value in exif_data.items():\n    print(f&quot;{tag}: {value}&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u83b7\u53d6\u5230\u6709\u5173\u56fe\u7247\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u4fbf\u4e8e\u8fdb\u4e00\u6b65\u5904\u7406\u6216\u5f52\u6863\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5bfc\u51faJPG\u56fe\u7247\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4f7f\u7528PIL\u5e93\u3001OpenCV\u5e93\u3001Matplotlib\u5e93\u7b49\u65b9\u5f0f\u3002\u8fd9\u4e9b\u5e93\u5404\u6709 [&hellip;]","protected":false},"author":3,"featured_media":1161640,"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\/1161635"}],"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=1161635"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161635\/revisions"}],"predecessor-version":[{"id":1161644,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161635\/revisions\/1161644"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1161640"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1161635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1161635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1161635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}