{"id":3790,"date":"2022-07-12T23:04:54","date_gmt":"2022-07-13T07:34:54","guid":{"rendered":"http:\/\/10ccc.cc\/?p=3790"},"modified":"2026-01-02T20:22:40","modified_gmt":"2026-01-02T12:22:40","slug":"%e7%94%9f%e6%88%90%e4%bb%8e%e5%85%85%e7%94%b5%e5%8f%a3%e6%89%8d%e8%83%bd%e7%9c%8b%e6%b8%85%e7%9a%84%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/10ccc.cc\/?p=3790","title":{"rendered":"\u751f\u6210\u4ece\u5145\u7535\u53e3\u624d\u80fd\u770b\u6e05\u7684\u56fe\u7247"},"content":{"rendered":"<h2>\u542f<\/h2>\n<blockquote><p>\u8fd9\u7bc7\u6587\u7ae0\u7684\u8d77\u70b9\u6765\u81ea\u4e8e\u5fae\u4fe1\u7fa4\u3002\u6211\u4eec\u8fd9\u4e2a\u7fa4\u91cc\u7684\u5c0f\u4f19\u4f34\u4eec\u5404\u4e2a\u90fd\u957f\u5f97\u597d\u770b\u3001\u8bf4\u8bdd\u53c8\u597d\u542c\uff0c\u800c\u4e14\u8fd8\u7279\u522b\u597d\u770b\u3002<\/p><\/blockquote>\n<figure id=\"attachment_3795\" aria-describedby=\"caption-attachment-3795\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3795\" src=\"http:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/IMG_9255-1024x1024.jpg\" alt=\"\u5fae\u4fe1\u7fa4\u804a\" width=\"500\" height=\"500\" srcset=\"https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/IMG_9255-1024x1024.jpg 1024w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/IMG_9255-300x300.jpg 300w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/IMG_9255-150x150.jpg 150w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/IMG_9255-768x767.jpg 768w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/IMG_9255.jpg 1170w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><figcaption id=\"caption-attachment-3795\" class=\"wp-caption-text\">\u5fae\u4fe1\u7fa4\u804a\uff0c\u7fa4\u6210\u5458\u53d1\u9001\u4e86\u4e00\u5f20\u626d\u66f2\u7684\u56fe\u7247<\/figcaption><\/figure>\n<p>\u6587\u5b57\u53d8\u5f62\u7684\u56fe\u7247\u5982\u4e0b\u56fe\uff0c\u5982\u679c\u6211\u4eec\u6309\u7167\u5176\u6307\u5f15\u2014\u2014\u201c\u4ece\u624b\u673a\u5145\u7535\u53e3\u659c\u7740\u770b\u201d\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8fd9\u4e9b\u9ed1\u8272\u8272\u5757\u548c\u7ebf\u6761\u4f1a\u201c\u9010\u6e10\u7ec4\u5408\u201d\u6210\u6587\u5b57\uff1a\u80af\u5fb7\u57fa\u75af\u72c2\u661f\u671f\u56db\uff0c\u7fa4\u91cc\u5bcc\u54e5V50\u3002<\/p>\n<figure id=\"attachment_3794\" aria-describedby=\"caption-attachment-3794\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3794\" src=\"http:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/IMG_9254.jpg\" alt=\"\u626d\u66f2\u6587\u5b57\u56fe\u7247\" width=\"400\" height=\"400\" srcset=\"https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/IMG_9254.jpg 1024w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/IMG_9254-300x300.jpg 300w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/IMG_9254-150x150.jpg 150w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/IMG_9254-768x768.jpg 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><figcaption id=\"caption-attachment-3794\" class=\"wp-caption-text\">\u53d8\u5f62\u7684\u6587\u5b57\u201c\u80af\u5fb7\u57fa\u75af\u72c2\u661f\u671f\u56db\uff0c\u7fa4\u91cc\u5bcc\u54e5V50\u201d<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<h2>\u5206\u6790<\/h2>\n<p>\u8fd9\u5f15\u8d77\u4e86\u6211\u7684\u5174\u8da3\u3002\u641c\u7d22\u5173\u952e\u8bcd\u201c\u4ece\u5145\u7535\u53e3\u770b\u7684\u56fe\u7247\u201d\uff0c\u4ece\u7ed3\u679c\u770b\uff0c\u8fd9\u79cd\u56fe\u7247\u5f88\u4e45\u4e4b\u524d\u5c31\u5f00\u59cb\u5728\u7f51\u7edc\u4e0a\u6d41\u884c\uff0c\u8fd1\u671f\u51fa\u73b0\u7684\u6b21\u6570\u53c8\u591a\u4e86\u8d77\u6765\u3002\u90a3\u4e48\u8fd9\u79cd\u56fe\u7247\u600e\u4e48\u5236\u4f5c\u5462\uff1f<\/p>\n<p>&nbsp;<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u5206\u6790\u4e00\u4e0b\u539f\u7406<\/p>\n<figure id=\"attachment_3798\" aria-describedby=\"caption-attachment-3798\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3798\" src=\"http:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/explainssss-462x1024.png\" alt=\"\u539f\u7406\u9610\u8ff0\" width=\"400\" height=\"886\" srcset=\"https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/explainssss-462x1024.png 462w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/explainssss-135x300.png 135w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/explainssss-768x1702.png 768w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/explainssss-693x1536.png 693w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/explainssss-924x2048.png 924w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/explainssss.png 1019w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><figcaption id=\"caption-attachment-3798\" class=\"wp-caption-text\">\u5b9e\u73b0\u539f\u7406<\/figcaption><\/figure>\n<p>Graph1 \u63cf\u8ff0\u4e86\u6211\u4eec\u4ece\u624b\u673a\u5145\u7535\u53e3\u65b9\u5411\u770b\u5411\u5c4f\u5e55\u6587\u5b57\u7684\u573a\u666f \uff0c\u5176\u4e2d\u84dd\u8272\u7ebf\u6761\u4e3a\u5c4f\u5e55\u4e2d\u7684\u6587\u5b57\uff0c\u7eff\u8272\u7ebf\u6761\u4e3a\u6587\u5b57\u5728\u6211\u4eec\u773c\u4e2d\u7684\u8c61\u3002<\/p>\n<p>\u4e3a\u4e86\u66f4\u597d\u7684\u5206\u6790\u95ee\u9898\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3aGraph2 \u4e2d\u7684\u6a21\u578b\u3002\u6ce8\u610f\uff1a\u7ea2\u8272\u7ebf\u6761\u957f\u5ea6\u76f8\u7b49\uff0c\u84dd\u8272\u7ebf\u6761\u957f\u5ea6\u4e5f\u76f8\u7b49\u3002\u540c\u6837\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5047\u8bbe\u8fd9\u4e9b\u7ebf\u6761\u662f\u5c4f\u5e55\u4e2d\u7684\u6587\u5b57\uff0c\u5176\u957f\u5ea6\u53ef\u4ee5\u7406\u89e3\u4e3a\u6587\u5b57\u5b57\u53f7\u5927\u5c0f\u3002\u00a0 \u89c2\u5bdf\u4e24\u6761\u7ea2\u8272\u7ebf\u6761\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\uff0c\u5f53\u76f8\u540c\u957f\u5ea6\u5927\u5c0f\u7684\u6587\u5b57\uff0c\u5728\u65cb\u8f6c\u4e00\u5b9a\u89d2\u5ea6\u540e\uff0c\u4f1a\u9020\u6210\u5176\u6295\u5f71\u7684\u53d8\u5c0f\uff0c\u5373\u6211\u4eec\u770b\u5230\u4f1a\u88ab\u4ece\u8f83\u5927\u7684\u5b57\u53f7\u6324\u538b\u6210\u8f83\u5c0f\u5b57\u53f7\uff0c\u6324\u538b\u65b9\u5411\u4e0e\u89c6\u7ebf\u65b9\u5411\u76f8\u540c\u3002<\/p>\n<p>\u5c4f\u5e55\u65cb\u8f6c \u6216 \u89c2\u5bdf\u8005\u4e3b\u52a8\u6539\u53d8\u89c2\u5bdf\u89d2\u5ea6 \u90fd\u4f1a\u9020\u6210 \u7684\u5b57\u4f53\u7684\u8c61\u88ab\u6324\u538b\uff0c\u6324\u538b\u7a0b\u5ea6\u4e0e\u89c2\u5bdf\u7684\u89d2\u5ea6\u03a9\u6709\u5173\uff0c\u76f8\u5173\u7cfb\u6570\u4e3asin \u03a9\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u90a3\u4e48\uff0c\u6211\u4eec\u6709\u5982\u4e0b\u903b\u8f91\u94fe\u6761:<\/p>\n<ol>\n<li>\u5c4f\u5e55\u6b63\u5e38\u5c55\u793a\u6587\u5b57\u00a0 ----&gt;\u00a0 \u4ee5\u4e00\u5b9a\u89d2\u5ea6\u67e5\u770b\u6587\u5b57 ---&gt; \u770b\u5230\u88ab\u6324\u538b\u7684\u6587\u5b57 \uff08\u6324\u538b\uff09\uff1b<\/li>\n<li>\u4ee5\u4e00\u5b9a\u89d2\u5ea6\u67e5\u770b\u6587\u5b57 ----&gt; \u770b\u5230\u6b63\u5e38\u7684\u6587\u5b57\u00a0 ----&gt;\u00a0 \u5c4f\u5e55\u62c9\u4f38\u5c55\u793a\u6587\u5b57 (\u62c9\u4f38)\uff1b<\/li>\n<\/ol>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u5982\u679c\u9700\u8981\u4ece\u5145\u7535\u53e3\u770b\u5c4f\u5e55\u770b\u5230(\u770b\u6e05)\u6b63\u5e38\u7684\u6587\u5b57\uff0c\u90a3\u4e48\u5c4f\u5e55\u5728\u5c55\u793a\u65f6\u5c31\u9700\u8981\u5c06\u6587\u5b57\u62c9\u4f38\u3002\u4e14\u6839\u636e\u4e0a\u6587\uff0c\u5982\u679c\u6211\u4eec\u6709\u8981\u6c42\u5c4f\u5e55\u6240\u5c55\u793a\u7684\u88ab\u62c9\u4f38\u8fc7\u7684\u6587\u5b57\u8981\u89c2\u5bdf\u8005\u4ee5\u7279\u5b9a\u7684\u503e\u89d2\u624d\u770b\u770b\u6e05\uff0c\u90a3\u4e48\u4e45\u9700\u8981\u5c06\u6587\u5b57\u62c9\u4f38\u4e3a\u539f\u5c3a\u5bf8\u7684 sin\u03a9 \u500d \uff08Graph 3 |\u00a0 Graph 4 \u6240\u793a\uff09\u3002<\/p>\n<p>\u5373\uff1a\u4e3a\u4e86\u8fbe\u5230\u6548\u679c\uff0c\u6211\u4eec\u9700\u8981\u505a\u5982\u4e0b\u7684\u5de5\u4f5c\uff1a<\/p>\n<ol>\n<li>\u5c06\u6587\u5b57\u62c9\u4f38\uff1b<\/li>\n<li>\u5728\u62c9\u4f38\u6587\u5b57\u7684\u540c\u65f6\uff0c\u4fdd\u6301\u6587\u5b57\u6e05\u6670\uff1b<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h2>\u5b9e\u64cd<\/h2>\n<p>\u5176\u5b9e\uff0c\u7ecf\u8fc7\u5206\u6790\uff0c\u77e5\u9053\u539f\u7406\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u7528ps\u5b8c\u6210\u8fd9\u9879\u5de5\u4f5c\u3002\u4f46\u5948\u4f55\u6211\u7528ps\u7684\u7ecf\u9a8c\u505c\u7559\u5728\u4e86CS4\u7248\u672c\u3002\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u7528\u4ee3\u7801\u6765\u5b9e\u73b0\u3002<\/p>\n<blockquote><p>\u6ce8\u610f\uff1a\u767e\u5ea6\u6216\u8005\u5fae\u4fe1\u641c\u7d22\u51fa\u6765\u7684\u6559\u7a0b\u4e5f\u90fd\u662f\u7528ps\u6765\u5b8c\u6210\uff0c\u5982\u679c\u611f\u5174\u8da3\uff0c\u53ef\u4ee5\u641c\u7d22\u76f8\u5173\u7684\u6559\u7a0b\u3002\u5bf9\u4e8e\u6b64\u95ee\u9898\uff0c\u4e2a\u4eba\u6bd4\u8f83\u63a8\u8350\u4f7f\u7528\u5fae\u4fe1\u641c\u7d22\u5173\u952e\u5b57\u201c\u4ece\u5145\u7535\u53e3\u624d\u80fd\u770b\u6e05\u7684\u56fe\u7247\u201d\u3002<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h2>\u5f00\u5de5<\/h2>\n<div class=\"dm-code-snippet dark no-background no-background-mobile dm-normal-version\" style=\"background-color:#abb8c3;\" snippet-height=\"\">\n\t\t\t<div class=\"control-language\">\n                <div class=\"dm-buttons\">\n                    <div class=\"dm-buttons-left\">\n                        <div class=\"dm-button-snippet red-button\"><\/div>\n                        <div class=\"dm-button-snippet orange-button\"><\/div>\n                        <div class=\"dm-button-snippet green-button\"><\/div>\n                    <\/div>\n                    <div class=\"dm-buttons-right\">\n                        <a id=\"dm-copy-raw-code\">\n                        <span class=\"dm-copy-text\">Copy Code<\/span>\n                        <span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span>\n                        <span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a>\n                    <\/div>\n                <\/div>\n                <pre class=\"no-line-numbers\"><code id=\"dm-code-raw\" class=\"no-wrap language-python\"><\/p>\n<pre class=\"dm-pre-admin-side\"># - * - coding:utf-8 - * -\r\n#  \u4f5c\u8005\uff1aElias Cheung\r\n#  \u7f16\u5199\u65f6\u95f4\uff1a2022\/7\/12  14:43\r\nfrom PIL import Image, ImageFont, ImageDraw\r\nfrom typing import Union, List, NewType\r\nfrom datetime import datetime\r\nfrom math import ceil, floor\r\n\r\nPicture = NewType(\"Picture\", Image)\r\n\r\n\r\ndef draw_text(text: str, font_family: str, font_size: int = 50,\r\n              text_color: Union[str, tuple] = (0, 0, 0), location: tuple = None,\r\n              bg_color: Union[str, tuple] = \"white\") -&gt; List[Picture]:\r\n    \"\"\"\r\n    :param location: location on image where the text would be written onto, if None, it will be reset to(0,2*font_size)\r\n    :param font_family: src of ttf file (win or mac), or name of built-in fonts(ios);\r\n    :param text: text that you want to draw;\r\n    :param text_color: text color, in pattern (r, g, b) or string white, green, ..., e.t.c.;\r\n    :param font_size: font size;\r\n    :param bg_color: canvas color, in pattern (r, g, b) or string white, green, ..., e.t.c.;\r\n    :return: 2 picture in a list; twisted one (1st) and just-crop one (2nd);\r\n    \"\"\"\r\n    print(f\"\u3010{datetime.now()}\u3011\u521b\u5efa\u753b\u677f\")\r\n    print(f\"\u3010{datetime.now()}\u3011\u5f00\u59cb\u7ed8\u5236\u6b63\u6587\")\r\n\r\n    font = ImageFont.truetype(font_family, font_size)\r\n    image = Image.new(mode=\"RGB\", size=(len(text) * font_size, len(text) * font_size), color=bg_color)\r\n\r\n    draw = ImageDraw.Draw(image)\r\n    location = (0, 2 * font_size) if location is None else location\r\n    draw.text(location, text, fill=text_color, font=font)\r\n\r\n    crop_image = image.crop((0, int(1.8 * font_size), len(text) * font_size, int(3.2 * font_size)))\r\n    twisted_image = crop_image.resize(size=(ceil(len(text)) * font_size, 50 * font_size))\r\n\r\n    print(f\"\u3010{datetime.now()}\u3011\u6b63\u6587\u7ed8\u5236\u5b8c\u6bd5\")\r\n    return [twisted_image, crop_image]\r\n\r\n\r\ndef refine_drew_text_image(image: Picture, font_size: int = 50,\r\n                           text_color: Union[str, tuple] = (0, 0, 0),\r\n                           bg_color: Union[str, tuple] = (255, 255, 255)) -&gt; Picture:\r\n    \"\"\"\r\n    :param text_color: text color, in pattern (r, g, b) or string white, green, ..., e.t.c.;\r\n                       MUST BE THE SAME COLOR AS THE ONE YOU USED TO DRAW TEXT WITH FUNCTION draw_text();\r\n    :param bg_color: text color, in pattern (r, g, b) or string white, green, ..., e.t.c.;\r\n                     MUST BE THE SAME COLOR AS THE ONE YOU USED WITH FUNCTION draw_text();\r\n    :param image: image to be refined, come from function draw_text();\r\n    :param font_size: MUST BE EQUAL TO THE FONT SIZE USED IN FUNCTION draw_text();\r\n    :return: refined picture\r\n    \"\"\"\r\n\r\n    print(f\"\u3010{datetime.now()}\u3011\u5f00\u59cb\u4f18\u5316\u56fe\u7247\")\r\n    #  \u6838\u5fc3\u90e8\u5206,\r\n    for w in range(image.width):\r\n        for h in range(image.height):\r\n            r_, g_, b_ = image.getpixel((w, h))\r\n            _r, _g, _b = text_color\r\n            if (r_, g_, b_, 0) != (_r, _g, _b, 0):\r\n                image.putpixel((w, h), bg_color)\r\n\r\n    print(f\"\u3010{datetime.now()}\u3011\u51c6\u5907\u8f93\u51fa\u4f18\u5316\u6548\u679c\u56fe\")\r\n    new_image = Image.new(mode=\"RGB\", size=(45 * font_size, 100 * font_size), color=bg_color)\r\n    new_image.paste(image, (18 * font_size, 0))\r\n\r\n    print(f\"\u3010{datetime.now()}\u3011\u8ba1\u7b97\u8f93\u51fa\u6548\u679c\u56fe\u8fb9\u754c\u5c3a\u5bf8\")\r\n    coordinates_y, coordinates_x = list(), list()\r\n    for w in range(new_image.width):\r\n        for h in range(new_image.height):\r\n            r_, g_, b_ = new_image.getpixel((w, h))\r\n            if (r_, g_, b_) != (255, 255, 255):\r\n                coordinates_y.append(h)\r\n                coordinates_x.append(w)\r\n\r\n    text_start_y = min(coordinates_y)\r\n    text_end_y = max(coordinates_y)\r\n    text_start_x = min(coordinates_x)\r\n    text_end_x = max(coordinates_x)\r\n\r\n    crop_box = (text_start_x - 2 * font_size,\r\n                text_start_y - 2 * font_size,\r\n                text_end_x + 2 * font_size,\r\n                text_end_y + 2 * font_size)\r\n    print(f\"\u3010{datetime.now()}\u3011\u786e\u5b9a\u65b0\u8fb9\u754c\", crop_box)\r\n\r\n    new_image = new_image.crop(crop_box)\r\n    return new_image\r\n\r\n\r\ndef concatenate_images(*images: Picture, strategy: str = \"MAX\",\r\n                       bg_color: Union[str, tuple] = (255, 255, 255)) -&gt; Picture:\r\n    \"\"\"\r\n    :param bg_color: background color used as concatenated images; better be THE SAME as used in draw_text() and\r\n                    refine_drew_text_image()\r\n    :param images: one or more image(s) to be concatenated\r\n    :param strategy: MAX or MIN, if MAX, concatenated picture's WIDTH will be set to the LARGEST width of image in image\r\n                    tuple, vice versa;\r\n    :return: concatenated picture\r\n    \"\"\"\r\n    images: tuple = images\r\n    images_width = [image.width for image in images]\r\n    images_height = [image.height for image in images]\r\n\r\n    max_width, min_width = max(images_width), min(images_width)\r\n\r\n    new_image_height = sum(images_height) + 50\r\n    new_image_width = max_width if strategy == \"MAX\" else min_width\r\n\r\n    images = tuple(map(lambda img: img.resize(size=(new_image_width, floor(new_image_width * img.height \/ img.width))),\r\n                       images))\r\n\r\n    new_image = Image.new(mode=\"RGB\", size=(new_image_width, new_image_height), color=bg_color)\r\n\r\n    current_y = 0\r\n    for image in images:\r\n        new_image.paste(image, (0, current_y))\r\n        current_y += image.height\r\n\r\n    return new_image\r\n\r\n\r\nif __name__ in \"__main__\":\r\n    lyris = \"\u751f\u6210\u4ece\u5145\u7535\u53e3\u624d\u80fd\u770b\u6e05\u7684\u56fe\u7247\"\r\n    string = \"\u95ed\u4e0a\u4e00\u53ea\u773c\u770b\u4ece\u5145\u7535\u53e3\u54e6\"\r\n    font_src = \"w6.ttf\"  # pc\/mac \u53ef\u7528ttf\u6587\u4ef6\u7684\u8def\u5f84\u66ff\u4ee3\uff0cios\u53ef\u4f7f\u7528\u7cfb\u7edf\u4e2d\u5df2\u5b89\u88c5\u7684\u5b57\u4f53\uff0c\u8f93\u5165\u540d\u79f0\u5373\u53ef\r\n\r\n    pic = draw_text(text=lyris, font_family=font_src)\r\n    pic[0].show()\r\n    print(\"\u4e0a\u56fe\u4e3a\u4e2d\u95f4\u4e34\u65f6\u4ea7\u7269\uff0c\u4f18\u5316\u4e2d\uff0c\u8bf7\u8010\u5fc3\u7b49\u5f85\u7ed3\u679c\uff5e\")\r\n\r\n    mod_pic = refine_drew_text_image(pic[0])\r\n    mod_pic.show()\r\n\r\n    hint = draw_text(text=string, font_family=font_src)\r\n\r\n    last = concatenate_images(pic[0], hint[1], strategy=\"MAX\")\r\n    last.show()\r\n    last.save(f\"{lyris}_{datetime.now()}.png\")<\/pre>\n<p><\/code><\/pre>\n\t\t\t<\/div>\n        <\/div>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<h2>\u89e3\u6790<\/h2>\n<p><strong>draw_text()<\/strong><\/p>\n<div class=\"dm-code-snippet dark no-background no-background-mobile dm-normal-version\" style=\"background-color:#abb8c3;\" snippet-height=\"\">\n\t\t\t<div class=\"control-language\">\n                <div class=\"dm-buttons\">\n                    <div class=\"dm-buttons-left\">\n                        <div class=\"dm-button-snippet red-button\"><\/div>\n                        <div class=\"dm-button-snippet orange-button\"><\/div>\n                        <div class=\"dm-button-snippet green-button\"><\/div>\n                    <\/div>\n                    <div class=\"dm-buttons-right\">\n                        <a id=\"dm-copy-raw-code\">\n                        <span class=\"dm-copy-text\">Copy Code<\/span>\n                        <span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span>\n                        <span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a>\n                    <\/div>\n                <\/div>\n                <pre class=\"no-line-numbers\"><code id=\"dm-code-raw\" class=\"no-wrap language-python\"><\/p>\n<pre class=\"dm-pre-admin-side\">def draw_text(text: str, font_family: str, font_size: int = 50,\r\n              text_color: Union[str, tuple] = (0, 0, 0), location: tuple = None,\r\n              bg_color: Union[str, tuple] = \"white\") -&gt; List[Picture]:\r\n    \"\"\"\r\n    :param location: location on image where the text would be written onto, if None, it will be reset to(0,2*font_size)\r\n    :param font_family: src of ttf file (win or mac), or name of built-in fonts(ios);\r\n    :param text: text that you want to draw;\r\n    :param text_color: text color, in pattern (r, g, b) or string white, green, ..., e.t.c.;\r\n    :param font_size: font size;\r\n    :param bg_color: canvas color, in pattern (r, g, b) or string white, green, ..., e.t.c.;\r\n    :return: 2 picture in a list; twisted one (1st) and just-crop one (2nd);\r\n    \"\"\"\r\n    print(f\"\u3010{datetime.now()}\u3011\u521b\u5efa\u753b\u677f\")\r\n    print(f\"\u3010{datetime.now()}\u3011\u5f00\u59cb\u7ed8\u5236\u6b63\u6587\")\r\n\r\n    font = ImageFont.truetype(font_family, font_size)\r\n    image = Image.new(mode=\"RGB\", size=(len(text) * font_size, len(text) * font_size), color=bg_color)\r\n\r\n    draw = ImageDraw.Draw(image)\r\n    location = (0, 2 * font_size) if location is None else location\r\n    draw.text(location, text, fill=text_color, font=font)\r\n\r\n    crop_image = image.crop((0, int(1.8 * font_size), len(text) * font_size, int(3.2 * font_size)))\r\n    twisted_image = crop_image.resize(size=(ceil(len(text)) * font_size, 50 * font_size))\r\n\r\n    print(f\"\u3010{datetime.now()}\u3011\u6b63\u6587\u7ed8\u5236\u5b8c\u6bd5\")\r\n    return [twisted_image, crop_image]\r\n<\/pre>\n<p><\/code><\/pre>\n\t\t\t<\/div>\n        <\/div>\n<hr \/>\n<p>&nbsp;<\/p>\n<p><strong>draw_text()\u00a0 <\/strong>\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u751f\u6210\u4e00\u5f20 <strong>\u521d\u6b65\u626d\u66f2\u7684\u56fe\u7247<\/strong> \u548c <strong>\u7ecf\u8fc7\u88c1\u5207\u4f46\u672a\u62c9\u4f38\u626d\u66f2\u7684\u56fe\u7247<\/strong>\u3002<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_3793\" aria-describedby=\"caption-attachment-3793\" style=\"width: 200px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3793\" src=\"http:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/tmpi6u81lzh-307x1024.png\" alt=\"\u521d\u6b65\u626d\u66f2\u62c9\u4f38\u7684\u56fe\u7247\" width=\"200\" height=\"667\" srcset=\"https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/tmpi6u81lzh-307x1024.png 307w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/tmpi6u81lzh-90x300.png 90w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/tmpi6u81lzh-461x1536.png 461w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/tmpi6u81lzh-614x2048.png 614w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/tmpi6u81lzh.png 750w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/><figcaption id=\"caption-attachment-3793\" class=\"wp-caption-text\">\u521d\u6b65\u626d\u66f2\u62c9\u4f38\u7684\u56fe\u7247\uff0c\u5df2\u7ecf\u53ef\u4ee5\u770b\u5230\u6548\u679c<\/figcaption><\/figure>\n<ol>\n<li>\u521d\u6b65\u626d\u66f2\u7684\u56fe\u7247\uff1a\u7ecf\u8fc7\u6b64\u6b65\u9aa4\u5904\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5df2\u7ecf\u53ef\u4ee5\u770b\u5230\u6548\u679c\uff0c\u4f46\u56fe\u7247\u663e\u5f97\u5f88\u810f\uff0c\u8fd8\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\uff1b<\/li>\n<li>\u7ecf\u8fc7\u88c1\u5207\u4f46\u672a\u62c9\u4f38\u626d\u66f2\u7684\u56fe\u7247: \u4e3b\u8981\u7528\u6765\u751f\u6210\u6b63\u5e38\u5c55\u793a\u7684\u63d0\u793a\u6587\u6848\uff1b<\/li>\n<\/ol>\n<hr \/>\n<p>&nbsp;<\/p>\n<p><strong>refine_drew_text_image()<\/strong><\/p>\n<p><strong>\u4f18\u5316\u4e0a\u6e38\u4ea7\u751f\u7684\u56fe\u7247<\/strong><br \/>\n<div class=\"dm-code-snippet dark no-background no-background-mobile dm-normal-version\" style=\"background-color:#abb8c3;\" snippet-height=\"\">\n\t\t\t<div class=\"control-language\">\n                <div class=\"dm-buttons\">\n                    <div class=\"dm-buttons-left\">\n                        <div class=\"dm-button-snippet red-button\"><\/div>\n                        <div class=\"dm-button-snippet orange-button\"><\/div>\n                        <div class=\"dm-button-snippet green-button\"><\/div>\n                    <\/div>\n                    <div class=\"dm-buttons-right\">\n                        <a id=\"dm-copy-raw-code\">\n                        <span class=\"dm-copy-text\">Copy Code<\/span>\n                        <span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span>\n                        <span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a>\n                    <\/div>\n                <\/div>\n                <pre class=\"no-line-numbers\"><code id=\"dm-code-raw\" class=\"no-wrap language-python\"><\/p>\n<pre class=\"dm-pre-admin-side\">def refine_drew_text_image(image: Picture, font_size: int = 50,\r\n                           text_color: Union[str, tuple] = (0, 0, 0),\r\n                           bg_color: Union[str, tuple] = (255, 255, 255)) -&gt; Picture:\r\n    \"\"\"\r\n    :param text_color: text color, in pattern (r, g, b) or string white, green, ..., e.t.c.;\r\n                       MUST BE THE SAME COLOR AS THE ONE YOU USED TO DRAW TEXT WITH FUNCTION draw_text();\r\n    :param bg_color: text color, in pattern (r, g, b) or string white, green, ..., e.t.c.;\r\n                     MUST BE THE SAME COLOR AS THE ONE YOU USED WITH FUNCTION draw_text();\r\n    :param image: image to be refined, come from function draw_text();\r\n    :param font_size: MUST BE EQUAL TO THE FONT SIZE USED IN FUNCTION draw_text();\r\n    :return: refined picture\r\n    \"\"\"\r\n\r\n    print(f\"\u3010{datetime.now()}\u3011\u5f00\u59cb\u4f18\u5316\u56fe\u7247\")\r\n    #  \u6838\u5fc3\u90e8\u5206,\r\n    for w in range(image.width):\r\n        for h in range(image.height):\r\n            r_, g_, b_ = image.getpixel((w, h))\r\n            _r, _g, _b = text_color\r\n            if (r_, g_, b_, 0) != (_r, _g, _b, 0):\r\n                image.putpixel((w, h), bg_color)\r\n\r\n    print(f\"\u3010{datetime.now()}\u3011\u51c6\u5907\u8f93\u51fa\u4f18\u5316\u6548\u679c\u56fe\")\r\n    new_image = Image.new(mode=\"RGB\", size=(45 * font_size, 100 * font_size), color=bg_color)\r\n    new_image.paste(image, (18 * font_size, 0))\r\n\r\n    print(f\"\u3010{datetime.now()}\u3011\u8ba1\u7b97\u8f93\u51fa\u6548\u679c\u56fe\u8fb9\u754c\u5c3a\u5bf8\")\r\n    coordinates_y, coordinates_x = list(), list()\r\n    for w in range(new_image.width):\r\n        for h in range(new_image.height):\r\n            r_, g_, b_ = new_image.getpixel((w, h))\r\n            if (r_, g_, b_) != (255, 255, 255):\r\n                coordinates_y.append(h)\r\n                coordinates_x.append(w)\r\n\r\n    text_start_y = min(coordinates_y)\r\n    text_end_y = max(coordinates_y)\r\n    text_start_x = min(coordinates_x)\r\n    text_end_x = max(coordinates_x)\r\n\r\n    crop_box = (text_start_x - 2 * font_size,\r\n                text_start_y - 2 * font_size,\r\n                text_end_x + 2 * font_size,\r\n                text_end_y + 2 * font_size)\r\n    print(f\"\u3010{datetime.now()}\u3011\u786e\u5b9a\u65b0\u8fb9\u754c\", crop_box)\r\n\r\n    new_image = new_image.crop(crop_box)\r\n    return new_image<\/pre>\n<p><\/code><\/pre>\n\t\t\t<\/div>\n        <\/div><\/p>\n<figure id=\"attachment_3792\" aria-describedby=\"caption-attachment-3792\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3792\" src=\"http:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/tmpwhh65iix-535x1024.png\" alt=\"\u4f18\u5316\u540e\u7684\u56fe\u7247\" width=\"300\" height=\"574\" srcset=\"https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/tmpwhh65iix-535x1024.png 535w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/tmpwhh65iix-157x300.png 157w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/tmpwhh65iix-768x1470.png 768w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/tmpwhh65iix-802x1536.png 802w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/tmpwhh65iix.png 944w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><figcaption id=\"caption-attachment-3792\" class=\"wp-caption-text\">\u4f18\u5316\u540e\u7684\u56fe\u7247<\/figcaption><\/figure>\n<p><strong>refine_drew_text_image()\u00a0<\/strong>\u7684\u4f5c\u7528\u662f\u5c06<strong>draw_text() \u751f\u6210\u7684\u56fe\u7247\u8fdb\u884c\u4f18\u5316\u3002\u5c06\u5176\u53d8\u5f97\u5e72\u51c0\u6e05\u6670\u3002<\/strong><strong>draw_text() \u751f\u6210\u7684\u56fe\u7247\u201c\u663e\u5f97\u810f\u201d\u662f\u56e0\u4e3a\u56fe\u7247\u8fdb\u884c\u62c9\u4f38\u64cd\u4f5c\u65f6\uff0c\u4f1a\u589e\u52a0\u4e0e\u5b57\u4f53\u989c\u8272\u548c\u80cc\u666f\u4e0d\u540c\u7684\u989c\u8272\u3002<\/strong><\/p>\n<p><strong>\u90a3\u4e48\uff0c\u4f18\u5316\u903b\u8f91\u5c31\u663e\u800c\u6613\u89c1\u4e86\uff0c\u5c31\u662f\u904d\u5386 draw_text() \u751f\u6210\u7684\u56fe\u7247\u7684\u6bcf\u4e2a\u50cf\u7d20\u3002\u5c06\u989c\u8272\u4e0e\u6587\u5b57\u6700\u521d\u7684\u989c\u8272\u4ee5\u53ca\u80cc\u666f\u4e0d\u540c\u7684\u50cf\u7d20\u5168\u90e8\u586b\u5145\u4e3a\u80cc\u666f\u8272\u3002\u8fd9\u6837\u53ef\u4ee5\u4f7f\u5f97\u56fe\u7247\u5728\u53d8\u6e05\u6670\u7684\u540c\u65f6\u663e\u5f97\u9510\u5229\u3002<\/strong><\/p>\n<p>\u6838\u5fc3\u4ee3\u7801<\/p>\n<div class=\"dm-code-snippet dark no-background no-background-mobile dm-normal-version\" style=\"background-color:#abb8c3;\" snippet-height=\"\">\n\t\t\t<div class=\"control-language\">\n                <div class=\"dm-buttons\">\n                    <div class=\"dm-buttons-left\">\n                        <div class=\"dm-button-snippet red-button\"><\/div>\n                        <div class=\"dm-button-snippet orange-button\"><\/div>\n                        <div class=\"dm-button-snippet green-button\"><\/div>\n                    <\/div>\n                    <div class=\"dm-buttons-right\">\n                        <a id=\"dm-copy-raw-code\">\n                        <span class=\"dm-copy-text\">Copy Code<\/span>\n                        <span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span>\n                        <span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a>\n                    <\/div>\n                <\/div>\n                <pre class=\"no-line-numbers\"><code id=\"dm-code-raw\" class=\"no-wrap language-python\"><\/p>\n<pre class=\"dm-pre-admin-side\">    for w in range(image.width):\r\n        for h in range(image.height):\r\n            r_, g_, b_ = image.getpixel((w, h))\r\n            _r, _g, _b = text_color\r\n            if (r_, g_, b_, 0) != (_r, _g, _b, 0):\r\n                image.putpixel((w, h), bg_color)<\/pre>\n<p><\/code><\/pre>\n\t\t\t<\/div>\n        <\/div>\n<p>&nbsp;<\/p>\n<hr \/>\n<p><strong>concatenate_images()<br \/>\n<\/strong>\u5c06\u53d8\u5f62\u6587\u5b57\u7684\u56fe\u7247\u548c\u63cf\u8ff0\u6587\u5b57\u7684\u56fe\u7247\u62fc\u63a5\u5728\u4e00\u8d77\u3002<\/p>\n<pre><div class=\"dm-code-snippet dark no-background no-background-mobile dm-normal-version\" style=\"background-color:#abb8c3;\" snippet-height=\"\">\n\t\t\t<div class=\"control-language\">\n                <div class=\"dm-buttons\">\n                    <div class=\"dm-buttons-left\">\n                        <div class=\"dm-button-snippet red-button\"><\/div>\n                        <div class=\"dm-button-snippet orange-button\"><\/div>\n                        <div class=\"dm-button-snippet green-button\"><\/div>\n                    <\/div>\n                    <div class=\"dm-buttons-right\">\n                        <a id=\"dm-copy-raw-code\">\n                        <span class=\"dm-copy-text\">Copy Code<\/span>\n                        <span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span>\n                        <span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a>\n                    <\/div>\n                <\/div>\n                <pre class=\"no-line-numbers\"><code id=\"dm-code-raw\" class=\"no-wrap language-python\"><\/pre>\n<pre class=\"dm-pre-admin-side\">def concatenate_images(*images: Picture, strategy: str = \"MAX\",\r\n                       bg_color: Union[str, tuple] = (255, 255, 255)) -&gt; Picture:\r\n    \"\"\"\r\n    :param bg_color: background color used as concatenated images; better be THE SAME as used in draw_text() and\r\n                    refine_drew_text_image()\r\n    :param images: one or more image(s) to be concatenated\r\n    :param strategy: MAX or MIN, if MAX, concatenated picture's WIDTH will be set to the LARGEST width of image in image\r\n                    tuple, vice versa;\r\n    :return: concatenated picture\r\n    \"\"\"\r\n    images: tuple = images\r\n    images_width = [image.width for image in images]\r\n    images_height = [image.height for image in images]\r\n\r\n    max_width, min_width = max(images_width), min(images_width)\r\n\r\n    new_image_height = sum(images_height) + 50\r\n    new_image_width = max_width if strategy == \"MAX\" else min_width\r\n\r\n    images = tuple(map(lambda img: img.resize(size=(new_image_width, floor(new_image_width * img.height \/ img.width))),\r\n                       images))\r\n\r\n    new_image = Image.new(mode=\"RGB\", size=(new_image_width, new_image_height), color=bg_color)\r\n\r\n    current_y = 0\r\n    for image in images:\r\n        new_image.paste(image, (0, current_y))\r\n        current_y += image.height\r\n\r\n    return new_image<\/pre>\n<pre><\/code><\/pre>\n\t\t\t<\/div>\n        <\/div>\r\n\r\n---\r\n\u4e3a\u4e86\u62d3\u5c55\u6027\uff0c\u6211\u5c06\u5176\u5199\u6210\u4e86\u53ef\u5c06\u4e00\u5f20\u6216\u591a\u5f20\u56fe\u7247\u62fc\u63a5\u6210\u4e00\u5f20\u65b0\u56fe\u7247\u3002<\/pre>\n<h2>\u6587\u4ef6\u4e0b\u8f7d<\/h2>\n<p><a href=\"http:\/\/10ccc.cc\/wp-content\/uploads\/2022\/07\/draw_twisted_text.py\" target=\"_blank\"  rel=\"nofollow\" >draw_twisted_text<\/a><\/p>\n<pre><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u542f \u8fd9\u7bc7\u6587\u7ae0\u7684\u8d77\u70b9\u6765\u81ea\u4e8e\u5fae\u4fe1\u7fa4\u3002\u6211\u4eec\u8fd9\u4e2a\u7fa4\u91cc\u7684\u5c0f\u4f19\u4f34\u4eec\u5404\u4e2a\u90fd\u957f\u5f97\u597d\u770b\u3001\u8bf4\u8bdd\u53c8\u597d\u542c\uff0c\u800c\u4e14\u8fd8\u7279\u522b\u597d\u770b\u3002 \u6587\u5b57\u53d8\u5f62\u7684\u56fe\u7247\u5982\u4e0b\u56fe\uff0c\u5982\u679c\u6211\u4eec &#8230;<\/p>\n","protected":false},"author":1,"featured_media":3799,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[123,84,82],"tags":[86,89],"class_list":["post-3790","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-funny","category-82","tag-86","tag-89"],"_links":{"self":[{"href":"https:\/\/10ccc.cc\/index.php?rest_route=\/wp\/v2\/posts\/3790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/10ccc.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/10ccc.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/10ccc.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/10ccc.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3790"}],"version-history":[{"count":5,"href":"https:\/\/10ccc.cc\/index.php?rest_route=\/wp\/v2\/posts\/3790\/revisions"}],"predecessor-version":[{"id":4511,"href":"https:\/\/10ccc.cc\/index.php?rest_route=\/wp\/v2\/posts\/3790\/revisions\/4511"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/10ccc.cc\/index.php?rest_route=\/wp\/v2\/media\/3799"}],"wp:attachment":[{"href":"https:\/\/10ccc.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/10ccc.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/10ccc.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}