{"id":1014028,"date":"2024-12-27T11:52:49","date_gmt":"2024-12-27T03:52:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1014028.html"},"modified":"2024-12-27T11:52:51","modified_gmt":"2024-12-27T03:52:51","slug":"python-%e5%a6%82%e4%bd%95%e5%88%b6%e4%bd%9c%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1014028.html","title":{"rendered":"python \u5982\u4f55\u5236\u4f5c\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094251\/c42660ef-89b1-486e-be62-aa2180b3b922.webp\" alt=\"python \u5982\u4f55\u5236\u4f5c\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u5236\u4f5c\u56fe\u7247\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528PIL\uff08Python Imaging Library\uff09\u5e93\u3001\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf\u5bf9\u8c61\u3001\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u5f62\u72b6\u548c\u6587\u672c\u3001\u4fdd\u5b58\u56fe\u50cf\u5230\u6587\u4ef6\u3002PIL\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u5e76\u4e14\u975e\u5e38\u5bb9\u6613\u4e0a\u624b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u8be6\u7ec6\u89e3\u91ca\u5176\u4e2d\u4e00\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u7740\u91cd\u4e8e\u5982\u4f55\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u5f62\u72b6\u548c\u6587\u672c\u3002PIL\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3aImageDraw\u7684\u6a21\u5757\uff0c\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7ec4\u7528\u4e8e\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u57fa\u672c\u5f62\u72b6\u548c\u6587\u672c\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u56fe\u50cf\u4e0a\u6dfb\u52a0\u5404\u79cd\u56fe\u5f62\u5143\u7d20\uff0c\u4f8b\u5982\u77e9\u5f62\u3001\u692d\u5706\u3001\u7ebf\u6761\u548c\u6587\u672c\u3002\u8fd9\u4e9b\u529f\u80fd\u5bf9\u4e8e\u5236\u4f5c\u56fe\u50cf\u3001\u6dfb\u52a0\u6c34\u5370\u6216\u521b\u5efa\u7b80\u5355\u7684\u56fe\u5f62\u8bbe\u8ba1\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u5236\u4f5c\u56fe\u7247\u7684\u8fc7\u7a0b\uff0c\u5305\u62ecPIL\u5e93\u7684\u5b89\u88c5\u3001\u56fe\u50cf\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u64cd\u4f5c\u3001\u56fe\u50cf\u4fdd\u5b58\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5PIL\u5e93<\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528PIL\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Pillow\uff0c\u8fd9\u662fPIL\u5e93\u7684\u73b0\u4ee3\u5316\u5206\u652f\uff0c\u652f\u6301\u66f4\u591a\u7684\u56fe\u50cf\u683c\u5f0f\u548c\u529f\u80fd\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\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\u4f60\u5c31\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165PIL\u5e93\u5e76\u4f7f\u7528\u5176\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u521b\u5efa\u56fe\u50cf\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u56fe\u50cf\u5bf9\u8c61\u662f\u5236\u4f5c\u56fe\u7247\u7684\u7b2c\u4e00\u6b65\u3002\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\u4e2d\u7684Image\u6a21\u5757\u6765\u521b\u5efa\u65b0\u7684\u56fe\u50cf\u5bf9\u8c61\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aRGB\u6a21\u5f0f\u7684\u56fe\u50cf\uff0c\u5927\u5c0f\u4e3a800x600\uff0c\u80cc\u666f\u8272\u4e3a\u767d\u8272<\/strong><\/h2>\n<p>image = Image.new(&#39;RGB&#39;, (800, 600), &#39;white&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5927\u5c0f\u4e3a800&#215;600\u7684RGB\u56fe\u50cf\uff0c\u5e76\u5c06\u80cc\u666f\u8272\u8bbe\u7f6e\u4e3a\u767d\u8272\u3002\u901a\u8fc7\u6307\u5b9a\u4e0d\u540c\u7684\u6a21\u5f0f\u548c\u989c\u8272\uff0c\u53ef\u4ee5\u521b\u5efa\u5404\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u5f62\u72b6\u548c\u6587\u672c<\/p>\n<\/p>\n<p><p>\u4f7f\u7528PIL\u5e93\u7684ImageDraw\u6a21\u5757\uff0c\u53ef\u4ee5\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u5404\u79cd\u5f62\u72b6\u548c\u6587\u672c\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528ImageDraw\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u77e9\u5f62\u3001\u692d\u5706\u548c\u6587\u672c\u7684\u793a\u4f8b\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\u56fe\u50cf\u5bf9\u8c61<\/strong><\/h2>\n<p>image = Image.new(&#39;RGB&#39;, (800, 600), &#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\u4e2a\u77e9\u5f62<\/strong><\/h2>\n<p>draw.rectangle([50, 50, 200, 200], outline=&#39;black&#39;, fill=&#39;blue&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e2a\u692d\u5706<\/strong><\/h2>\n<p>draw.ellipse([300, 50, 450, 200], outline=&#39;red&#39;, fill=&#39;green&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u6587\u672c<\/strong><\/h2>\n<p>font = ImageFont.truetype(&quot;arial.ttf&quot;, 36)<\/p>\n<p>draw.text((100, 300), &quot;Hello, PIL!&quot;, fill=&#39;black&#39;, font=font)<\/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\u4e2aImageDraw\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u5bf9\u8c61\u7684rectangle\u3001ellipse\u548ctext\u65b9\u6cd5\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u77e9\u5f62\u3001\u692d\u5706\u548c\u6587\u672c\u3002\u6ce8\u610f\uff0c\u7ed8\u5236\u6587\u672c\u65f6\u9700\u8981\u6307\u5b9a\u5b57\u4f53\uff0c\u53ef\u4ee5\u4f7f\u7528ImageFont.truetype\u65b9\u6cd5\u52a0\u8f7d\u5b57\u4f53\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4fdd\u5b58\u56fe\u50cf<\/p>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u56fe\u50cf\u7684\u7ed8\u5236\u540e\uff0c\u4f60\u53ef\u4ee5\u5c06\u56fe\u50cf\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u4f7f\u7528PIL\u5e93\u7684Image\u5bf9\u8c61\u7684save\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u5404\u79cd\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u4f8b\u5982PNG\u3001JPEG\u7b49\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4fdd\u5b58\u56fe\u50cf\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u56fe\u50cf\u5230\u6587\u4ef6<\/p>\n<p>image.save(&#39;output_image.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3aPNG\u683c\u5f0f\uff0c\u5e76\u5c06\u6587\u4ef6\u540d\u6307\u5b9a\u4e3aoutput_image.png\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5e94\u7528\u5b9e\u4f8b<\/p>\n<\/p>\n<p><p>\u7ed3\u5408\u4e0a\u8ff0\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u5236\u4f5c\u4e00\u4e2a\u7b80\u5355\u7684\u6d77\u62a5\u6216\u8005\u56fe\u5f62\u3002\u5047\u8bbe\u6211\u4eec\u8981\u5236\u4f5c\u4e00\u4e2a\u5305\u542b\u516c\u53f8\u540d\u79f0\u548cLogo\u7684\u7b80\u5355\u6d77\u62a5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\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\u56fe\u50cf\u5bf9\u8c61<\/strong><\/h2>\n<p>image = Image.new(&#39;RGB&#39;, (800, 600), &#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\u516c\u53f8Logo\uff08\u7b80\u5355\u8868\u793a\u4e3a\u4e00\u4e2a\u5706\u5f62\uff09<\/strong><\/h2>\n<p>draw.ellipse([350, 50, 450, 150], outline=&#39;black&#39;, fill=&#39;orange&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u516c\u53f8\u540d\u79f0<\/strong><\/h2>\n<p>font = ImageFont.truetype(&quot;arial.ttf&quot;, 48)<\/p>\n<p>draw.text((200, 200), &quot;Awesome Company&quot;, fill=&#39;black&#39;, font=font)<\/p>\n<h2><strong>\u6dfb\u52a0\u4e00\u4e9b\u88c5\u9970<\/strong><\/h2>\n<p>draw.line([0, 250, 800, 250], fill=&#39;black&#39;, width=3)<\/p>\n<p>draw.rectangle([50, 300, 750, 550], outline=&#39;black&#39;, width=2)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf\u5230\u6587\u4ef6<\/strong><\/h2>\n<p>image.save(&#39;company_poster.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u5e45\u7b80\u5355\u7684\u516c\u53f8\u6d77\u62a5\uff0c\u5305\u542b\u516c\u53f8\u540d\u79f0\u548cLogo\u3002Logo\u7b80\u5355\u5730\u7528\u4e00\u4e2a\u6a59\u8272\u7684\u5706\u5f62\u8868\u793a\uff0c\u516c\u53f8\u540d\u79f0\u7528\u9ed1\u8272\u6587\u672c\u663e\u793a\uff0c\u5e76\u5728\u56fe\u50cf\u4e0a\u6dfb\u52a0\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u88c5\u9970\u5143\u7d20\uff0c\u5982\u7ebf\u6761\u548c\u77e9\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7PIL\u5e93\uff0cPython\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u80fd\u529b\u3002\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u548c\u64cd\u4f5c\u56fe\u50cf\uff0c\u7ed8\u5236\u5404\u79cd\u5f62\u72b6\u548c\u6587\u672c\uff0c\u5e76\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u5404\u79cd\u683c\u5f0f\u7684\u6587\u4ef6\u3002PIL\u5e93\u7684\u4e30\u5bcc\u529f\u80fd\u4f7f\u5176\u6210\u4e3a\u5236\u4f5c\u56fe\u7247\u3001\u56fe\u5f62\u8bbe\u8ba1\u548c\u56fe\u50cf\u5904\u7406\u7684\u7406\u60f3\u5de5\u5177\u3002\u5229\u7528\u8fd9\u4e9b\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u8bb8\u591a\u6709\u8da3\u7684\u56fe\u50cf\u5904\u7406\u5e94\u7528\uff0c\u5982\u751f\u6210\u52a8\u6001\u6d77\u62a5\u3001\u6dfb\u52a0\u6c34\u5370\u3001\u5236\u4f5c\u56fe\u6807\u7b49\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u751f\u6210\u56fe\u50cf\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u521b\u5efa\u548c\u5904\u7406\u56fe\u50cf\uff0c\u4f8b\u5982Pillow\u3001Matplotlib\u548cOpenCV\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5730\u751f\u6210\u56fe\u50cf\uff0c\u7ed8\u5236\u56fe\u5f62\u6216\u5904\u7406\u73b0\u6709\u7684\u56fe\u50cf\u6587\u4ef6\u3002\u5b89\u88c5\u8fd9\u4e9b\u5e93\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684\u4ee3\u7801\u6765\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u50cf\uff0c\u4f8b\u5982\u7b80\u5355\u7684\u56fe\u5f62\u3001\u6587\u672c\u6216\u590d\u6742\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u7ed8\u5236\u56fe\u5f62\u548c\u56fe\u50cf\uff1f<\/strong><br \/>\u5229\u7528Matplotlib\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\uff0c\u5982\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u548c\u6563\u70b9\u56fe\u3002\u53ea\u9700\u5bfc\u5165\u5e93\u5e76\u7f16\u5199\u51e0\u884c\u4ee3\u7801\uff0c\u5373\u53ef\u751f\u6210\u9ad8\u8d28\u91cf\u7684\u56fe\u50cf\u6587\u4ef6\u3002\u6b64\u5916\uff0cPillow\u5e93\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u5728\u56fe\u50cf\u4e0a\u6dfb\u52a0\u6587\u672c\u3001\u5f62\u72b6\u548c\u6ee4\u955c\uff0c\u4ece\u800c\u589e\u5f3a\u56fe\u50cf\u6548\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u751f\u6210\u7684\u56fe\u50cf\u4fdd\u5b58\u4e3a\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u7684<code>save()<\/code>\u65b9\u6cd5\u5c06\u521b\u5efa\u7684\u56fe\u50cf\u4fdd\u5b58\u4e3a\u591a\u79cd\u683c\u5f0f\uff0c\u5982JPEG\u3001PNG\u6216GIF\u7b49\u3002\u5f53\u7528\u6237\u5b8c\u6210\u56fe\u50cf\u7684\u521b\u5efa\u6216\u7f16\u8f91\u540e\uff0c\u53ea\u9700\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u683c\u5f0f\uff0c\u5373\u53ef\u5c06\u56fe\u50cf\u4fdd\u5b58\u5230\u672c\u5730\u8ba1\u7b97\u673a\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\u6216\u5206\u4eab\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5236\u4f5c\u56fe\u7247\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528PIL\uff08Python Imaging Library\uff09\u5e93\u3001\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe [&hellip;]","protected":false},"author":3,"featured_media":1014034,"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\/1014028"}],"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=1014028"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1014028\/revisions"}],"predecessor-version":[{"id":1014036,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1014028\/revisions\/1014036"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1014034"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1014028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1014028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1014028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}