{"id":1174923,"date":"2025-01-15T17:24:48","date_gmt":"2025-01-15T09:24:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174923.html"},"modified":"2025-01-15T17:24:51","modified_gmt":"2025-01-15T09:24:51","slug":"%e7%94%a8python%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e5%9b%be%e7%89%87%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1174923.html","title":{"rendered":"\u7528python\u5982\u4f55\u521b\u5efa\u56fe\u7247\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25110522\/8d0fa888-7b0f-4277-92d3-a986c2e0710e.webp\" alt=\"\u7528python\u5982\u4f55\u521b\u5efa\u56fe\u7247\u6587\u4ef6\" \/><\/p>\n<p><p> \u4e00\u3001\u6982\u8ff0<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u521b\u5efa\u56fe\u7247\u6587\u4ef6<\/strong>\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528Pillow\u5e93\u3001\u4f7f\u7528Matplotlib\u5e93\u3001\u4f7f\u7528OpenCV\u5e93\u7b49\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u6709\u4e0d\u540c\u7684\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>Pillow\u5e93<\/strong>\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\uff0c\u5e76\u63d0\u4f9b\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002<strong>Matplotlib\u5e93<\/strong>\u4e3b\u8981\u7528\u4e8e\u7ed8\u56fe\u548c\u6570\u636e\u53ef\u89c6\u5316\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u548c\u4fdd\u5b58\u56fe\u7247\u3002<strong>OpenCV\u5e93<\/strong>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u652f\u6301\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u548c\u5206\u6790\u529f\u80fd\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u521b\u5efa\u56fe\u7247\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001PILLOW\u5e93<\/p>\n<\/p>\n<p><p>Pillow\uff08PIL\u7684\u53cb\u597d\u5206\u652f\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u548c\u64cd\u4f5c\u56fe\u50cf\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Pillow\u5e93\u521b\u5efa\u56fe\u7247\u6587\u4ef6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5Pillow\u5e93<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Pillow\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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>\u521b\u5efa\u7b80\u5355\u7684\u56fe\u7247\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u5e93\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u7247\u6587\u4ef6\uff0c\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684RGB\u56fe\u50cf\uff0c\u5927\u5c0f\u4e3a100x100\uff0c\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272<\/strong><\/h2>\n<p>image = Image.new(&#39;RGB&#39;, (100, 100), &#39;white&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image.save(&#39;simple_image.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5728\u56fe\u7247\u4e0a\u7ed8\u5236\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>Pillow\u5e93\u8fd8\u53ef\u4ee5\u5728\u56fe\u7247\u4e0a\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\uff0c\u5982\u7ebf\u6761\u3001\u77e9\u5f62\u3001\u5706\u5f62\u7b49\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684RGB\u56fe\u50cf\uff0c\u5927\u5c0f\u4e3a200x200\uff0c\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272<\/strong><\/h2>\n<p>image = Image.new(&#39;RGB&#39;, (200, 200), &#39;white&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aImageDraw\u5bf9\u8c61<\/strong><\/h2>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u6761\u7ea2\u8272\u7684\u7ebf<\/strong><\/h2>\n<p>draw.line((0, 0, 200, 200), fill=&#39;red&#39;, width=3)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e2a\u84dd\u8272\u7684\u77e9\u5f62<\/strong><\/h2>\n<p>draw.rectangle((50, 50, 150, 150), outline=&#39;blue&#39;, width=3)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e2a\u7eff\u8272\u7684\u692d\u5706<\/strong><\/h2>\n<p>draw.ellipse((75, 75, 125, 125), outline=&#39;green&#39;, width=3)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image.save(&#39;draw_image.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5728\u56fe\u7247\u4e0a\u6dfb\u52a0\u6587\u672c<\/h3>\n<\/p>\n<p><p>Pillow\u5e93\u8fd8\u53ef\u4ee5\u5728\u56fe\u7247\u4e0a\u6dfb\u52a0\u6587\u672c\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw, ImageFont<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684RGB\u56fe\u50cf\uff0c\u5927\u5c0f\u4e3a300x100\uff0c\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272<\/strong><\/h2>\n<p>image = Image.new(&#39;RGB&#39;, (300, 100), &#39;white&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aImageDraw\u5bf9\u8c61<\/strong><\/h2>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53<\/strong><\/h2>\n<p>font = ImageFont.truetype(&quot;arial.ttf&quot;, 36)<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u672c<\/strong><\/h2>\n<p>draw.text((10, 30), &quot;Hello, Pillow!&quot;, fill=&#39;black&#39;, font=font)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image.save(&#39;text_image.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001MATPLOTLIB\u5e93<\/p>\n<\/p>\n<p><p>Matplotlib\u5e93\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u548c\u4fdd\u5b58\u56fe\u7247\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Matplotlib\u5e93\u521b\u5efa\u56fe\u7247\u6587\u4ef6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5Matplotlib\u5e93<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Matplotlib\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u521b\u5efa\u7b80\u5355\u7684\u56fe\u7247\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u5e93\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u7247\u6587\u4ef6\uff0c\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/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>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u50cf<\/strong><\/h2>\n<p>ax.plot(x, y)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>plt.savefig(&#39;simple_plot.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5728\u56fe\u7247\u4e0a\u7ed8\u5236\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>Matplotlib\u5e93\u8fd8\u53ef\u4ee5\u5728\u56fe\u7247\u4e0a\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\uff0c\u5982\u7ebf\u6761\u3001\u77e9\u5f62\u3001\u5706\u5f62\u7b49\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.patches as patches<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u6761\u7ea2\u8272\u7684\u7ebf<\/strong><\/h2>\n<p>line = patches.FancyArrowPatch((0, 0), (1, 1), color=&#39;red&#39;)<\/p>\n<p>ax.add_patch(line)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e2a\u84dd\u8272\u7684\u77e9\u5f62<\/strong><\/h2>\n<p>rect = patches.Rectangle((0.5, 0.5), 0.2, 0.3, linewidth=1, edgecolor=&#39;blue&#39;, facecolor=&#39;none&#39;)<\/p>\n<p>ax.add_patch(rect)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e2a\u7eff\u8272\u7684\u5706\u5f62<\/strong><\/h2>\n<p>circle = patches.Circle((0.8, 0.8), 0.1, linewidth=1, edgecolor=&#39;green&#39;, facecolor=&#39;none&#39;)<\/p>\n<p>ax.add_patch(circle)<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u50cf\u7684\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim([0, 1])<\/p>\n<p>ax.set_ylim([0, 1])<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>plt.savefig(&#39;draw_plot.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5728\u56fe\u7247\u4e0a\u6dfb\u52a0\u6587\u672c<\/h3>\n<\/p>\n<p><p>Matplotlib\u5e93\u8fd8\u53ef\u4ee5\u5728\u56fe\u7247\u4e0a\u6dfb\u52a0\u6587\u672c\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u672c<\/strong><\/h2>\n<p>ax.text(0.5, 0.5, &quot;Hello, Matplotlib!&quot;, fontsize=12, ha=&#39;center&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u50cf\u7684\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim([0, 1])<\/p>\n<p>ax.set_ylim([0, 1])<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>plt.savefig(&#39;text_plot.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001OPENCV\u5e93<\/p>\n<\/p>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u652f\u6301\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u548c\u5206\u6790\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528OpenCV\u5e93\u521b\u5efa\u56fe\u7247\u6587\u4ef6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5OpenCV\u5e93<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528OpenCV\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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>\u521b\u5efa\u7b80\u5355\u7684\u56fe\u7247\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u5e93\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u7247\u6587\u4ef6\uff0c\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/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\u7684RGB\u56fe\u50cf\uff0c\u5927\u5c0f\u4e3a100x100\uff0c\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272<\/strong><\/h2>\n<p>image = np.ones((100, 100, 3), dtype=np.uint8) * 255<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>cv2.imwrite(&#39;simple_image.png&#39;, image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5728\u56fe\u7247\u4e0a\u7ed8\u5236\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>OpenCV\u5e93\u8fd8\u53ef\u4ee5\u5728\u56fe\u7247\u4e0a\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\uff0c\u5982\u7ebf\u6761\u3001\u77e9\u5f62\u3001\u5706\u5f62\u7b49\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/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\u7684RGB\u56fe\u50cf\uff0c\u5927\u5c0f\u4e3a200x200\uff0c\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272<\/strong><\/h2>\n<p>image = np.ones((200, 200, 3), dtype=np.uint8) * 255<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u6761\u7ea2\u8272\u7684\u7ebf<\/strong><\/h2>\n<p>cv2.line(image, (0, 0), (200, 200), (0, 0, 255), 3)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e2a\u84dd\u8272\u7684\u77e9\u5f62<\/strong><\/h2>\n<p>cv2.rectangle(image, (50, 50), (150, 150), (255, 0, 0), 3)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e2a\u7eff\u8272\u7684\u5706\u5f62<\/strong><\/h2>\n<p>cv2.circle(image, (100, 100), 25, (0, 255, 0), 3)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>cv2.imwrite(&#39;draw_image.png&#39;, image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5728\u56fe\u7247\u4e0a\u6dfb\u52a0\u6587\u672c<\/h3>\n<\/p>\n<p><p>OpenCV\u5e93\u8fd8\u53ef\u4ee5\u5728\u56fe\u7247\u4e0a\u6dfb\u52a0\u6587\u672c\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/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\u7684RGB\u56fe\u50cf\uff0c\u5927\u5c0f\u4e3a300x100\uff0c\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272<\/strong><\/h2>\n<p>image = np.ones((300, 100, 3), dtype=np.uint8) * 255<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u672c<\/strong><\/h2>\n<p>cv2.putText(image, &quot;Hello, OpenCV!&quot;, (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2, cv2.LINE_AA)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>cv2.imwrite(&#39;text_image.png&#39;, image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u4f7f\u7528Python\u521b\u5efa\u56fe\u7247\u6587\u4ef6\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\u3002<strong>Pillow\u5e93<\/strong>\u9002\u7528\u4e8e\u5404\u79cd\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c<strong>Matplotlib\u5e93<\/strong>\u9002\u7528\u4e8e\u7ed8\u56fe\u548c\u6570\u636e\u53ef\u89c6\u5316\uff0c<strong>OpenCV\u5e93<\/strong>\u5219\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u9700\u8981\u521b\u5efa\u548c\u4fdd\u5b58\u56fe\u7247\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u8fdb\u884c\u7b80\u5355\u7684\u56fe\u50cf\u5904\u7406\u548c\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\uff1b\u5982\u679c\u9700\u8981\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\uff1b\u5982\u679c\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u548c\u5206\u6790\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u521b\u5efa\u548c\u64cd\u4f5c\u56fe\u7247\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u521b\u5efa\u4e0d\u540c\u683c\u5f0f\u7684\u56fe\u7247\u6587\u4ef6\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u56fe\u7247\u683c\u5f0f\u7684\u521b\u5efa\uff0c\u5305\u62ecPNG\u3001JPEG\u548cGIF\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528PIL\uff08Pillow\uff09\u5e93\u8f7b\u677e\u521b\u5efa\u548c\u4fdd\u5b58\u8fd9\u4e9b\u683c\u5f0f\u7684\u56fe\u7247\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Pillow\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install Pillow<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u5f20\u7b80\u5355\u7684PNG\u56fe\u7247\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u521b\u5efa\u4e00\u5f20\u65b0\u7684\u56fe\u7247\uff0c\u6a21\u5f0f\u4e3aRGB\uff0c\u5c3a\u5bf8\u4e3a200x200\uff0c\u80cc\u666f\u4e3a\u767d\u8272\nimage = Image.new(&quot;RGB&quot;, (200, 200), &quot;white&quot;)\nimage.save(&quot;example.png&quot;)  # \u4fdd\u5b58\u4e3aPNG\u6587\u4ef6\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u7ed8\u5236\u56fe\u5f62\u5e76\u4fdd\u5b58\u4e3a\u56fe\u7247\uff1f<\/strong><br \/>\u9664\u4e86\u521b\u5efa\u7b80\u5355\u7684\u56fe\u7247\uff0cPython\u8fd8\u53ef\u4ee5\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002\u4f7f\u7528Pillow\u5e93\u7684<code>ImageDraw<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u7ed8\u5236\u7ebf\u6761\u3001\u77e9\u5f62\u3001\u5706\u5f62\u7b49\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u7ed8\u5236\u4e00\u4e2a\u7ea2\u8272\u7684\u77e9\u5f62\u5e76\u4fdd\u5b58\u4e3aJPEG\u683c\u5f0f\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image, ImageDraw\n\n# \u521b\u5efa\u4e00\u5f20\u65b0\u7684\u56fe\u7247\nimage = Image.new(&quot;RGB&quot;, (200, 200), &quot;white&quot;)\ndraw = ImageDraw.Draw(image)\n\n# \u7ed8\u5236\u4e00\u4e2a\u7ea2\u8272\u7684\u77e9\u5f62\ndraw.rectangle([50, 50, 150, 150], fill=&quot;red&quot;)\nimage.save(&quot;rectangle.jpg&quot;)  # \u4fdd\u5b58\u4e3aJPEG\u6587\u4ef6\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6279\u91cf\u521b\u5efa\u56fe\u7247\u6587\u4ef6\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u6279\u91cf\u521b\u5efa\u591a\u4e2a\u56fe\u7247\u6587\u4ef6\uff0c\u53ef\u4ee5\u5c06\u521b\u5efa\u903b\u8f91\u653e\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\uff0c\u6839\u636e\u9700\u8981\u8c03\u6574\u6bcf\u5f20\u56fe\u7247\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u793a\u4f8b\u521b\u5efa\u4e865\u5f20\u4e0d\u540c\u989c\u8272\u7684\u56fe\u7247\uff1a  <\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\ncolors = [&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;yellow&quot;, &quot;purple&quot;]\n\nfor index, color in enumerate(colors):\n    image = Image.new(&quot;RGB&quot;, (200, 200), color)\n    image.save(f&quot;color_image_{index}.png&quot;)  # \u4fdd\u5b58\u4e3aPNG\u6587\u4ef6\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u9ad8\u6548\uff0c\u800c\u4e14\u7075\u6d3b\uff0c\u5141\u8bb8\u7528\u6237\u6839\u636e\u9700\u6c42\u81ea\u5b9a\u4e49\u6bcf\u5f20\u56fe\u7247\u7684\u989c\u8272\u548c\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u6982\u8ff0 \u4f7f\u7528Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u521b\u5efa\u56fe\u7247\u6587\u4ef6\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528Pillow\u5e93\u3001\u4f7f\u7528Matplotlib\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1174929,"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\/1174923"}],"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=1174923"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174923\/revisions"}],"predecessor-version":[{"id":1174932,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174923\/revisions\/1174932"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1174929"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1174923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1174923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1174923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}