Skip to content

Commit

Permalink
feat: converting images to webp format closes #13
Browse files Browse the repository at this point in the history
docs: add documentation for `webp` command
  • Loading branch information
mdsanima committed Apr 29, 2023
1 parent 00ba0db commit fd1f60d
Show file tree
Hide file tree
Showing 5 changed files with 94 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 @@ -43,6 +43,7 @@ Avaiable command for this package:
- `mdsanima grid` generate grid 2x2
- `mdsanima jpg` convert to jpg
- `mdsanima png` convert to png
- `mdsanima webp` convert to webp

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 @@ -18,6 +18,7 @@
from .cli_png import cli_png
from .cli_uuid import cli_uuid
from .cli_watermark import cli_watermark
from .cli_webp import cli_webp


def main_cli():
Expand Down Expand Up @@ -52,5 +53,7 @@ def main_cli():
cli_jpg()
if args.command == "png":
cli_png()
if args.command == "webp":
cli_webp()
except AttributeError:
parser.print_help()
76 changes: 76 additions & 0 deletions src/mdsanima_cli/cli_webp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright © 2023 Marcin Różewski MDSANIMA


"""This module is designed to converting images to WebP format 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 .ascii import ascii_title
from .exif import get_exif_bytes
from .mprints import print_cli_proc

from .cli_check import print_directory_check


def convert_webp(image_path: str, new_name: str) -> None:
"""Converting a image to WebP format, and then save the result with a new
file name. Adding exif data.
"""

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

# Add exif data.
exif_bytes = get_exif_bytes("with webp")

# Save the result.
image.save(new_name, exif=exif_bytes)


def compute_webp() -> None:
"""Computing all images in the current directory to WebP format and save
them with a new file name.
"""

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

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

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


def cli_webp() -> None:
"""Main function for `webp` command."""
print_directory_check("WEBP", "CONVERTING TO WEBP")
ascii_title("processing")
compute_webp()
13 changes: 13 additions & 0 deletions src/mdsanima_cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
PNG_AP_DESC = "Convert image files to PNG format in the current directory."
PNG_AP_HELP = "converting image files to PNG format in the current dir"

WEB_AP_PROG = "webp"
WEB_AP_DESC = "Convert image files to WEBP format in the current directory."
WEB_AP_HELP = "converting image files to WEBP format in the current dir"


def create_argument_parser() -> None:
"""This function creates an argument parser for all available functions in
Expand Down Expand Up @@ -153,4 +157,13 @@ def create_argument_parser() -> None:
)
png_parser.set_defaults(command=PNG_AP_PROG)

# Create subparser for webp command.
webp_parser = subparsers.add_parser(
WEB_AP_PROG,
description=WEB_AP_DESC,
help=WEB_AP_HELP,
epilog=AP_TOP_EPIL,
)
webp_parser.set_defaults(command=WEB_AP_PROG)

return parser

0 comments on commit fd1f60d

Please sign in to comment.