Skip to content

Commit

Permalink
feat: converting images to jpg format closes #39
Browse files Browse the repository at this point in the history
docs: add documentation for `jpg` command
  • Loading branch information
mdsanima committed Apr 29, 2023
1 parent 9731063 commit e51fbea
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .idea/commands.md

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

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ mdsanima

The response is printing help how to use this command-line tools.

### CLI

You can use the `mdsanima` command or the `mds` alias, which have the same functionality.

### CLI

Avaiable command for this package:

- `mdsanima` main command showing help
Expand All @@ -41,6 +41,7 @@ Avaiable command for this package:
- `mdsanima logo` append a logo
- `mdsanima watermark` append a watermark
- `mdsanima grid` generate grid 2x2
- `mdsanima jpg` convert to jpg

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 @@ -11,6 +11,7 @@

from .cli_check import cli_check
from .cli_grid import cli_grid
from .cli_jpg import cli_jpg
from .cli_logo import cli_logo
from .cli_number import cli_number
from .cli_pixelart import cli_pixelart
Expand Down Expand Up @@ -46,5 +47,7 @@ def main_cli():
cli_watermark()
if args.command == "grid":
cli_grid()
if args.command == "jpg":
cli_jpg()
except AttributeError:
parser.print_help()
76 changes: 76 additions & 0 deletions src/mdsanima_cli/cli_jpg.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 JPG 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_jpg(image_path: str, new_name: str) -> None:
"""Converting a image to JPG 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 jpg")

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


def compute_jpg() -> None:
"""Computing all images in the current directory to JPG 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 jpg 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
convert_jpg(file, new_name)
print_cli_proc("CONVERTING", count, file, new_name)
count += 1
if file.endswith(jpg) and not file.endswith(suffix + jpg):
new_name = file[:-4] + suffix + jpg
convert_jpg(file, new_name)
print_cli_proc("CONVERTING", count, file, new_name)
count += 1
if file.endswith(webp) and not file.endswith(suffix + jpg):
new_name = file[:-5] + suffix + jpg
convert_jpg(file, new_name)
print_cli_proc("CONVERTING", count, file, new_name)
count += 1


def cli_jpg() -> None:
"""Main function for `jpg` command."""
print_directory_check("JPG", "CONVERTING TO JPG")
ascii_title("processing")
compute_jpg()
13 changes: 13 additions & 0 deletions src/mdsanima_cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
GP_DESC = "Generate a grid 2x2 from all images in the current directory."
GP_HELP = "generating a grid 2x2 from all images in the current directory"

JP_PROG = "jpg"
JP_DESC = "Convert image files to JPG format in the current directory."
JP_HELP = "converting image files to JPG format in the current directory"


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

# Create subparser for jpg command.
jpg_parser = subparsers.add_parser(
JP_PROG,
description=JP_DESC,
help=JP_HELP,
epilog=AP_TOP_EPIL,
)
jpg_parser.set_defaults(command=JP_PROG)

return parser

0 comments on commit e51fbea

Please sign in to comment.