feat: basic image management done

This commit is contained in:
2026-01-08 20:28:35 +01:00
parent a1f3c0638d
commit 1c642ee207
6 changed files with 78 additions and 20 deletions

View File

@@ -0,0 +1,11 @@
import argparse
def generate_argument_parser():
parser = argparse.ArgumentParser(prog="Image Processor", description="A simple image format convert and resize tool.", epilog="Use any combination of arguments.")
parser.add_argument("-H", "--height", type=int, help="max height for vertical images")
parser.add_argument("-W", "--width", type=int, help="max width for horizontal images")
parser.add_argument("-e", "--extension", type=str, default="png",help="extension of the processed files")
parser.add_argument("-d", "--directory", type=str, help="directory of files to process")
parser.add_argument("-f", "--files", nargs="+", type=list, help="list of files to process")
parser.add_argument("-o", "--output", type=str, default="./",help="directory for the output files")
return parser

View File

@@ -0,0 +1,44 @@
from PIL import Image
import os
import math
def parse_image(src: str, target_src: str, max_vertical_size_px: int, max_horizontal_size_px: int, target_extension: str):
if (src == None):
return
if (target_extension == None):
target_extension = "png"
rawImage = Image.open(src)
if (rawImage.height > rawImage.width):
if (max_vertical_size_px == None or max_vertical_size_px < 1):
max_vertical_size_px = rawImage.height
target_height = max_vertical_size_px
target_width = math.ceil(rawImage.width * max_vertical_size_px / rawImage.height)
else:
if (max_horizontal_size_px == None or max_horizontal_size_px < 1):
max_horizontal_size_px = rawImage.width
target_width = max_horizontal_size_px
target_height = math.ceil(rawImage.height * max_horizontal_size_px / rawImage.width)
rawImage = rawImage.resize((target_width, target_height))
filename = os.path.basename(src)
last_dot = filename.rfind(".")
filename = filename[:last_dot] + "." + target_extension
if (target_src == None):
target_src = os.path.abspath(os.path.dirname(src))
else:
target_src = os.path.abspath(target_src)
full_path = os.path.join(target_src, filename)
print(">> Path: ", full_path)
# rawImage.save(full_path)
def parse_whole_directory(dir_src: str, target_src: str, max_vertical_size_px: int, max_horizontal_size_px: int, target_extension: str):
for file_path in get_file_path_iter(dir_src):
parse_image(file_path,target_src, max_vertical_size_px, max_horizontal_size_px, target_extension)
def get_file_path_iter(directory):
for dirpath,_,filenames in os.walk(directory):
for f in filenames:
yield os.path.abspath(os.path.join(dirpath, f))

View File

@@ -1,17 +0,0 @@
from PIL import Image
import math
def parse_raw_image(src: str, max_size_px: int, target_extension: str):
rawImage = Image.open(src)
if (rawImage.height > rawImage.width):
target_height = max_size_px
target_width = math.ceil(rawImage.width * max_size_px / rawImage.height)
else:
target_width = max_size_px
target_height = math.ceil(rawImage.height * max_size_px / rawImage.width)
rawImage = rawImage.resize((target_width, target_height))
last_dot = src.rfind(".")
result_path = src[:last_dot]
rawImage.save(result_path + "." + target_extension)