0

I'm using COCO data-set. in that data-set images have different length and width but I have to crop them to equal length and width.

All the images are placed in a folder. I have tried to crop them into equal length and width i.e. 386X386

import sys
import os
from PIL import Image

filepath = "/content/drive/My Drive/Colab Notebooks/SRGAN/data"

# Loop through all provided arguments
for filename in os.listdir(filepath):
    if "." not in filename:
        continue
    ending = filename.split(".")[1]
    if ending not in ["jpg", "gif", "png"]:
        continue


    # Attempt to open an image file
    image = Image.open(os.path.join(filepath, filename))


    # Perform operations on the image here
    image = image.crop((0, 0, 386, 386))

    # Split our origional filename into name and extension 
    name, extension = os.path.splitext(filename)

    # Save the image as "(origional_name)_thumb.jpg
    print(name + '_cropped.jpg')
    image.save(os.path.join("/content/drive/My Drive/Colab Notebooks/SRGAN/data", name + '_cropped.jpg'))

It should crop all images to size 386X386, but it is generating multiple copies of same images with different length and width.

star123
  • 323
  • 2
  • 3
  • 9
  • Possible duplicate of [Crop entire image with the same cropping size with PIL in python](https://stackoverflow.com/questions/53501331/crop-entire-image-with-the-same-cropping-size-with-pil-in-python) – shaik moeed May 28 '19 at 12:33

1 Answers1

0
from keras.preprocessing import image
test_img = image.load_img('Image Path', target_size=(386,386))

'target size' defines the dimensions to crop to.

Trollsors
  • 492
  • 4
  • 17