Refactoring the department structure
This commit is contained in:
39
image/crop_image_to_object_pixels.py
Normal file
39
image/crop_image_to_object_pixels.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import os
|
||||
import argparse
|
||||
from PIL import Image
|
||||
|
||||
def crop_to_object(input_folder, output_folder=None):
|
||||
# Если выходная папка не указана, используем входную
|
||||
if output_folder is None:
|
||||
output_folder = input_folder
|
||||
|
||||
# Проверяем, существует ли выходная папка, если нет, создаем ее
|
||||
if not os.path.exists(output_folder):
|
||||
os.makedirs(output_folder)
|
||||
|
||||
# Проходим по всем файлам в входной папке
|
||||
for filename in os.listdir(input_folder):
|
||||
if filename.endswith('.png'):
|
||||
img_path = os.path.join(input_folder, filename)
|
||||
img = Image.open(img_path)
|
||||
|
||||
# Получаем границы непрозрачных пикселей
|
||||
bbox = img.getbbox()
|
||||
if bbox:
|
||||
# Обрезаем изображение
|
||||
img_cropped = img.crop(bbox)
|
||||
# Сохраняем обработанное изображение
|
||||
img_cropped.save(os.path.join(output_folder, filename))
|
||||
print(f"Processed {filename}")
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Crop PNG images to the size of the visible object.")
|
||||
parser.add_argument('input_folder', type=str, help='Path to the input folder containing PNG images.')
|
||||
parser.add_argument('output_folder', type=str, nargs='?', default=None, help='Path to the output folder to save processed images. If not provided, the input folder will be used.')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
crop_to_object(args.input_folder, args.output_folder)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user