Beispiel #1
0
def main():
    if len(sys.argv) < 3:
        raise RuntimeError(
            'Command Line Argument Must be (sketch file, style file)')

    style_f = './data/styles/%s' % sys.argv[2]
    test_f = './data/test/%s' % sys.argv[1]

    filename = sys.argv[1][:-4] + sys.argv[2][:-4] + '.png'

    style = Image.open(style_f).convert('RGB')
    style = transforms.Resize((512, 512))(style)
    style_pil = style

    test = Image.open(test_f).convert('RGB')
    test_pil = transforms.Resize((512, 512))(test)

    transform = transforms.Compose(
        [transforms.Resize((512, 512)),
         transforms.ToTensor()])

    test = transform(test)
    test = scale(test)
    test = test.unsqueeze(0).to(device)

    to_pil = transforms.ToPILImage()

    try:
        images = list(crop_region(style))
        result = {}
        for i, img in enumerate(images, 1):
            colors = cgm.extract(img, topk + 1)
            result[str(i)] = {
                '%d' % i: get_rgb(colors[i])
                for i in range(1, topk + 1)
            }

        color_tensor = make_colorgram_tensor(result)
        color_tensor = color_tensor.unsqueeze(0).to(device)

        fakeB, _ = model(test, color_tensor)
        fakeB = fakeB.squeeze(0)
        fakeB = re_scale(fakeB.detach().cpu())
        fakeB = to_pil(fakeB)

        result_image = Image.new('RGB', (512 * 3, 512))
        result_image.paste(test_pil, (512 * 0, 0, 512 * 1, 512))
        result_image.paste(style_pil, (512 * 1, 0, 512 * 2, 512))
        result_image.paste(fakeB, (512 * 2, 0, 512 * 3, 512))
        save_image(result_image, os.path.join(out_root, filename))

    except IndexError:
        exit(1)
Beispiel #2
0
def extract_color_histogram(image, topk=4):
    """
    get image
    extract top-k colors except background color
    return (1, 3 * k, image.shape) tensor
    """
    width, height = image.size
    colors = colorgram.extract(image, topk + 1)
    tensor = torch.ones([topk * 3, width, height])
    for i, color in enumerate(colors[1:]):
        red = i * 3
        green = i * 3 + 1
        blue = i * 3 + 2
        tensor[red] *= color.rgb.r
        tensor[green] *= color.rgb.g
        tensor[blue] *= color.rgb.b

    return scale(tensor / 255.)
Beispiel #3
0
    def get_image_with_palette(infile: str,
                               outline_width: float,
                               palette_length_div: float,
                               outline_color,
                               num_colors: int = 12):
        colors = colorgram.extract(infile, 12)
        original_image: Image = Image.open(infile)
        width, height = original_image.size
        palette_height: int = int(height / palette_length_div)
        final_image: Image = Image.new(original_image.mode,
                                       (width, height + palette_height))
        palette: Image = Image.new(original_image.mode,
                                   (width, palette_height))
        draw_api: ImageDraw = ImageDraw.Draw(palette)
        pos_x: float = 0
        pos_x2: float = 0
        swatch_size: float = width / num_colors
        swatch_size2: int = 100

        for col in colors:
            draw_api.rectangle([pos_x, 0, pos_x + swatch_size, palette_height],
                               fill=col.rgb,
                               width=outline_width,
                               outline=outline_color)
            pos_x = pos_x + swatch_size
            pos_x2 = pos_x2 + swatch_size2

        del draw_api
        box: Tuple[int, float, float,
                   float] = (0, height, width, height + palette_height)

        final_image.paste(original_image)
        final_image.paste(palette, box)

        file_name = f"{str(uuid.uuid4())}.jpg"
        file_path = os.path.join(os.path.dirname(__file__), file_name)
        final_image.save(file_path, 'jpeg', quality=100, optimize=True)
        return file_path