|
import os
|
|
import cv2
|
|
|
|
|
|
def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
|
|
dim = None
|
|
(h, w) = image.shape[:2]
|
|
|
|
if width is None and height is None:
|
|
return image
|
|
|
|
if width is None:
|
|
r = height / float(h)
|
|
dim = (int(w * r), height)
|
|
else:
|
|
r = width / float(w)
|
|
dim = (width, int(h * r))
|
|
|
|
resized = cv2.resize(image, dim, interpolation=inter)
|
|
return resized
|
|
|
|
|
|
def convert_and_resize_images_in_folder(folder_path):
|
|
for filename in os.listdir(folder_path):
|
|
if filename.lower().endswith(('.png', '.jpeg', '.bmp', '.tiff', '.gif', '.jpg')):
|
|
file_path = os.path.join(folder_path, filename)
|
|
|
|
|
|
image = cv2.imread(file_path)
|
|
if image is None:
|
|
print(f"Error reading {file_path}")
|
|
continue
|
|
|
|
|
|
resized_image = image_resize(image, height=1080)
|
|
|
|
|
|
base, ext = os.path.splitext(filename)
|
|
output_file_path = os.path.join(folder_path, base + '.jpg')
|
|
|
|
|
|
cv2.imwrite(output_file_path, resized_image)
|
|
print(f"Converted and resized {file_path} to {output_file_path}")
|
|
|
|
if __name__ == "__main__":
|
|
folder_path = '.'
|
|
convert_and_resize_images_in_folder(folder_path)
|
|
|