Exemplo n.º 1
0
def generate_panel(
    panel: dict,
    colors: dict,
    session: Optional[requests.Session] = requests.Session()
) -> Image.Image:
    image = Image.new('RGB', get_size(panel))
    canvas = ImageDraw.Draw(image)
    image2 = Image.new('RGBA', (image.width * 2, image.height * 2))
    canvas2 = ImageDraw.Draw(image2)
    size = get_size(panel)
    background = ImageUtil.ratio_resize(
        ImageUtil.get_image(panel['displayAssets'][0]['background'],
                            session).convert('RGBA'), *size)
    image.paste(background, ImageUtil.center_x(background.width, image.width,
                                               0), background)
    display_asset = ImageUtil.ratio_resize(
        ImageUtil.get_image(panel['displayAssets'][0]['url'],
                            session).convert('RGBA'), *size)
    image.paste(display_asset,
                ImageUtil.center_x(display_asset.width, image.width, 0),
                display_asset)

    canvas.polygon(
        ((0, size[1] - PRICE_HEIGHT), (size[0], size[1] - PRICE_HEIGHT),
         (size[0], size[1]), (0, size[1])),
        fill=(14, 14, 14))
    vbucks = ImageUtil.ratio_resize(
        ImageUtil.open('vbucks.png').point(lambda x: x * 0.8).convert(
            'RGBA').rotate(-15), 40, 40)
    pos = size[0] - vbucks.width - 5
    image.paste(vbucks, (pos, size[1] - vbucks.height + 10), vbucks)
    text = f"{panel['price']['finalPrice']:,}"
    fonts = name_fonts.fonts_size(15, 15, 15)
    x, y = fonts.text_size(text)
    pos = pos - x - 3
    fonts.write_text(canvas,
                     text, (pos, size[1] - y - 4),
                     fill=(160, 175, 185))

    if panel['price']['finalPrice'] != panel['price']['regularPrice']:
        text = f"{panel['price']['regularPrice']:,}"
        fonts = name_fonts.fonts_size(15, 15, 15)
        x, y = fonts.text_size(text)
        pos = pos - x - 6
        fonts.write_text(canvas,
                         text, (pos, size[1] - y - 4),
                         fill=(100, 100, 100))
        canvas2.line((((pos - 2) * 2, (size[1] - y - 4 + 10) * 2),
                      ((pos + x + 3) * 2, (size[1] - y - 4 + 6) * 2)),
                     fill=(100, 110, 110),
                     width=3 * 2)

    canvas2.polygon(
        ((0, (size[1] - PRICE_HEIGHT - NAME_HEIGHT - RARITY_HEIGHT) * 2),
         (size[0] * 2,
          (size[1] - PRICE_HEIGHT - NAME_HEIGHT - RARITY_HEIGHT - SLOPE) * 2),
         (size[0] * 2, (size[1] - PRICE_HEIGHT - NAME_HEIGHT - SLOPE) * 2),
         (0, (size[1] - PRICE_HEIGHT - NAME_HEIGHT) * 2)),
        fill=colors[panel['series']['id']
                    if panel['series'] is not None else panel['rarity']['id']])
    canvas2.polygon(
        ((0, (size[1] - PRICE_HEIGHT - NAME_HEIGHT) * 2),
         (size[0] * 2, (size[1] - PRICE_HEIGHT - NAME_HEIGHT - SLOPE) * 2),
         (size[0] * 2,
          (size[1] - PRICE_HEIGHT) * 2), (0, (size[1] - PRICE_HEIGHT) * 2)),
        fill=(30, 30, 30))
    image2.thumbnail(image.size, Image.LANCZOS)
    image.paste(image2, (0, 0), image2)

    fonts = name_fonts.fonts_size(20, 20, 20)
    x, y = fonts.text_size(panel['displayName'])
    fonts.write_text(canvas,
                     panel['displayName'],
                     ImageUtil.center_x(x, image.width,
                                        size[1] - PRICE_HEIGHT - y - 10),
                     fill=(255, 255, 255))

    icons = [
        ImageUtil.ratio_resize(
            ImageUtil.open(filename).convert('RGBA'), 30, 30)
        for filename in set(
            itertools.chain(*[
                get_user_facing_flag_images(item) for item in panel['granted']
            ]))
    ]
    x = size[0] - 10
    for icon in icons:
        x -= icon.width
        image.paste(icon, (x, size[1] - PRICE_HEIGHT - NAME_HEIGHT -
                           RARITY_HEIGHT - 15 - icon.height), icon)
        x -= 10

    return image