{"id":287,"date":"2021-08-11T08:33:00","date_gmt":"2021-08-11T08:33:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=287"},"modified":"2021-08-11T08:33:00","modified_gmt":"2021-08-11T08:33:00","slug":"287","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/08\/11\/287\/","title":{"rendered":"Pillow \u5c4f\u5e55\u622a\u56fe\u548c\u6587\u5b57"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/1037695853.jpg\" alt=\"test_web_high.jpg\" title=\"test_web_high.jpg\"><\/p>\n<h2>Pillow \u56fe\u50cf\u5e93\u6559\u7a0b - \u4f7f\u7528 ImageGrab \u6a21\u5757<\/h2>\n<h3>ImageGrab \u6a21\u5757\uff08\u4ec5\u9650 OS X \u548c Windows\uff09<\/h3>\n<ul>\n<li>\u8be5 <code>ImageGrab<\/code> \u6a21\u5757\u53ef\u7528\u4e8e\u5c06\u5c4f\u5e55\u6216\u526a\u8d34\u677f\u7684\u5185\u5bb9\u590d\u5236\u5230 PIL \u56fe\u50cf\u5b58\u50a8\u5668\u3002<\/li>\n<li><code>PIL.ImageGrab.grab()<\/code> \u62cd\u6444\u5c4f\u5e55\u5feb\u7167\u3002\u8fb9\u754c\u6846\u5185\u7684\u50cf\u7d20\u5728 Windows \u4e0a\u4f5c\u4e3a\u201cRGB\u201d\u56fe\u50cf\u6216\u5728 OS X \u4e0a\u4f5c\u4e3a\u201cRGBA\u201d\u8fd4\u56de\u3002\u5982\u679c\u7701\u7565\u8fb9\u754c\u6846\uff0c\u5219\u590d\u5236\u6574\u4e2a\u5c4f\u5e55\u3002<\/li>\n<li><code>PIL.ImageGrab.grabclipboard()<\/code> \u62cd\u6444\u526a\u8d34\u677f\u56fe\u50cf\u7684\u5feb\u7167\uff08\u5982\u679c\u6709\uff09\u3002<\/li>\n<\/ul>\n<h3>\u793a\u4f8b\u4ee3\u78011  \u5c4f\u5e55\u5c45\u4e2d \u9009\u62e9640x480\u622a\u56fe<\/h3>\n<pre><code class=\"lang-python\">from PIL import Image, ImageGrab\n\n#  \u5c4f\u5e55\u5c45\u4e2d\u9009\u62e9 x,y \u8303\u56f4\ndef center_box(x, y):\n    im = ImageGrab.grab()            # \u5148\u6574\u4e2a\u5c4f\u5e55\u622a\u56fe\uff0c\u83b7\u53d6\u5c4f\u5e55\u5927\u5c0f\n    center_point = (im.width \/\/ 2, im.height \/\/ 2)     # \u8ba1\u7b97\u5c4f\u5e55\u4e2d\u5fc3\u70b9\u5750\u6807\n    origin = center_point[0] - x \/\/ 2, center_point[1] - y \/\/ 2    # \u9009\u62e9\u8303\u56f4 box \u5706\u70b9\n    box = (origin[0], origin[1], origin[0] + x, origin[1] + y)\n    return box\n\nbox = center_box(640, 480)      # \u5c4f\u5e55\u5c45\u4e2d \u9009\u62e9 640x480\nimg = ImageGrab.grab(box)       # \u5c4f\u5e55\u90e8\u5206 \u622a\u56fe\nprint(box)\nimg.save('box_640x480.png')\nimg.show()\n\n<\/code><\/pre>\n<h3>\u793a\u4f8b\u4ee3\u78012  \u5c4f\u5e55\u5c45\u622a\u56fe\uff0c\u586b\u5145\u6587\u5b57<\/h3>\n<pre><code>from PIL import Image, ImageDraw, ImageFont, ImageGrab\nimport bitfont     # bitfont\u5e93  https:\/\/github.com\/hongwenjun\/pillow_font\/\n\n# im = Image.new(\"RGB\", (400,200),color=(255,0,0))      # \u65b0\u5efa\u56fe\u7247\n# im = bitfont.load_background()                        # \u52a0\u8f7d\u81ea\u5b9a\u4e49bitfont\u5e93\u4e2d\u80cc\u666f\n# im = im.resize((800,200))                             # \u8bbe\u7f6e\u56fe\u7247\u5927\u5c0f\nim = ImageGrab.grab((0,0,800,500))                      # \u62cd\u6444\u5c4f\u5e55\u5feb\u7167\u3002(\u7a7a\u53c2\u6570)\u9ed8\u8ba4\u5168\u5c4f\uff0c\u53c2\u6570 box \u526a\u5207\u90e8\u5206\n# im = ImageGrab.grabclipboard()                        # \u62cd\u6444\u526a\u8d34\u677f\u56fe\u50cf\u7684\u5feb\u7167\uff08\u5982\u679c\u6709\uff09\u3002\ndraw = ImageDraw.Draw(im)\n\nfont = ImageFont.truetype(\"DroidSansFallback.ttf\", 26)\nfont_v = bitfont.load_vcrmono()\ndraw.text((5, 10), bitfont.text[1], font=font)\ndraw.text((5, 40), bitfont.text[1], (255,128,128), font=font_v)\n\ndraw.text((5, 100), \"\u5b89\u5353\u9ed1\u4f53 DroidSansFallback.ttf\n\u4e2d\u6587\u5b57\u4f53\u6d4b\u8bd5\n PIL \u4f7f\u7528\u81ea\u5df1\u7684\u5b57\u4f53\u6587\u4ef6\u683c\u5f0f\u6765\u5b58\u50a8\u4f4d\u56fe\u5b57\u4f53\uff0c\n\u9650\u5236\u4e3a 256 \u4e2a\u5b57\u7b26\u3002\n \u4ece\u7248\u672c 1.1.4 \u5f00\u59cb\uff0cPIL \u53ef\u4ee5\u914d\u7f6e\u4e3a\u652f\u6301 TrueType\n\u548c OpenType \u5b57\u4f53\", (255,0,255), font=font)\n\nfont = ImageFont.truetype(\"DroidSansFallback.ttf\", 72)\ndraw.text((50, 300), \"Pillow\u5c4f\u5e55\u622a\u56fe\u548c\u6587\u5b57\", (255,255,0), font=font)\n\n# im.save('test.png')\n# im.save('test.j2k')\nim.save(\"test_web_high.jpg\", quality=\"web_high\")\n# JPEG \u8d28\u91cf\u8bbe\u7f6e\u7b49\u540c\u4e8e Photoshop \u8bbe\u7f6e  web_low\u3001web_medium\u3001web_high\u3001web_very_high\u3001web_maximum\u3001 low\u3001medium\u3001high\u3001maximum\n\nim.show()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Pillow \u56fe\u50cf\u5e93\u6559\u7a0b &#8211; \u4f7f\u7528 ImageGrab \u6a21\u5757 ImageGrab \u6a21\u5757\uff08\u4ec5\u9650 OS  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[45],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-learn","tag-python"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=287"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/287\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}