[Python] For loop not writing images

Discussão em 'Python' iniciado por Stack, Setembro 16, 2024.

  1. Stack

    Stack Membro Participativo

    I'm attempting to write a for loop that opens each image in a folder, processes it, and then outputs it back into the same folder. The problem is, when I run the script, nothing happens. I just see the script displayed in the command window. I have tried running each part of the script outside of the loop, and the individual parts seem to be okay. For reference, I have defined the guassian noise image outside of the loop, with the loop adding the guassian noise over the original image. I then want to append a number to the end of the new image name, so the original image is not deleted.

    img_dir = "path to folder"
    data_path = os.path.join(img_dir, '*tif')
    files = glob.glob(data_path)
    data = []

    i = 0
    for f1 in files:
    img = cv2.imread(f1)
    data.append(img)
    # Add gaussian noise image to original image
    gn_img = cv2.add(img, gauss_noise)
    cv2.imwrite(
    "{}{}_{:07d}.tif".format(
    img_dir,
    f1.split("/")[-1].strip(".tif"),
    i),
    gn_img)
    i += 1

    Continue reading...

Compartilhe esta Página