4
$\begingroup$

I have to resize some images of different size to 224x224 before they can be passed as input for VGG19, and then apply transfer learning.

I have tried these methods: add patches, take a square of 224x224 from the images' center, automatically adjust height and width.

I would like to know, if there is one, which is the best method for resizing these images.

$\endgroup$

1 Answer 1

2
$\begingroup$

This can be accomplished using the PIL library in Python.

One thing to note - if you are resizing the images to 224x224, you might want to keep the proportion of your image, e.g. if the height and width is significantly different, then you might lose perspective by resizing in this way.

If proportion is not an issue, you can resize as follows:

from PIL import Image
baseheight = 224
img = Image.open('image.jpg')
width = 224
img = img.resize((width, baseheight), Image.ANTIALIAS)
img.save('resizedimage.jpg')

That said, if you wanted to keep the same proportions as the original image and only wanted to set height to 224 for instance while keeping a proportional width, you could also use PIL as follows:

from PIL import Image
baseheight = 224
img = Image.open('image.jpg')
hpercent = (baseheight / float(img.size[1]))
width = int((float(img.size[0]) * float(hpercent)))
img = img.resize((width, baseheight), Image.ANTIALIAS)
img.save('resizedimage.jpg')
$\endgroup$
1
  • $\begingroup$ And just a reminder that if you're sending this as a single image to a model for a prediction, you'll need to add the batch dimension with np.expand_dims(yourImageArray, axis=0) $\endgroup$
    – James_SO
    Commented Aug 4, 2023 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.