コード例 #1
0
ファイル: views.py プロジェクト: BeanInJ/DjangoDemo
    def get(self, request):
        color_bg = (get_color(), get_color(), get_color())
        image = Image.new(mode='RGB', size=(100, 50), color=color_bg)
        imagedraw = ImageDraw(image, mode='RGB')
        the_code = get_txt()
        upper_code = the_code.upper()
        request.session["the_code"] = upper_code
        font = ImageFont.truetype("C:\Windows\Fonts\simsunb.ttf", 50)
        for i in range(4):
            fill = (get_color(), get_color(), get_color())
            imagedraw.text(xy=(25 * i, 0),
                           text=the_code[i],
                           fill=fill,
                           font=font)
        for i in range(1000):
            fill = (get_color(), get_color(), get_color())
            xy = (random.randrange(201), random.randrange(100))
            imagedraw.point(xy=xy, fill=fill)

        imagedraw.arc(
            (random.randrange(0, 10), 6, 100, random.randrange(70, 100)),
            random.randrange(0, 180),
            random.randrange(250, 360),
            fill=get_color())

        fp = BytesIO()
        image.save(fp, "png")

        return HttpResponse(fp.getvalue(), content_type="image/png")
コード例 #2
0
def getcode(request):
    mode = 'RGB'
    size = [200, 100]
    color_bg = getcolor()
    image = Image.new(mode=mode, size=size, color=color_bg)
    imagedraw = ImageDraw(image, mode=mode)

    iamgefont = ImageFont.truetype(settings.FONT_PATH, 30)
    verify_code = 'Come'
    for i in range(len(verify_code)):
        # file text填充色
        fill = getcolor()
        imagedraw.text(xy=(30 * i, 30), text=verify_code[i], font=iamgefont, fill=fill)
    # 干扰点
    for i in range(1000):
        xy = (random.randrange(size[0]), random.randrange(size[1]))
        fill = getcolor()
        imagedraw.point(xy=xy, fill=fill)
    # 画圆 xy区域,四元数组  start int end int 圆经过strat和end
    start = random.randrange(size[0])
    end = random.randrange(size[1])
    fill = getcolor()
    imagedraw.arc(xy=(0, 0, 200, 100), start=start, end=end, fill=fill)
    for i in range(10):
        imagedraw.line(xy=(
        random.randrange(size[0]), random.randrange(size[1]), (random.randrange(size[0]), random.randrange(size[1]))),
                       fill=getcolor())
    fp = BytesIO()

    image.save(fp, 'png')
    cache.set('code', verify_code)

    return HttpResponse(fp.getvalue(), content_type='image/png')
コード例 #3
0
def draw_arc(draw: ImageDraw.ImageDraw, target, offset=(0, 0), scale=1.0):
    # check
    # calc
    x0 = (target['center']['x'] - target['radius']) * scale + offset[0]
    x1 = (target['center']['x'] + target['radius']) * scale + offset[0]
    y0 = (target['center']['y'] - target['radius']) * scale + offset[1]
    y1 = (target['center']['y'] + target['radius']) * scale + offset[1]
    start = target['range']['start']
    end = target['range']['end']
    # draw
    draw.arc([(int(x0), int(y0)), (int(x1), int(y1))], start, end)
コード例 #4
0
def draw_rounded_rectangle(draw: ImageDraw, xy, corner_radius, fill=None, outline=None, width=None):
    rad = corner_radius
    upper_left = xy[0]
    bottom_right = xy[1]
    draw.rectangle(
        [
            (upper_left[0], upper_left[1] + rad),
            (bottom_right[0], bottom_right[1] - rad)
        ],
        fill=fill,
    )
    draw.rectangle(
        [
            (upper_left[0] + rad, upper_left[1]),
            (bottom_right[0] - rad, bottom_right[1])
        ],
        fill=fill,
    )
    draw.pieslice([upper_left, (upper_left[0] + rad * 2, upper_left[1] + rad * 2)],
        180, 270, fill=fill
    )
    draw.pieslice([(bottom_right[0] - rad * 2, bottom_right[1] - rad * 2), bottom_right],
        0, 90, fill=fill
    )
    draw.pieslice([(upper_left[0], bottom_right[1] - rad * 2), (upper_left[0] + rad * 2, bottom_right[1])],
        90, 180, fill=fill
    )
    draw.pieslice([(bottom_right[0] - rad * 2, upper_left[1]), (bottom_right[0], upper_left[1] + rad * 2)],
        270, 360, fill=fill
    )

    border_delta = width/2 - 1
    if width > 0:
        draw.line([(upper_left[0] + rad, upper_left[1] + border_delta), (bottom_right[0] - rad, upper_left[1] + border_delta)],
                  fill=outline, width=width)
        draw.line([(bottom_right[0] - border_delta, upper_left[1] + rad), (bottom_right[0] - border_delta, bottom_right[1] - rad)],
                  fill=outline, width=width)
        draw.line([(bottom_right[0] - rad, bottom_right[1] - border_delta), (upper_left[0] + rad, bottom_right[1] - border_delta)],
                  fill=outline, width=width)
        draw.line([(upper_left[0] + border_delta, bottom_right[1] - rad), (upper_left[0] + border_delta, upper_left[1] + rad)],
                  fill=outline, width=width)

        draw.arc([upper_left, (upper_left[0] + rad * 2, upper_left[1] + rad * 2)],
                 180, 270, fill=outline, width=width)
        draw.arc([(bottom_right[0] - rad * 2, upper_left[1]), (bottom_right[0], upper_left[1] + rad * 2)],
                 270, 360, fill=outline, width=width)
        draw.arc([(bottom_right[0] - rad * 2, bottom_right[1] - rad * 2), bottom_right],
                 0, 90, fill=outline, width=width)
        draw.arc([(upper_left[0], bottom_right[1] - rad * 2), (upper_left[0] + rad * 2, bottom_right[1])],
                 90, 180, fill=outline, width=width)