{"id":1109378,"date":"2025-01-08T17:09:29","date_gmt":"2025-01-08T09:09:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1109378.html"},"modified":"2025-01-08T17:09:33","modified_gmt":"2025-01-08T09:09:33","slug":"python%e5%a6%82%e4%bd%95%e5%bb%ba%e7%ab%8b%e4%b8%80%e4%b8%aa%e6%96%b0%e7%9a%84%e5%9b%be%e5%83%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1109378.html","title":{"rendered":"python\u5982\u4f55\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072652\/3dbe90c8-9a40-4469-aed0-0f06be343a39.webp\" alt=\"python\u5982\u4f55\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf\uff0c\u53ef\u4ee5\u901a\u8fc7\u5f15\u5165\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u5982PIL\uff08Pillow\uff09\u6216OpenCV\u6765\u5b8c\u6210\u3002\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u521b\u5efa\u4e00\u4e2a\u7a7a\u767d\u56fe\u50cf\u3001\u586b\u5145\u56fe\u50cf\u5185\u5bb9\u3001\u4fdd\u5b58\u56fe\u50cf\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PIL\u5e93\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf\uff0c\u5e76\u5bf9\u5176\u4e2d\u4f7f\u7528\u5e93\u7684\u5b89\u88c5\u548c\u57fa\u672c\u64cd\u4f5c\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86Pillow\u5e93\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\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\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165\u6240\u9700\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7a7a\u767d\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Pillow\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7a7a\u767d\u56fe\u50cf\u3002\u4f60\u53ef\u4ee5\u6307\u5b9a\u56fe\u50cf\u7684\u5c3a\u5bf8\u3001\u989c\u8272\u6a21\u5f0f\uff08\u6bd4\u5982RGB\uff09\u548c\u80cc\u666f\u989c\u8272\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa\u7a7a\u767d\u56fe\u50cf\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2aRGB\u6a21\u5f0f\u7684\u7a7a\u767d\u56fe\u50cf\uff0c\u5c3a\u5bf8\u4e3a800x600\uff0c\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272<\/p>\n<p>width, height = 800, 600<\/p>\n<p>background_color = (255, 255, 255)<\/p>\n<p>image = Image.new(&#39;RGB&#39;, (width, height), background_color)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed8\u5236\u5185\u5bb9\u5230\u56fe\u50cf\u4e0a<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e86\u4e00\u4e2a\u7a7a\u767d\u56fe\u50cf\u4e4b\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u63d0\u4f9b\u7684<code>ImageDraw<\/code>\u6a21\u5757\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u5404\u79cd\u5f62\u72b6\u3001\u6587\u672c\u7b49\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7ed8\u5236\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u7ed8\u5236\u77e9\u5f62<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">draw = ImageDraw.Draw(image)<\/p>\n<p>rect_start = (50, 50)<\/p>\n<p>rect_end = (200, 200)<\/p>\n<p>rect_color = (255, 0, 0)  # \u7ea2\u8272<\/p>\n<p>draw.rectangle([rect_start, rect_end], fill=rect_color)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7ed8\u5236\u5706\u5f62<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">circle_center = (400, 300)<\/p>\n<p>circle_radius = 100<\/p>\n<p>circle_color = (0, 255, 0)  # \u7eff\u8272<\/p>\n<p>draw.ellipse((circle_center[0] - circle_radius, circle_center[1] - circle_radius, <\/p>\n<p>              circle_center[0] + circle_radius, circle_center[1] + circle_radius), <\/p>\n<p>             fill=circle_color)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ed8\u5236\u6587\u672c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text_position = (100, 500)<\/p>\n<p>text = &quot;Hello, PIL!&quot;<\/p>\n<p>text_color = (0, 0, 255)  # \u84dd\u8272<\/p>\n<p>draw.text(text_position, text, fill=text_color)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4fdd\u5b58\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u56fe\u50cf\u7684\u7ed8\u5236\u4e4b\u540e\uff0c\u4f60\u53ef\u4ee5\u5c06\u56fe\u50cf\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">image.save(&#39;new_image.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5c55\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u63d0\u4f9b\u7684<code>show<\/code>\u65b9\u6cd5\u5728\u9ed8\u8ba4\u56fe\u50cf\u67e5\u770b\u5668\u4e2d\u663e\u793a\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Pillow\u5e93\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf\u5e76\u8fdb\u884c\u7ed8\u5236\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aRGB\u6a21\u5f0f\u7684\u7a7a\u767d\u56fe\u50cf\uff0c\u5c3a\u5bf8\u4e3a800x600\uff0c\u80cc\u666f\u989c\u8272\u4e3a\u767d\u8272<\/strong><\/h2>\n<p>width, height = 800, 600<\/p>\n<p>background_color = (255, 255, 255)<\/p>\n<p>image = Image.new(&#39;RGB&#39;, (width, height), background_color)<\/p>\n<h2><strong>\u83b7\u53d6\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<h2><strong>\u7ed8\u5236\u77e9\u5f62<\/strong><\/h2>\n<p>rect_start = (50, 50)<\/p>\n<p>rect_end = (200, 200)<\/p>\n<p>rect_color = (255, 0, 0)  # \u7ea2\u8272<\/p>\n<p>draw.rectangle([rect_start, rect_end], fill=rect_color)<\/p>\n<h2><strong>\u7ed8\u5236\u5706\u5f62<\/strong><\/h2>\n<p>circle_center = (400, 300)<\/p>\n<p>circle_radius = 100<\/p>\n<p>circle_color = (0, 255, 0)  # \u7eff\u8272<\/p>\n<p>draw.ellipse((circle_center[0] - circle_radius, circle_center[1] - circle_radius, <\/p>\n<p>              circle_center[0] + circle_radius, circle_center[1] + circle_radius), <\/p>\n<p>             fill=circle_color)<\/p>\n<h2><strong>\u7ed8\u5236\u6587\u672c<\/strong><\/h2>\n<p>text_position = (100, 500)<\/p>\n<p>text = &quot;Hello, PIL!&quot;<\/p>\n<p>text_color = (0, 0, 255)  # \u84dd\u8272<\/p>\n<p>draw.text(text_position, text, fill=text_color)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&#39;new_image.png&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u7684Pillow\u5e93\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf\u3002\u901a\u8fc7\u5b89\u88c5\u5e93\u3001\u521b\u5efa\u7a7a\u767d\u56fe\u50cf\u3001\u7ed8\u5236\u5185\u5bb9\u3001\u4fdd\u5b58\u548c\u5c55\u793a\u56fe\u50cf\u7b49\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u8fdb\u884c\u57fa\u672c\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u3002<strong>Pillow\u5e93\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\uff0c\u4f7f\u5f97\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u53d8\u5f97\u975e\u5e38\u4fbf\u6377\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u8fdb\u884c\u66f4\u590d\u6742\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u5982\u56fe\u50cf\u8fc7\u6ee4\u3001\u53d8\u6362\u7b49\uff0c\u53ef\u4ee5\u53c2\u8003Pillow\u7684\u5b98\u65b9\u6587\u6863\u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002Pillow\u5e93\u7684\u7075\u6d3b\u6027\u548c\u5f3a\u5927\u529f\u80fd\uff0c\u4f7f\u5176\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\u975e\u5e38\u53d7\u6b22\u8fce\uff0c\u662fPython\u5f00\u53d1\u8005\u7684\u9996\u9009\u5de5\u5177\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u521b\u5efa\u65b0\u7684\u56fe\u50cf\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528PIL\uff08Python Imaging Library\uff09\u5e93\u4e2d\u7684<code>Image<\/code>\u6a21\u5757\u6765\u521b\u5efa\u65b0\u7684\u56fe\u50cf\u6587\u4ef6\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Pillow\u5e93\uff08PIL\u7684\u4e00\u4e2a\u5206\u652f\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install Pillow<\/code>\u6765\u5b8c\u6210\u3002\u521b\u5efa\u56fe\u50cf\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u56fe\u50cf\u7684\u5c3a\u5bf8\u548c\u989c\u8272\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a200&#215;200\u50cf\u7d20\u7684\u7ea2\u8272\u56fe\u50cf\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image\n\n# \u521b\u5efa\u4e00\u4e2a200x200\u7684\u7ea2\u8272\u56fe\u50cf\nimg = Image.new(&#39;RGB&#39;, (200, 200), color=&#39;red&#39;)\nimg.save(&#39;red_image.png&#39;)\n<\/code><\/pre>\n<p><strong>Python\u652f\u6301\u54ea\u4e9b\u56fe\u50cf\u683c\u5f0f\u7684\u521b\u5efa\u548c\u4fdd\u5b58\uff1f<\/strong><br \/>\u4f7f\u7528Pillow\u5e93\uff0cPython\u652f\u6301\u591a\u79cd\u56fe\u50cf\u683c\u5f0f\u7684\u521b\u5efa\u548c\u4fdd\u5b58\uff0c\u5305\u62ecJPEG\u3001PNG\u3001BMP\u3001GIF\u7b49\u3002\u4e0d\u540c\u7684\u683c\u5f0f\u9002\u5408\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u4f8b\u5982\uff0cJPEG\u9002\u5408\u4fdd\u5b58\u7167\u7247\uff0c\u800cPNG\u9002\u5408\u9700\u8981\u900f\u660e\u80cc\u666f\u7684\u56fe\u50cf\u3002\u5728\u4fdd\u5b58\u56fe\u50cf\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6587\u4ef6\u6269\u5c55\u540d\u6765\u6307\u5b9a\u683c\u5f0f\uff0c\u5982<code>.jpg<\/code>\u3001<code>.png<\/code>\u7b49\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u521b\u5efa\u56fe\u50cf\u65f6\u6dfb\u52a0\u6587\u672c\u6216\u56fe\u5f62\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u7684<code>ImageDraw<\/code>\u6a21\u5757\u5728\u521b\u5efa\u7684\u56fe\u50cf\u4e0a\u6dfb\u52a0\u6587\u672c\u6216\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u56fe\u50cf\u4e0a\u6dfb\u52a0\u6587\u672c\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image, ImageDraw, ImageFont\n\n# \u521b\u5efa\u4e00\u4e2a\u7a7a\u767d\u56fe\u50cf\nimg = Image.new(&#39;RGB&#39;, (300, 100), color=&#39;white&#39;)\ndraw = ImageDraw.Draw(img)\n\n# \u6dfb\u52a0\u6587\u672c\nfont = ImageFont.load_default()\ndraw.text((10, 25), &quot;Hello, World!&quot;, fill=&#39;black&#39;, font=font)\n\nimg.save(&#39;text_image.png&#39;)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u751f\u6210\u4e00\u5e45\u5305\u542b\u201cHello, World!\u201d\u6587\u672c\u7684\u56fe\u50cf\u3002\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u6587\u672c\u4f4d\u7f6e\u3001\u5b57\u4f53\u548c\u989c\u8272\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf\uff0c\u53ef\u4ee5\u901a\u8fc7\u5f15\u5165\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u5982PIL\uff08Pillow\uff09\u6216OpenCV\u6765\u5b8c\u6210\u3002\u6838\u5fc3\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1109385,"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\/1109378"}],"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=1109378"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109378\/revisions"}],"predecessor-version":[{"id":1109388,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109378\/revisions\/1109388"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1109385"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1109378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1109378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1109378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}