Uncategorized

Python processes images, but overrides existing image and creates new images


I am trying to resize images and then store the images as img_resized.jpg

I am able to resize original files and save the resized images as img_resized.jpg, but when I run the program again: The original images in said folder stays the same, and the program resizes/changes the img_resized.jpg and also adds another img_resized_resized.jpg.

The extra img_resized_resized.jpg is acceptable atm since I have not made an if statement excluding existing files named _resiazed.jpg.

This is the code I’ve written so far. I expected the code to resize all images in said folder, and create new img_resized.jpg of all images in folder. But it alters some of the already resized images in folder. It does not change the originals.
How can i prevent the script from changing the img_resized.jpg files in folder while creating new files?

”’python

    import cv2
    import glob
    import os


    def img_reshape(img_file):
        image = cv2.imread(str(img_file), 1)
        resized_image = cv2.resize(image, (int(image.shape[1]/2), int(image.shape[0]/2)))
        file_name = os.path.splitext(os.path.basename(img_file))[0]
        output_path = f"files\\sample_images\\{file_name}_resized.jpg"
        return cv2.imwrite(output_path, img=resized_image)


    def main():

        for files in glob.glob("files\\sample_images\\*.jpg"):
    img_reshape(files)

    main()

”’



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *