{"id":1041195,"date":"2024-12-31T12:47:19","date_gmt":"2024-12-31T04:47:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1041195.html"},"modified":"2024-12-31T12:47:24","modified_gmt":"2024-12-31T04:47:24","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e5%9b%be%e7%89%87%e4%b8%8adraw%e5%80%be%e6%96%9c%e6%96%87%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1041195.html","title":{"rendered":"python\u5982\u4f55\u5728\u56fe\u7247\u4e0adraw\u503e\u659c\u6587\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/6b28acc2-a7f5-4275-976a-dbb761b84e15.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5728\u56fe\u7247\u4e0adraw\u503e\u659c\u6587\u5b57\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u5411\u56fe\u7247\u4e0a\u7ed8\u5236\u503e\u659c\u6587\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\uff08PIL\uff09\u3002Pillow\u5e93\u662fPython Imaging Library\uff08PIL\uff09\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u5b83\u589e\u52a0\u4e86\u4e00\u4e9b\u65b0\u529f\u80fd\u548c\u7b80\u5316\u4e86\u8bb8\u591a\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Pillow\u5e93<\/li>\n<li>\u52a0\u8f7d\u56fe\u7247<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u5b57\u4f53\u5bf9\u8c61<\/li>\n<li>\u4f7f\u7528<code>ImageDraw<\/code>\u6a21\u5757\u7684<code>text<\/code>\u65b9\u6cd5\u5c06\u6587\u5b57\u7ed8\u5236\u5230\u56fe\u7247\u4e0a\uff0c\u5e76\u5e94\u7528\u65cb\u8f6c\u8f6c\u6362<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u5b89\u88c5Pillow\u5e93<\/p>\n<p>pip install pillow<\/p>\n<h2><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/strong><\/h2>\n<p>from PIL import Image, ImageDraw, ImageFont<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u7247<\/strong><\/h2>\n<p>image = Image.open(&#39;your_image.jpg&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5b57\u4f53\u5bf9\u8c61<\/strong><\/h2>\n<p>font = ImageFont.truetype(&#39;arial.ttf&#39;, size=36)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6587\u5b57\u4f4d\u7f6e\u548c\u5185\u5bb9<\/strong><\/h2>\n<p>text = &quot;Hello World&quot;<\/p>\n<p>position = (100, 100)<\/p>\n<h2><strong>\u901a\u8fc7\u65cb\u8f6c\u56fe\u50cf\u6765\u5b9e\u73b0\u503e\u659c\u6587\u5b57<\/strong><\/h2>\n<p>text_image = Image.new(&#39;RGBA&#39;, draw.textsize(text, font=font), (255, 255, 255, 0))<\/p>\n<p>text_draw = ImageDraw.Draw(text_image)<\/p>\n<p>text_draw.text((0, 0), text, font=font, fill=&quot;black&quot;)<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/h2>\n<p>rotated_text_image = text_image.rotate(45, expand=1)<\/p>\n<h2><strong>\u5c06\u65cb\u8f6c\u540e\u7684\u6587\u5b57\u7c98\u8d34\u5230\u539f\u56fe\u50cf\u4e0a<\/strong><\/h2>\n<p>image.paste(rotated_text_image, position, rotated_text_image)<\/p>\n<h2><strong>\u4fdd\u5b58\u6216\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>image.save(&#39;output_image.jpg&#39;)<\/p>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u52a0\u8f7d\u4e86\u4e00\u5f20\u56fe\u7247\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a<code>ImageDraw<\/code>\u5bf9\u8c61\u4ee5\u4fbf\u5728\u56fe\u7247\u4e0a\u7ed8\u5236\u6587\u5b57\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u6587\u5b57\u7684\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u65cb\u8f6c\u4ee5\u5b9e\u73b0\u503e\u659c\u6548\u679c\u3002\u6700\u540e\uff0c\u5c06\u65cb\u8f6c\u540e\u7684\u6587\u5b57\u7c98\u8d34\u5230\u539f\u56fe\u50cf\u4e0a\u5e76\u4fdd\u5b58\u6216\u663e\u793a\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528Pillow\u5e93\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u56fe\u7247\u4e0a\u7ed8\u5236\u503e\u659c\u6587\u5b57\uff0c\u5e76\u4e14\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u5b57\u4f53\u3001\u5927\u5c0f\u3001\u989c\u8272\u548c\u65cb\u8f6c\u89d2\u5ea6\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001\u5b89\u88c5Pillow\u5e93<\/p>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u4f7f\u7528Pillow\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u5b89\u88c5Pillow\u5e93\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\u60a8\u5c31\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528Pillow\u5e93\u4e86\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u52a0\u8f7d\u56fe\u7247<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u52a0\u8f7d\u4e00\u5f20\u56fe\u7247\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u5f20\u540d\u4e3a&quot;your_image.jpg&quot;\u7684\u56fe\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u52a0\u8f7d\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>image = Image.open(&#39;your_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u52a0\u8f7d\u56fe\u7247\u540e\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a<code>ImageDraw<\/code>\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u5728\u56fe\u7247\u4e0a\u7ed8\u5236\u6587\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import ImageDraw<\/p>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u521b\u5efa\u4e00\u4e2a\u5b57\u4f53\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u7ed8\u5236\u6587\u5b57\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u521b\u5efa\u4e00\u4e2a<code>ImageFont<\/code>\u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528\u7cfb\u7edf\u4e2d\u7684\u5b57\u4f53\u6587\u4ef6\uff08\u4f8b\u5982Arial\u5b57\u4f53\uff09\u6765\u521b\u5efa\u5b57\u4f53\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import ImageFont<\/p>\n<p>font = ImageFont.truetype(&#39;arial.ttf&#39;, size=36)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u5b57\u4f53\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7ed8\u5236\u6587\u5b57<\/p>\n<\/p>\n<p><p>\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u51c6\u5907\u597d\u4e86\u6240\u6709\u5fc5\u8981\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5f00\u59cb\u7ed8\u5236\u503e\u659c\u6587\u5b57\u4e86\u3002\u9996\u5148\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6587\u5b57\u7684\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u65cb\u8f6c\u4ee5\u5b9e\u73b0\u503e\u659c\u6548\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello World&quot;<\/p>\n<p>position = (100, 100)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u900f\u660e\u80cc\u666f\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>text_image = Image.new(&#39;RGBA&#39;, draw.textsize(text, font=font), (255, 255, 255, 0))<\/p>\n<p>text_draw = ImageDraw.Draw(text_image)<\/p>\n<p>text_draw.text((0, 0), text, font=font, fill=&quot;black&quot;)<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/h2>\n<p>rotated_text_image = text_image.rotate(45, expand=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u900f\u660e\u80cc\u666f\u7684\u56fe\u50cf\uff0c\u5e76\u5728\u5176\u4e0a\u7ed8\u5236\u6587\u5b57\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>rotate<\/code>\u65b9\u6cd5\u5c06\u6587\u5b57\u56fe\u50cf\u65cb\u8f6c45\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5c06\u65cb\u8f6c\u540e\u7684\u6587\u5b57\u7c98\u8d34\u5230\u539f\u56fe\u50cf\u4e0a<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u5c06\u65cb\u8f6c\u540e\u7684\u6587\u5b57\u56fe\u50cf\u7c98\u8d34\u5230\u539f\u56fe\u50cf\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">image.paste(rotated_text_image, position, rotated_text_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u5c06\u503e\u659c\u7684\u6587\u5b57\u7c98\u8d34\u5230\u4e86\u539f\u56fe\u50cf\u7684\u6307\u5b9a\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4fdd\u5b58\u6216\u663e\u793a\u7ed3\u679c<\/p>\n<\/p>\n<p><p>\u7ed8\u5236\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u6216\u76f4\u63a5\u663e\u793a\u51fa\u6765\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">image.save(&#39;output_image.jpg&#39;)<\/p>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5904\u7406\u540e\u7684\u56fe\u50cf\u4fdd\u5b58\u4e3a&quot;output_image.jpg&quot;\uff0c\u5e76\u5728\u9ed8\u8ba4\u56fe\u50cf\u67e5\u770b\u5668\u4e2d\u663e\u793a\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u5728Python\u4e2d\u8f7b\u677e\u5730\u5728\u56fe\u7247\u4e0a\u7ed8\u5236\u503e\u659c\u6587\u5b57\u3002Pillow\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u4f7f\u60a8\u80fd\u591f\u6839\u636e\u9700\u8981\u8c03\u6574\u5b57\u4f53\u3001\u5927\u5c0f\u3001\u989c\u8272\u548c\u65cb\u8f6c\u89d2\u5ea6\uff0c\u4ece\u800c\u521b\u5efa\u51fa\u4e2a\u6027\u5316\u7684\u56fe\u50cf\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528OpenCV\u7ed8\u5236\u503e\u659c\u6587\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528OpenCV\u5e93\u7ed8\u5236\u503e\u659c\u6587\u5b57\u53ef\u4ee5\u901a\u8fc7\u7ed3\u5408\u56fe\u50cf\u65cb\u8f6c\u548c\u6587\u672c\u7ed8\u5236\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u6587\u672c\uff0c\u7136\u540e\u5bf9\u8be5\u6587\u672c\u8fdb\u884c\u65cb\u8f6c\u3002OpenCV\u63d0\u4f9b\u4e86<code>cv2.putText()<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u6587\u672c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>cv2.getRotationMatrix2D()<\/code>\u548c<code>cv2.warpAffine()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u65cb\u8f6c\u6548\u679c\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u521b\u5efa\u6587\u672c\u56fe\u50cf\u3001\u83b7\u53d6\u65cb\u8f6c\u77e9\u9635\u5e76\u5e94\u7528\u65cb\u8f6c\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5728\u54ea\u4e9b\u56fe\u50cf\u5904\u7406\u5e93\u4e2d\u7ed8\u5236\u503e\u659c\u6587\u5b57\uff1f<\/strong><br \/>\u9664\u4e86OpenCV\uff0c\u5176\u4ed6\u56fe\u50cf\u5904\u7406\u5e93\u5982Pillow\u548cMatplotlib\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u503e\u659c\u6587\u5b57\u7684\u7ed8\u5236\u3002Pillow\u63d0\u4f9b\u4e86<code>ImageDraw<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u6587\u672c\u56fe\u50cf\u5e76\u4f7f\u7528<code>rotate<\/code>\u65b9\u6cd5\u8fdb\u884c\u65cb\u8f6c\u3002Matplotlib\u5219\u5141\u8bb8\u5728\u7ed8\u56fe\u65f6\u8bbe\u7f6e\u6587\u672c\u7684\u89d2\u5ea6\u5c5e\u6027\uff0c\u4ece\u800c\u8f7b\u677e\u6dfb\u52a0\u503e\u659c\u6587\u5b57\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u6574\u503e\u659c\u6587\u5b57\u7684\u5927\u5c0f\u548c\u989c\u8272\uff1f<\/strong><br \/>\u5728\u4f7f\u7528OpenCV\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>cv2.putText()<\/code>\u51fd\u6570\u4e2d\u7684\u5b57\u4f53\u5927\u5c0f\u548c\u989c\u8272\u53c2\u6570\u6765\u8c03\u6574\u503e\u659c\u6587\u5b57\u7684\u5916\u89c2\u3002\u989c\u8272\u901a\u5e38\u4ee5BGR\u683c\u5f0f\u6307\u5b9a\uff0c\u800c\u5b57\u4f53\u5927\u5c0f\u5219\u7531\u5b57\u4f53\u7c7b\u578b\u548c\u7f29\u653e\u56e0\u5b50\u51b3\u5b9a\u3002\u5728Pillow\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>font<\/code>\u5bf9\u8c61\u7684\u5927\u5c0f\u548c\u6307\u5b9a\u989c\u8272\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u6548\u679c\u3002\u786e\u4fdd\u5728\u8c03\u6574\u65f6\u8003\u8651\u5230\u56fe\u50cf\u7684\u6574\u4f53\u7f8e\u89c2\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5411\u56fe\u7247\u4e0a\u7ed8\u5236\u503e\u659c\u6587\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\uff08PIL\uff09\u3002Pillow\u5e93\u662fPython Ima [&hellip;]","protected":false},"author":3,"featured_media":1041213,"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\/1041195"}],"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=1041195"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1041195\/revisions"}],"predecessor-version":[{"id":1041215,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1041195\/revisions\/1041215"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1041213"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1041195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1041195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1041195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}