Ejemplo n.º 1
0
def test_concatenation():
    """`vstack`, `hstack` and `concatenate` test.
    Focus on the size of image.

    Especially, focusing on the size of `images`.
    """
    image1 = Image.fromarray(
        np.random.uniform(0, 255, size=(32, 24, 4)).astype(np.uint8))
    image2 = Image.fromarray(
        np.random.uniform(0, 255, size=(22, 20, 4)).astype(np.uint8))
    ret = vstack((image1, image2))
    assert ret.mode == "RGBA"
    assert ret.size == (
        max(image1.size[0], image2.size[0]),
        image1.size[1] + image2.size[1],
    )

    ret = hstack((image1, image2))
    assert ret.mode == "RGBA"
    assert ret.size == (
        image1.size[0] + image2.size[0],
        max(image1.size[1], image2.size[1]),
    )

    # AlignMode
    assert hstack((image1, image2), align="center").mode == "RGBA"
    assert hstack((image1, image2), align="start").mode == "RGBA"
    assert hstack((image1, image2), align="end").mode == "RGBA"
Ejemplo n.º 2
0
def via_pdf(text,
            fontsize: int = 24,
            color: Color = None,
            target_dpi: int = 96):
    """

    target_dpi: The dpi which corresponds to `fontsize`. 
    """

    # Here, fontsize is used
    # To avoid the harmful effects of expansion,
    # Large DPI is used for conversion to `PNG`,
    # and shrinkage is performed.

    PNG_DPI = 960
    LATEX_FONTSIZE = 12

    path = _this_folder / "__latex__.tex"
    pdf_path = _this_folder / "__latex__.pdf"
    if path.exists():
        os.unlink(path)
    if pdf_path.exists():
        os.unlink(pdf_path)
    if color is not None:
        color = Color(color)

    documentclass = gen_documentclass(LATEX_FONTSIZE)
    preamble = gen_preamble()
    doc = gen_document(text, color=color)

    lines = [documentclass, preamble, doc]

    path.write_text("\n".join(lines))
    ret = subprocess.run(f"lualatex {path}",
                         shell=True,
                         input="",
                         cwd=path.parent)
    if ret.returncode != 0:
        raise ValueError("Failed to compile the latex.")
    assert ret.returncode == 0
    assert pdf_path.exists()
    images = convert_from_path(pdf_path, transparent=True, dpi=PNG_DPI)
    image = vstack(images)

    # Since this image seems large, so
    # `trim` may require a lot of time.
    inv_image = ImageOps.invert(image.convert("RGB"))
    image = image.crop(inv_image.getbbox())

    # The fontsize is modified.
    ratio = (fontsize * target_dpi) / (LATEX_FONTSIZE * PNG_DPI)
    image = resize(image, ratio)

    return image
Ejemplo n.º 3
0
def test_trim():
    """`fairyimage.trim`'s test.
    Here, we would like to confirm the background is eliminated.
    """
    image = Image.fromarray(
        np.random.uniform(0, 255, size=(32, 24, 4)).astype(np.uint8)
    )

    # Intentionally, append `background`.
    background = Image.fromarray(np.zeros((16, 16, 4)).astype(np.uint8))
    image = fi.vstack((image, background))        
    image = fi.hstack((image, background))
    image = fi.trim(image)
    assert image.size == (24, 32)
Ejemplo n.º 4
0
def test_equalize():
    """### Content
    * It is modified so that the length of images should be the same.

    ### Comment.
    * `AlignMode` / `resize` should be kept in mind  for `equalize`.
    """
    from PIL import Image
    from fairyimage import make_logo, equalize, vstack

    logos = [
        make_logo(f"{'O'* (index + 1)}", fontcolor=(0, 128, 189))
        for index in range(5)
    ]
    logos = equalize(logos, axis="width")
    image = vstack(logos)
    assert isinstance(image, Image.Image)
Ejemplo n.º 5
0
    def __call__(self, word_to_image: Dict[str, Image.Image]):
        # Firstly, I'd like to realize the most crude idea.

        # parameters which may require modification based on `word_to_image`.
        fontsize = self.to_fontsize(self.fontsize, word_to_image)

        word_to_logo = self.make_logos(word_to_image, fontsize)
        if self.frame_width:
            word_to_image = {
                word: fi.frame(image,
                               color=self.frame_color,
                               width=self.frame_width,
                               inner=False)
                for word, image in word_to_image.items()
            }
        images = [
            fi.vstack((word_to_logo[word], word_to_image[word]),
                      align="center") for word in word_to_logo
        ]
        return fi.hstack(images, align="start")