{"id":3545,"date":"2023-07-25T08:46:16","date_gmt":"2023-07-25T00:46:16","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=3545"},"modified":"2023-07-25T08:46:18","modified_gmt":"2023-07-25T00:46:18","slug":"%e7%94%bb%e5%9b%be%e3%80%81%e5%86%99%e5%ad%97%ef%bc%88%e6%b0%b4%e5%8d%b0%ef%bc%89draw-multicolor-rectangular-horizontal-and-vertical-gradients","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=3545","title":{"rendered":"\u753b\u56fe\u3001\u5199\u5b57\uff08\u6c34\u5370\uff09draw multicolor rectangular horizontal and vertical gradients"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>from PIL import Image, ImageDraw, ImageFont\r\n\r\nBLACK, DARKGRAY, GRAY = ((0,0,0), (63,63,63), (127,127,127))\r\nLIGHTGRAY, WHITE = ((191,191,191), (255,255,255))\r\nBLUE, GREEN, RED = ((0, 0, 255), (0, 255, 0), (255, 0, 0))\r\n\r\n\r\nclass Point(object):\r\n    def __init__(self, x, y):\r\n        self.x, self.y = x, y\r\n\r\nclass Rect(object):\r\n    def __init__(self, x1, y1, x2, y2):\r\n        minx, maxx = (x1,x2) if x1 &lt; x2 else (x2,x1)\r\n        miny, maxy = (y1,y2) if y1 &lt; y2 else (y2,y1)\r\n        self.min = Point(minx, miny)\r\n        self.max = Point(maxx, maxy)\r\n\r\n    width  = property(lambda self: self.max.x - self.min.x)\r\n    height = property(lambda self: self.max.y - self.min.y)\r\n\r\n\r\ndef gradient_color(minval, maxval, val, color_palette):\r\n    \"\"\" Computes intermediate RGB color of a value in the range of minval\r\n        to maxval (inclusive) based on a color_palette representing the range.\r\n    \"\"\"\r\n    max_index = len(color_palette)-1\r\n    delta = maxval - minval\r\n    if delta == 0:\r\n        delta = 1\r\n    v = float(val-minval) \/ delta * max_index\r\n    i1, i2 = int(v), min(int(v)+1, max_index)\r\n    (r1, g1, b1), (r2, g2, b2) = color_palette&#91;i1], color_palette&#91;i2]\r\n    f = v - i1\r\n    return int(r1 + f*(r2-r1)), int(g1 + f*(g2-g1)), int(b1 + f*(b2-b1))\r\n\r\ndef horz_gradient(draw, rect, color_func, color_palette):\r\n    minval, maxval = 1, len(color_palette)\r\n    delta = maxval - minval\r\n    width = float(rect.width)  # Cache.\r\n    for x in range(rect.min.x, rect.max.x+1):\r\n        f = (x - rect.min.x) \/ width\r\n        val = minval + f * delta\r\n        color = color_func(minval, maxval, val, color_palette)\r\n        draw.line(&#91;(x, rect.min.y), (x, rect.max.y)], fill=color)\r\n\r\ndef vert_gradient(draw, rect, color_func, color_palette):\r\n    minval, maxval = 1, len(color_palette)\r\n    delta = maxval - minval\r\n    height = float(rect.height)  # Cache.\r\n    for y in range(rect.min.y, rect.max.y+1):\r\n        f = (y - rect.min.y) \/ height\r\n        val = minval + f * delta\r\n        color = color_func(minval, maxval, val, color_palette)\r\n        draw.line(&#91;(rect.min.x, y), (rect.max.x, y)], fill=color)\r\n\r\n\r\nif __name__ == '__main__':\r\n    # Draw a three color vertical gradient.\r\n    color_palette = &#91;BLUE, GREEN, RED]\r\n    image_w = 300\r\n    image_h = 200\r\n    region = Rect(0, 0, image_w, image_h)\r\n    imgx, imgy = region.max.x + 1, region.max.y + 1\r\n    image = Image.new(\"RGB\", (imgx, imgy), WHITE)\r\n    draw = ImageDraw.Draw(image)\r\n    vert_gradient(draw, region, gradient_color, color_palette)\r\n    # image.text((40, 80),\"No Artwork\",(255,255,255))\r\n    font = ImageFont.truetype(r'C:\\Users\\System-Pc\\Desktop\\arial.ttf', 40)\r\n    # font = ImageFont.load_default()\r\n    #font = ImageFont.truetype(\"Ubuntu-R.ttf\", int(float(image_w) \/ 6))\r\n    draw.text((int(image_w \/ 12), int(image_h \/ 2.5)), \"No Artwork\", \\\r\n              fill=(0, 0, 0), font=font)\r\n    image.show()\r\n    # image.save(\"vert_gradient.png\", \"PNG\")\r\n    # print('image saved')<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,10],"tags":[],"class_list":["post-3545","post","type-post","status-publish","format-standard","hentry","category-2","category-python"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3545"}],"version-history":[{"count":1,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3545\/revisions"}],"predecessor-version":[{"id":3546,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3545\/revisions\/3546"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3545"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}