Read Multiple Images on a Folder in Opencv (Python)

How to read multiple images in Python?

You can use a for loop to repeat an action.
First of all, use a dictionary or list to store the file pathways. If you only need to display them once, a list is fine.

If these images need to be reused later in the code, or something else the following doesn't accomplish, edit the question with details of what you need to do and I'll edit my answer accordingly.

On to the code. I'll refer to the list of file pathways as file_list.
for x in file_list: will loop through the list, assigning each value in it to x, executing the code in the code block following the for loop, and repeating.
Remember to indent the code block properly.

for x in file_list:
image = Image.open(x,"r")
plt.imshow(image)
plt.show()

All done!

How to read multiple images from multiple folders in python

An easy way would be to install Glob. You can do this from the anaconda prompt with pip install glob.

Glob allows you to query files like you would in a terminal. Once you have Glob you could save all of the file names to a list and then loop through this list reading your images (NumPy arrays) into a new list.

import cv2
import numpy
import glob

folders = glob.glob('path\\to\\folder\\containing\\folders\\*')
imagenames_list = []
for folder in folders:
for f in glob.glob(folder+'/*.jpg'):
imagenames_list.append(f)

read_images = []
for image in imagenames_list:
read_images.append(cv2.imread(image, cv2.IMREAD_GRAYSCALE))

You could then access an image by indexing it i.e.

plt.imshow(read_images[0])

Read multiple images from specific directory, prepossessed and save them another directory using python and opencv

Try having a list of path to all the images and iterate over them :

all_img = glob.glob('.')
other_dir = 'new_path'
for img_id, img_path in enumerate(all_img):
img = cv2.imread(img_path,0)

#create a CLAHE object (Arguments are optional).
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(20,20))
cl1 = clahe.apply(img)
cv2.imwrite(f'{other_dir}/clahe_21_{img_id}.jpg',cl1)

how to read multiple images from one folder in python using for loop?

Change "imagenames__list" to "imagenames_list"

import cv2
import numpy
import glob
import pylab as plt
folders = glob.glob('/content/drive/My Drive/Colab Notebooks/Asplab/Cifar/image31.png')
imagenames_list = []
for folder in folders:
for f in glob.glob(folder+'/*.jpg'):
imagenames_list.append(f)

read_images = []

for image in imagenames_list:
read_images.append(cv2.imread(image, cv2.IMREAD_GRAYSCALE))


Related Topics



Leave a reply



Submit