-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add documentation for `jpg` command
- Loading branch information
Showing
5 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters