Reputation: 33
I have written some code in Python for resizing and converting images to grayscale from a folder and want to save them in another folder. When I compile the code, it shows no output and gives no error. I am sharing the whole code that I have written so far so I can find my error. This is my first question here.
from PIL import Image # to load images
from IPython.display import display # to display images
import cv2
import os
import glob
import numpy as np
img_path = r"C:\Users\usama\Documents\FYP-Data\FYP Project Data\hamza\h1.png"
im= Image.open(img_path)
print('{}'.format(im.format))
print('Size: {}'.format(im.size))
print('image mode:{}'.format(im.mode))
im.show()
img_list = []
resized_list = []
for filename in glob.glob(r'C:\Users\usama\Documents\FYP-Data\FYP Project Data\hamza\*.png'):
print(filename)
img=Image.open(filename)
img_list.append(img)
img_list=img_list[:2000]
for image in resized_list:
if image not in resized_list:
resized_list.append(img)
resized_list=(resized_list[:2000])
img=image.resize((500,500))
rl=cv2.imread('resized_list')
gray_image = cv2.cvtColor(rl, cv2.COLOR_BGR2GRAY)
cv2.imwrite('resized images *.png', gray_image)
Upvotes: 0
Views: 1588
Reputation: 545
Try the following:
import cv2
import glob
for filename in glob.glob(r'your\path\*.png'):
print(filename)
img=cv2.imread(filename)
rl=cv2.resize(img, (500,500))
gray_image = cv2.cvtColor(rl, cv2.COLOR_BGR2GRAY)
cv2.imwrite(f'{filename}.resized.png', gray_image)
Upvotes: 1