Apply resize on multiple images at one time in openCV python

Question:

I have read a folder containing pictures using glob and imread. Now my I want to resize all of those pictures using for loop in cv2.resize.

following is my code but the output is not correct–

import cv2
import glob



path = glob.glob("C:/Users/RX-91-9/Desktop/prescriptions/*.jpg")
for file in (path):
img=cv2.imread(file)
cv2.imshow("Image", img)
cv2.cv2.waitKey(3)
cv2.destroyAllWindows()

for i in img:
resized_image = cv2.resize(i, (1600,1600)) 
cv2.imshow('resized_image', resized_image)
cv2.waitKey(3)
cv2.destroyAllWindows()

I don’t know why the last for loop is not giving the expected output, i want all the images in ‘img’ to be resized. Please help if you find what is wrong in my for last for loop.

Asked By: guati dibba

||

Answers:

I assume that you have a list of images in some folder and you to resize all of them. You can run

import cv2
import glob

for filename in glob.glob('images/*.jpg'): # path to your images folder
    print(filename)
    img=cv2.imread(filename) 
    rl=cv2.resize(img, (500,500))
    cv2.imwrite(f'{filename}resized.jpg', rl)

Answered By: Prabhat Kumar Sahu

You can also use this here, hope to fix your issue 😀

import PIL
import os
from PIL import Image

path= r'your image file path'
for image in os.listdir(path):
    path_img = path+"/"+image
    img = Image.open(apth_img)
    img = img.resize((width,height))
Answered By: Python Lover