Exemplo n.º 1
0
def test_optimize_image_allow_convert(png_image, tmp_path):
    shutil.copy(png_image, tmp_path)
    src = tmp_path / png_image.name
    dst = tmp_path / "out.webp"
    optimize_image(src, dst, delete_src=True, convert=True)
    assert not src.exists()
    assert dst.exists() and os.path.getsize(dst) > 0
Exemplo n.º 2
0
def test_preset(
    preset,
    expected_version,
    options,
    fmt,
    png_image,
    jpg_image,
    gif_image,
    webp_image,
    tmp_path,
):
    assert preset.VERSION == expected_version
    assert preset.options == options
    src, dst = get_src_dst(
        tmp_path,
        fmt,
        png_image=png_image,
        jpg_image=jpg_image,
        gif_image=gif_image,
        webp_image=webp_image,
    )
    optimize_image(src, dst, delete_src=False, **preset.options)
    assert os.path.getsize(dst) < os.path.getsize(src)

    if fmt in ["jpg", "webp", "png"]:
        image_bytes = ""
        with open(src, "rb") as fl:
            image_bytes = fl.read()
        byte_stream = io.BytesIO(image_bytes)
        dst_bytes = get_optimization_method(fmt)(src=byte_stream,
                                                 **preset.options)
        assert dst_bytes.getbuffer().nbytes < byte_stream.getbuffer().nbytes
Exemplo n.º 3
0
def test_optimize_image_del_src(png_image, tmp_path):
    shutil.copy(png_image, tmp_path)
    src = tmp_path / png_image.name
    dst = tmp_path / "out.png"
    org_size = os.path.getsize(src)
    optimize_image(src, dst, delete_src=True)
    assert os.path.getsize(dst) < org_size
    assert not src.exists()
Exemplo n.º 4
0
def test_optimize_image_default(png_image, jpg_image, gif_image, webp_image,
                                tmp_path, fmt):
    src, dst = get_src_dst(
        tmp_path,
        fmt,
        png_image=png_image,
        jpg_image=jpg_image,
        gif_image=gif_image,
        webp_image=webp_image,
    )
    optimize_image(src, dst, delete_src=False)
    assert os.path.getsize(dst) < os.path.getsize(src)
Exemplo n.º 5
0
    def download_jpeg_image_and_convert(
        self, url, fpath, preset_options={}, resize=None
    ):
        """downloads a JPEG image and converts and optimizes it into desired format detected from fpath"""

        org_jpeg_path = pathlib.Path(
            tempfile.NamedTemporaryFile(delete=False, suffix=".jpg").name
        )
        save_large_file(url, org_jpeg_path)
        if resize is not None:
            resize_image(
                org_jpeg_path,
                width=resize[0],
                height=resize[1],
                method="cover",
            )
        optimize_image(
            org_jpeg_path, fpath, convert=True, delete_src=True, **preset_options
        )
        logger.debug(f"Converted {org_jpeg_path} to {fpath} and optimized ")
Exemplo n.º 6
0
def process_thumbnail(thumbnail_path, preset):
    # thumbnail might be WebP as .webp, JPEG as .jpg or WebP as .jpg
    tmp_thumbnail = thumbnail_path
    if not thumbnail_path.exists():
        logger.debug("We don't have video.webp, thumbnail is .jpg")
        tmp_thumbnail = thumbnail_path.with_suffix(".jpg")

    # resize thumbnail. we use max width:248x187px in listing
    # but our posters are 480x270px
    resize_image(
        tmp_thumbnail,
        width=480,
        height=270,
        method="cover",
        allow_upscaling=True,
    )
    optimize_image(tmp_thumbnail,
                   thumbnail_path,
                   delete_src=True,
                   **preset.options)
    return True
Exemplo n.º 7
0
def test_preset(
    preset,
    expected_version,
    options,
    fmt,
    png_image,
    jpg_image,
    gif_image,
    webp_image,
    tmp_path,
):
    assert preset.VERSION == expected_version
    assert preset.options == options
    src, dst = get_src_dst(
        tmp_path,
        fmt,
        png_image=png_image,
        jpg_image=jpg_image,
        gif_image=gif_image,
        webp_image=webp_image,
    )
    optimize_image(src, dst, delete_src=False, **preset.options)
    assert os.path.getsize(dst) < os.path.getsize(src)