<commit_before>"""
gallerize.images
~~~~~~~~~~~~~~~~

Image processing

:Copyright: 2007-2021 <NAME>
:License: MIT, see LICENSE for details.
"""

from pathlib import Path
import shutil
import subprocess

from .logging import debug
from .models import Dimension, Gallery, Image


def generate_images(gallery: Gallery) -> None:
    """Generate images for the gallery."""
    for image in gallery.images:
        _generate_image(image, gallery)
        _generate_thumbnail(image, gallery)


def _generate_image(image: Image, gallery: Gallery) -> None:
    """Create an (optionally resized) copy of an image."""
    destination_filename = gallery.destination_path / image.filename
    if gallery.resize:
        # Resize image.
        debug('Resizing image "{}" ...', image.full_filename)
        _resize_image(
            image.full_filename,
            destination_filename,
            gallery.max_image_size,
        )
    else:
        # Copy image.
        debug('Copying image "{}" ...', image.full_filename)
        shutil.copy(image.full_filename, destination_filename)

        if gallery.optimize_images:
            _optimize_image(destination_filename)


def _generate_thumbnail(image: Image, gallery: Gallery) -> None:
    """Create a small preview image for an image."""
    debug('Creating thumbnail "{}" ...', image.thumbnail_filename)
    destination_filename = gallery.destination_path / image.thumbnail_filename
    _resize_image(
        image.full_filename,
        destination_filename,
        gallery.max_thumbnail_size,
    )

    if gallery.optimize_images:
        _optimize_image(destination_filename)


def _resize_image(
    src_filename: Path, dst_filename: Path, max_dimension: Dimension<commit_msg>Fix image optimization when not resizing<commit_after>"""
gallerize.images
~~~~~~~~~~~~~~~~

Image processing

:Copyright: 2007-2021 <NAME>
:License: MIT, see LICENSE for details.
"""

from pathlib import Path
import shutil
import subprocess

from .logging import debug
from .models import Dimension, Gallery, Image


def generate_images(gallery: Gallery) -> None:
    """Generate images for the gallery."""
    for image in gallery.images:
        _generate_image(image, gallery)
        _generate_thumbnail(image, gallery)


def _generate_image(image: Image, gallery: Gallery) -> None:
    """Create an (optionally resized) copy of an image."""
    destination_filename = gallery.destination_path / image.filename
    if gallery.resize:
        # Resize image.
        debug('Resizing image "{}" ...', image.full_filename)
        _resize_image(
            image.full_filename,
            destination_filename,
            gallery.max_image_size,
        )
    else:
        # Copy image.
        debug('Copying image "{}" ...', image.full_filename)
        shutil.copy(image.full_filename, destination_filename)

    if gallery.optimize_images:
        _optimize_image(destination_filename)


def _generate_thumbnail(image: Image, gallery: Gallery) -> None:
    """Create a small preview image for an image."""
    debug('Creating thumbnail "{}" ...', image.thumbnail_filename)
    destination_filename = gallery.destination_path / image.thumbnail_filename
    _resize_image(
        image.full_filename,
        destination_filename,
        gallery.max_thumbnail_size,
    )

    if gallery.optimize_images:
        _optimize_image(destination_filename)


def _resize_image(
    src_filename: Path, dst_filename: Path, max_dimension: Dimension
{'fraction_non_alphanumeric': 0.1028440103418558, 'fraction_numerical': 0.004596380350474002, 'mean_word_length': 3.2881773399014778, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3792994', 'n_tokens_mistral': 981, 'n_tokens_neox': 942, 'n_words': 248}