Skip to content

Commit

Permalink
feat: generate jpeg thumbnail 128px closes #17
Browse files Browse the repository at this point in the history
docs: add documentation for `thumbnail` command
  • Loading branch information
mdsanima committed May 2, 2023
1 parent 1242234 commit 8daccc9
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .idea/commands.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Avaiable command for this package:
- `mdsanima webp` convert to webp
- `mdsanima pixelart` generate pixel art 32px
- `mdsanima grid` generate grid 2x2
- `mdsanima thumbnail` generate jpeg thumbnail 128px

The `pixelart` command works in folder that have only `.png` images and convert this images to pixel
art with creating the new file and appending the suffix `pixelart` to original file name.
Expand Down
3 changes: 3 additions & 0 deletions src/mdsanima_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from mdsanima_cli.command.number import cli_number
from mdsanima_cli.command.pixelart import cli_pixelart
from mdsanima_cli.command.png import cli_png
from mdsanima_cli.command.thumbnail import cli_thumbnail
from mdsanima_cli.command.uuids import cli_uuid
from mdsanima_cli.command.watermark import cli_watermark
from mdsanima_cli.command.webp import cli_webp
Expand Down Expand Up @@ -51,5 +52,7 @@ def main_cli():
cli_pixelart()
if args.command == "grid":
cli_grid()
if args.command == "thumbnail":
cli_thumbnail()
except AttributeError:
parser.print_help()
83 changes: 83 additions & 0 deletions src/mdsanima_cli/command/thumbnail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright © 2023 Marcin Różewski MDSANIMA


"""Generating JPEG thumbnails from all images in the curreny directory. It operates within
a specified folder and can process all images at once.
"""


from __future__ import annotations

import os

from PIL import Image

from mdsanima_cli.command.check import directory_statistic
from mdsanima_cli.parser import THUMBNAIL_COMMAND
from mdsanima_cli.parser import THUMBNAIL_HELP
from mdsanima_cli.utils.ascii import ascii_title
from mdsanima_cli.utils.exif import get_exif_bytes
from mdsanima_cli.utils.print import print_cli_proc


def generate_jpeg_thumbnail(image_path: str, new_name: str) -> None:
"""Generate JPEG thumbnail 128, and then save the result with a new name and exif data."""

# Open image file.
image = Image.open(image_path)

# Image size.
image_width, image_height = image.size

# Calculating size for thumbnail.
size = 128
resize_ratio = int(image_width / size)
thumbnail_width = int(image_width / resize_ratio)
thumbnail_height = int(image_height / resize_ratio)
thumbnail_size = (thumbnail_width, thumbnail_height)

# Add exif data.
exif_bytes = get_exif_bytes(THUMBNAIL_HELP)

# Create JPEG thumbnail and save the result.
image.thumbnail(thumbnail_size)
image.save(new_name, "JPEG", exif=exif_bytes)


def compute_thumbnail() -> None:
"""Generating JPEG thumbnails 128 from all images in the current directory."""

# Get directory stats info.
directory = os.listdir()
count = 1

# New file name suffix for generated files.
suffix = "_thumbnail"
png = ".png"
jpg = ".jpg"
webp = ".webp"

# Checking extension and compute thumbnail from all images in directory.
for file in directory:
if file.endswith(png) and not file.endswith(suffix + jpg):
new_name = file[:-4] + suffix + jpg
generate_jpeg_thumbnail(file, new_name)
print_cli_proc("computing", count, file, new_name)
count += 1
if file.endswith(jpg) and not file.endswith(suffix + jpg):
new_name = file[:-4] + suffix + jpg
generate_jpeg_thumbnail(file, new_name)
print_cli_proc("computing", count, file, new_name)
count += 1
if file.endswith(webp) and not file.endswith(suffix + jpg):
new_name = file[:-5] + suffix + jpg
generate_jpeg_thumbnail(file, new_name)
print_cli_proc("computing", count, file, new_name)
count += 1


def cli_thumbnail() -> None:
"""Main function for `thumbnail` command."""
directory_statistic(THUMBNAIL_COMMAND, THUMBNAIL_HELP)
ascii_title("processing")
compute_thumbnail()
13 changes: 13 additions & 0 deletions src/mdsanima_cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
GRID_DESCRIPTION = f"Generate grid 2x2 from {ALL_IMAGES}"
GRID_HELP = "generate grid 2x2"

THUMBNAIL_COMMAND = "thumbnail"
THUMBNAIL_DESCRIPTION = f"Generate JPEG thumbnails 128px from {ALL_IMAGES}"
THUMBNAIL_HELP = "generate jpeg thumbnail 128px"


def create_parser() -> None:
"""This function creates an argument parser for all available functions in this package, which
Expand Down Expand Up @@ -169,4 +173,13 @@ def create_parser() -> None:
)
grid_parser.set_defaults(command=GRID_COMMAND)

# Create subparser for thumbnail command.
thumbnail_parser = subparser.add_parser(
name=THUMBNAIL_COMMAND,
description=THUMBNAIL_DESCRIPTION,
help=THUMBNAIL_HELP,
epilog=EPILOG,
)
thumbnail_parser.set_defaults(command=THUMBNAIL_COMMAND)

return parser

0 comments on commit 8daccc9

Please sign in to comment.