🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI Freecv2.imread: Load Images with OpenCV

A Python script that works with image pixels needs to load the image first. OpenCV’s cv2.imread decodes an image file into a NumPy array for processing.
You’ll see how to check whether decoding succeeded and how read modes affect the returned array.
TL;DR
Use cv2.imread(path) to decode an image into a NumPy array, then check its shape and read mode.
- Check for None before accessing pixels.
- Choose grayscale or unchanged mode when those channels are required.
- Convert BGR to RGB for libraries that expect RGB.
What does cv2.imread return?
cv2.imread() decodes a file into a NumPy array. Color images commonly have shape (height, width, channels), while grayscale images have two dimensions.
IMREAD_COLOR requests color without alpha, IMREAD_GRAYSCALE requests grayscale, and IMREAD_UNCHANGED retains decoded channels and depth when the decoder provides them. Codec-specific grayscale conversion can differ slightly from converting a color result yourself.
How to load an image with cv2.imread step by step
Pass the path, check the result, and select a mode for the next operation. The examples use the recorded generated RGBA image.
Step 1: Import OpenCV and identify the file
The relative path starts at the process working directory, which can vary between a shell, IDE, and notebook. Install opencv-python in the Python environment used to run the script. Headless builds suit scripts that do not open desktop windows.
from pathlib import Path
import cv2
image_path = Path("sample.png")
image = cv2.imread(str(image_path))
Step 2: Check that decoding succeeded
OpenCV returns None when it cannot decode the path. Check before reading shape or indexing pixels.
if image is None:
raise FileNotFoundError(f"OpenCV could not read {image_path}")
print(image.shape)
The missing-file probe produced this warning and result:
[ WARN:[email protected]] global loadsave.cpp:279 findDecoder imread_('missing-image.png'): can't open/read file: check file path/integrity
missing read is None: True

Step 3: Choose a read mode
I ran the generated four-channel PNG in default, grayscale, and unchanged modes. The output shows the resulting shapes and one pixel:
OpenCV 5.1.0
color shape: (2, 3, 3) pixel: [20, 80, 200]
gray shape: (2, 3)
unchanged shape: (2, 3, 4) pixel: [20, 80, 200, 255]

Default color decoding produced three BGR channels, grayscale produced a two-dimensional array, and unchanged mode retained four channels.
color = cv2.imread(str(image_path), cv2.IMREAD_COLOR)
gray = cv2.imread(str(image_path), cv2.IMREAD_GRAYSCALE)
unchanged = cv2.imread(str(image_path), cv2.IMREAD_UNCHANGED)
Step 4: Convert channel order when needed
OpenCV’s color arrays use BGR order. Convert at the boundary when a plotting or image library expects RGB.
rgb = cv2.cvtColor(color, cv2.COLOR_BGR2RGB)
To save an array, use cv2.imwrite(). See also converting images to NumPy arrays.
Edge cases when reading images
Check the returned array against the input and planned operation.
| Case | Symptom | Fix |
|---|---|---|
| Wrong relative path or unreadable file | imread returns None | Resolve the path from the process directory and check the file and read access. |
| Transparent PNG loaded in color mode | Alpha is absent | Use IMREAD_UNCHANGED and inspect shape. |
| RGB consumer receives OpenCV data | Colors look swapped | Convert with COLOR_BGR2RGB. |
| High-bit-depth or categorical mask | Unexpected dtype or labels | Use unchanged mode and inspect dtype plus sample pixels before processing. |
| Different image sizes in a sequence | Arrays do not stack regularly | Compare shapes, then explicitly resize or crop to align them. |
OpenCV decodes file contents through an available codec, so changing the filename suffix does not repair damaged or unsupported data. A viewer or file-identification utility can help separate those causes.
For model input, follow the model’s required channel order, dimensions, numeric range, and batch shape. Keep analysis data separate from any resized or rescaled display copy.
Conclusion
Choose a read mode for the next operation, then inspect the decoded array before processing it. Continue with the OpenCV image codec reference or the AskPython guide to saving images with cv2.imwrite().
FAQ
These answers cover common return-value and color questions about image loading.
What does cv2.imread return?
It returns a NumPy array when decoding succeeds and None when OpenCV cannot read the image.
Does cv2.imread use BGR or RGB?
The default color result uses BGR channel order. Convert with cv2.COLOR_BGR2RGB when the next library expects RGB.
How do I read an image as grayscale?
Pass cv2.IMREAD_GRAYSCALE as the second argument to cv2.imread. The result has shape (height, width).
How do I keep transparency?
Use cv2.IMREAD_UNCHANGED and check whether the decoded array includes an alpha channel.


