Skip to content

Commit

Permalink
feat: generate grid 2x2 from all images in current dir closes #15
Browse files Browse the repository at this point in the history
docs: add documentation for `grid` command
  • Loading branch information
mdsanima committed Apr 29, 2023
1 parent 7874a69 commit 9731063
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 2 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 @@ -40,6 +40,7 @@ Avaiable command for this package:
- `mdsanima number` rename image file to number
- `mdsanima logo` append a logo
- `mdsanima watermark` append a watermark
- `mdsanima grid` generate grid 2x2

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 @@ -10,6 +10,7 @@
from .parser import create_argument_parser

from .cli_check import cli_check
from .cli_grid import cli_grid
from .cli_logo import cli_logo
from .cli_number import cli_number
from .cli_pixelart import cli_pixelart
Expand Down Expand Up @@ -43,5 +44,7 @@ def main_cli():
cli_logo()
if args.command == "watermark":
cli_watermark()
if args.command == "grid":
cli_grid()
except AttributeError:
parser.print_help()
116 changes: 116 additions & 0 deletions src/mdsanima_cli/cli_grid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Copyright © 2023 Marcin Różewski MDSANIMA


"""This module is designed to generating a grid from all images in the current
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_comp
from .mprints import print_cli_proc

from .cli_check import print_directory_check


def generate_grid(
image1_path: str,
image2_path: str,
image3_path: str,
image4_path: str,
grid_name: str,
) -> None:
"""Generating a grid 2x2 from four images, and then save the result with a
new file name. Images must be the same size. Adding exif data.
"""

# Open four images file.
image1 = Image.open(image1_path)
image2 = Image.open(image2_path)
image3 = Image.open(image3_path)
image4 = Image.open(image4_path)

# New image helper for generate grid.
grid_size = (image1.size[0] * 2, image1.size[1] * 2)
grid_image = Image.new("RGB", grid_size)

# Adding four images into grid 2x2.
grid_image.paste(image1, (0, 0))
grid_image.paste(image2, (image1.size[0], 0))
grid_image.paste(image3, (0, image1.size[1]))
grid_image.paste(image4, (image1.size[0], image1.size[1]))

# Add exif data.
exif_bytes = get_exif_bytes("with grid 2x2")

# Save the result.
grid_image.save(grid_name, exif=exif_bytes)


def compute_grid() -> None:
"""Computing grid 2x2 from all images in the current directory and save
them with a new file name.
"""

# Get directory stats info.
directory = os.listdir()
count = 1
grid = 1
img = []

# New file name prefix for generated files.
prefix = "grid_"
png = ".png"
jpg = ".jpg"
webp = ".webp"

# Checking extension and compute grid from all images in directory.
for file in directory:
if file.endswith(png):
grid_name = prefix + str(grid).zfill(5) + png
img.append(file)
print_cli_proc("APPENDING", count, file, grid_name)
if len(img) == 4:
images_clean = str(img).replace(",", "").replace("'", "")[1:-1]
print_cli_comp("COMPUTING", grid, images_clean, grid_name)
generate_grid(img[0], img[1], img[2], img[3], grid_name)
grid += 1
img = []
count += 1
if file.endswith(jpg):
grid_name = prefix + str(grid).zfill(5) + jpg
img.append(file)
print_cli_proc("APPENDING", count, file, grid_name)
if len(img) == 4:
images_clean = str(img).replace(",", "").replace("'", "")[1:-1]
print_cli_comp("COMPUTING", grid, images_clean, grid_name)
generate_grid(img[0], img[1], img[2], img[3], grid_name)
grid += 1
img = []
count += 1
if file.endswith(webp):
grid_name = prefix + str(grid).zfill(5) + webp
img.append(file)
print_cli_proc("APPENDING", count, file, grid_name)
if len(img) == 4:
images_clean = str(img).replace(",", "").replace("'", "")[1:-1]
print_cli_comp("COMPUTING", grid, images_clean, grid_name)
generate_grid(img[0], img[1], img[2], img[3], grid_name)
grid += 1
img = []
count += 1


def cli_grid() -> None:
"""Main function for `grid` command."""
print_directory_check("GRID", "COMPUTE GRID 2x2")
ascii_title("processing")
compute_grid()
15 changes: 14 additions & 1 deletion src/mdsanima_cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import argparse

from ._version import __version__
from ._version import __version__ # pylint: disable=E0401


AP_TOP_PROG = "mdsanima"
Expand Down Expand Up @@ -39,6 +39,10 @@
WP_DESC = "Append a watermark to all images in the current directory."
WP_HELP = "appending a watermark to all images in the current directory"

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"


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

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

return parser

0 comments on commit 9731063

Please sign in to comment.