Beginner level · Module 2: Digital Image Representation · Lesson 3 of 3
Colour Spaces, File Formats and Efficient Storage
Learners convert between BGR, RGB and HSV, choose file formats, and reduce image size with resizing and compression so that datasets stay small enough for modest computers and slow connections.
In this lesson you will
- Convert between colour spaces and explain why HSV helps
- Reduce image storage size with resizing and compression
Key concepts
BGR vs RGBHSV (hue, saturation, value)JPEG and PNGcompression qualityresizing with INTER_AREA
The lesson
OpenCV stores colour channels in BGR order, while Matplotlib and Pillow expect RGB. Forgetting cv2.cvtColor(img, cv2.COLOR_BGR2RGB) before plotting makes ripe tomatoes look blue. HSV separates colour (hue) from brightness (value), so selecting 'yellow' works better in HSV than in RGB when lighting changes.
JPEG uses lossy compression and suits photographs; PNG is lossless and suits diagrams, masks and screenshots. cv2.imwrite('out.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 85]) usually gives a much smaller file with little visible loss.
Phone photos are often 12 megapixels or more. Most learning tasks need far less: resizing to 640 pixels on the longer side with cv2.resize(..., interpolation=cv2.INTER_AREA) can cut a dataset from gigabytes to tens of megabytes, which makes it practical to share on USB and train on a laptop.
Step by step
- Display an image with and without BGR-to-RGB conversion.
- Convert to HSV and select the yellow pixels with cv2.inRange.
- Resize a phone photo to 640 pixels on its longer side.
- Save it at JPEG quality 95, 85 and 70 and compare file sizes and appearance.
Practice activity
Write a small script that resizes and compresses a folder of photos, and report the space saved.
Expected result: A working script and a table of original versus compressed sizes with a recommended quality setting.
Check your understanding
-
Why do OpenCV images look wrong in Matplotlib without conversion?
Show answer
OpenCV uses BGR channel order and Matplotlib expects RGB.
-
When should you use PNG rather than JPEG?
Show answer
For diagrams, masks and screenshots that need lossless storage.
Further learning
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.