Topic: Python – Reading Images from Datasets and Organizing Them
---
1. Reading Images from Folder Structure
Assuming your dataset folder looks like this:
You can use Python libraries like os, OpenCV, or PIL to read and organize images by their classes.
---
2. Code Example Using OpenCV
---
3. Optional: Resize Images for Uniformity
Use this inside the loop before appending
---
4. Using PIL (Pillow) Instead of OpenCV
---
5. Organizing Images in a Dictionary
---
6. Summary
• Use os.listdir() to iterate dataset directories.
• Read images with cv2.imread() or PIL.Image.open().
• Resize images to a uniform shape for model input.
• Store images and labels in lists or dictionaries for easy access.
---
Exercise
• Extend the code to save the loaded images and labels as numpy arrays for faster loading in the future.
---
#Python #ImageProcessing #DatasetHandling #OpenCV #PIL
https://t.iss.one/DataScience4
---
1. Reading Images from Folder Structure
Assuming your dataset folder looks like this:
dataset/
class1/
img1.jpg
img2.jpg
class2/
img3.jpg
img4.jpg
You can use Python libraries like os, OpenCV, or PIL to read and organize images by their classes.
---
2. Code Example Using OpenCV
import os
import cv2
dataset_path = "dataset"
data = []
labels = []
for class_name in os.listdir(dataset_path):
class_dir = os.path.join(dataset_path, class_name)
if os.path.isdir(class_dir):
for img_name in os.listdir(class_dir):
img_path = os.path.join(class_dir, img_name)
img = cv2.imread(img_path)
if img is not None:
data.append(img)
labels.append(class_name)
print(f"Total images: {len(data)}")
print(f"Total labels: {len(labels)}")
---
3. Optional: Resize Images for Uniformity
target_size = (128, 128)
resized_img = cv2.resize(img, target_size)
Use this inside the loop before appending
img to data.---
4. Using PIL (Pillow) Instead of OpenCV
from PIL import Image
img = Image.open(img_path)
img = img.resize((128, 128))
img_array = np.array(img)
---
5. Organizing Images in a Dictionary
dataset_dict = {}
for class_name in os.listdir(dataset_path):
class_dir = os.path.join(dataset_path, class_name)
if os.path.isdir(class_dir):
dataset_dict[class_name] = []
for img_name in os.listdir(class_dir):
img_path = os.path.join(class_dir, img_name)
img = cv2.imread(img_path)
if img is not None:
dataset_dict[class_name].append(img)---
6. Summary
• Use os.listdir() to iterate dataset directories.
• Read images with cv2.imread() or PIL.Image.open().
• Resize images to a uniform shape for model input.
• Store images and labels in lists or dictionaries for easy access.
---
Exercise
• Extend the code to save the loaded images and labels as numpy arrays for faster loading in the future.
---
#Python #ImageProcessing #DatasetHandling #OpenCV #PIL
https://t.iss.one/DataScience4
❤4