I used a dataset of images for machine learning training. Each image had a width of 64px and a height of 64px as well. Now, I want to test my machine learning model using images from google. The problem is that google images are larger than training images, and I want to resize them so that their height and width are 64px (just like the images in the training set). Is there any way to do this in python? I did find some methods, but all of them maintain aspect ratio. So, I am unable to achieve 64 by 64 size.
            Asked
            
        
        
            Active
            
        
            Viewed 7,507 times
        
    0
            
            
         
    
    
        Mahsa Seifikar
        
- 271
- 1
- 3
- 19
 
    
    
        Saad Farooq
        
- 39
- 1
- 8
- 
                    1What library do you plan on using for Machine Learning? Pytorch has a very nice function: `torchvision.transforms.CenterCrop()` which should work well. – ccl Feb 27 '20 at 18:50
- 
                    I am making a logistic regression model from scratch. – Saad Farooq Feb 27 '20 at 18:52
- 
                    Not sure what you are looking for here. Do you need to resize the whole image to given size, or you want to used a cropped section.? – shaivikochar Feb 27 '20 at 18:54
- 
                    resize the whole image to a given size. – Saad Farooq Feb 27 '20 at 19:01
- 
                    It seems that by default PIL doesn't maintain the aspect ratio, so I'm not sure what the issue is. – AMC Feb 27 '20 at 19:02
- 
                    Yes. I found the function. – Saad Farooq Feb 27 '20 at 19:02
- 
                    from PIL import Image image = Image.open('./dataset/image.jpeg') image= image.resize((64,64)) – Saad Farooq Feb 27 '20 at 19:02
- 
                    Does this answer your question? [Does Python PIL resize maintain the aspect ratio?](https://stackoverflow.com/questions/2232742/does-python-pil-resize-maintain-the-aspect-ratio) – AMC Feb 27 '20 at 19:07
- 
                    Also: https://stackoverflow.com/questions/9983263/how-to-crop-an-image-using-pil – AMC Feb 27 '20 at 19:07
2 Answers
1
            
            
        I found the function in PIL that resizes the image and does not maintain the aspect ratio.
from PIL import Image
image = Image.open('./dataset/image.jpeg')
image= image.resize((64,64))
 
    
    
        shaivikochar
        
- 400
- 2
- 7
 
    
    
        Saad Farooq
        
- 39
- 1
- 8
0
            
            
        You can use python-resize-image.
Install package:
pip install python-resize-image
Example:
from PIL import Image
from resizeimage import resizeimage
#open image file
with open('image.jpg', 'r+b') as fd_img:
    # create a PIL Image from file
    img = Image.open(fd_img)
    # resize image (contain)
    img = resizeimage.resize_contain(img, [64, 64])
    # covert to RBA incase it's RGBA
    img = img.convert("RGB")
    # save image
    img.save('resized-image.jpg', img.format)
 
    
    
        Thaer A
        
- 2,243
- 1
- 10
- 14