Skip to content

Commit

Permalink
feat: converting images to png format closes #40
Browse files Browse the repository at this point in the history
docs: add documentation for `png` command

refactor: argument parser variable names
  • Loading branch information
mdsanima committed Apr 29, 2023
1 parent e51fbea commit 00ba0db
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 57 deletions.
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 @@ -42,6 +42,7 @@ Avaiable command for this package:
- `mdsanima watermark` append a watermark
- `mdsanima grid` generate grid 2x2
- `mdsanima jpg` convert to jpg
- `mdsanima png` convert to png

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 @@ -15,6 +15,7 @@
from .cli_logo import cli_logo
from .cli_number import cli_number
from .cli_pixelart import cli_pixelart
from .cli_png import cli_png
from .cli_uuid import cli_uuid
from .cli_watermark import cli_watermark

Expand Down Expand Up @@ -49,5 +50,7 @@ def main_cli():
cli_grid()
if args.command == "jpg":
cli_jpg()
if args.command == "png":
cli_png()
except AttributeError:
parser.print_help()
76 changes: 76 additions & 0 deletions src/mdsanima_cli/cli_png.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 PNG 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_png(image_path: str, new_name: str) -> None:
"""Converting a image to PNG 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 png")

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


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


def cli_png() -> None:
"""Main function for `png` command."""
print_directory_check("PNG", "CONVERTING TO PNG")
ascii_title("processing")
compute_png()
125 changes: 69 additions & 56 deletions src/mdsanima_cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,41 @@
AP_TOP_DESC = "MDSANIMA CLI is a command-line interface for image processing."
AP_TOP_EPIL = "Copyright \U000000A9 2023 Marcin Różewski MDSANIMA"

CP_PROG = "check"
CP_DESC = "Displaying info about all images in the current directory."
CP_HELP = "printing info about all images inside the current directory"
CHE_AP_PROG = "check"
CHE_AP_DESC = "Displaying info about all images in the current directory."
CHE_AP_HELP = "printing info about all images inside the current dir"

PP_PROG = "pixelart"
PP_DESC = "Generate pixel art from all images in the current directory."
PP_HELP = "generating pixel art from all images in the current directory"
PIX_AP_PROG = "pixelart"
PIX_AP_DESC = "Generate pixel art from all images in the current directory."
PIX_AP_HELP = "generating pixel art from all images in the current dir"

UP_PROG = "uuid"
UP_DESC = "Rename image files to UUID in the current directory."
UP_HELP = "renaming all images file to UUID in the current directory"
UUI_AP_PROG = "uuid"
UUI_AP_DESC = "Rename image files to UUID in the current directory."
UUI_AP_HELP = "renaming all images file to UUID in the current dir"

NP_PROG = "number"
NP_DESC = "Rename image files to sequential numbers in current directory."
NP_HELP = "renaming all images file to seq numbers in the current directory"
NUM_AP_PROG = "number"
NUM_AP_DESC = "Rename image files to sequential numbers in current directory."
NUM_AP_HELP = "renaming all images file to seq numbers in the current dir"

LP_PROG = "logo"
LP_DESC = "Append a logo to all images in the current directory."
LP_HELP = "appending a logo to all images in the current directory"
LOG_AP_PROG = "logo"
LOG_AP_DESC = "Append a logo to all images in the current directory."
LOG_AP_HELP = "appending a logo to all images in the current dir"

WP_PROG = "watermark"
WP_DESC = "Append a watermark to all images in the current directory."
WP_HELP = "appending a watermark to all images in the current directory"
WAT_AP_PROG = "watermark"
WAT_AP_DESC = "Append a watermark to all images in the current directory."
WAT_AP_HELP = "appending a watermark to all images in the current dir"

GP_PROG = "grid"
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"
GRI_AP_PROG = "grid"
GRI_AP_DESC = "Generate a grid 2x2 from all images in the current directory."
GRI_AP_HELP = "generating a grid 2x2 from all images in the current dir"

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"
JPG_AP_PROG = "jpg"
JPG_AP_DESC = "Convert image files to JPG format in the current directory."
JPG_AP_HELP = "converting image files to JPG format in the current dir"

PNG_AP_PROG = "png"
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"


def create_argument_parser() -> None:
Expand All @@ -70,74 +74,83 @@ def create_argument_parser() -> None:

# Create subparser for check command.
check_parser = subparsers.add_parser(
CP_PROG,
description=CP_DESC,
help=CP_HELP,
CHE_AP_PROG,
description=CHE_AP_DESC,
help=CHE_AP_HELP,
epilog=AP_TOP_EPIL,
)
check_parser.set_defaults(command=CP_PROG)
check_parser.set_defaults(command=CHE_AP_PROG)

# Create subparser for pixelart command.
pixelart_parser = subparsers.add_parser(
PP_PROG,
description=PP_DESC,
help=PP_HELP,
PIX_AP_PROG,
description=PIX_AP_DESC,
help=PIX_AP_HELP,
epilog=AP_TOP_EPIL,
)
pixelart_parser.set_defaults(command=PP_PROG)
pixelart_parser.set_defaults(command=PIX_AP_PROG)

# Create subparser for uuid command.
uuid_parser = subparsers.add_parser(
UP_PROG,
description=UP_DESC,
help=UP_HELP,
UUI_AP_PROG,
description=UUI_AP_DESC,
help=UUI_AP_HELP,
epilog=AP_TOP_EPIL,
)
uuid_parser.set_defaults(command=UP_PROG)
uuid_parser.set_defaults(command=UUI_AP_PROG)

# Create subparser for number command.
number_parser = subparsers.add_parser(
NP_PROG,
description=NP_DESC,
help=NP_HELP,
NUM_AP_PROG,
description=NUM_AP_DESC,
help=NUM_AP_HELP,
epilog=AP_TOP_EPIL,
)
number_parser.set_defaults(command=NP_PROG)
number_parser.set_defaults(command=NUM_AP_PROG)

# Create subparser for logo command.
logo_parser = subparsers.add_parser(
LP_PROG,
description=LP_DESC,
help=LP_HELP,
LOG_AP_PROG,
description=LOG_AP_DESC,
help=LOG_AP_HELP,
epilog=AP_TOP_EPIL,
)
logo_parser.set_defaults(command=LP_PROG)
logo_parser.set_defaults(command=LOG_AP_PROG)

# Create subparser for watermark command.
watermark_parser = subparsers.add_parser(
WP_PROG,
description=WP_DESC,
help=WP_HELP,
WAT_AP_PROG,
description=WAT_AP_DESC,
help=WAT_AP_HELP,
epilog=AP_TOP_EPIL,
)
watermark_parser.set_defaults(command=WP_PROG)
watermark_parser.set_defaults(command=WAT_AP_PROG)

# Create subparser for grid command.
grid_parser = subparsers.add_parser(
GP_PROG,
description=GP_DESC,
help=GP_HELP,
GRI_AP_PROG,
description=GRI_AP_DESC,
help=GRI_AP_HELP,
epilog=AP_TOP_EPIL,
)
grid_parser.set_defaults(command=GP_PROG)
grid_parser.set_defaults(command=GRI_AP_PROG)

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

# Create subparser for png command.
png_parser = subparsers.add_parser(
PNG_AP_PROG,
description=PNG_AP_DESC,
help=PNG_AP_HELP,
epilog=AP_TOP_EPIL,
)
jpg_parser.set_defaults(command=JP_PROG)
png_parser.set_defaults(command=PNG_AP_PROG)

return parser

0 comments on commit 00ba0db

Please sign in to comment.