Fundamentals 6 min read

Common Image File Formats and How to Read/Write Them with OpenCV

This article introduces several common image file formats such as BMP, PNG, JPEG, TIFF, WebP, and others, explains their typical use cases, and provides Python OpenCV code snippets for reading, displaying, and saving images in each format.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Common Image File Formats and How to Read/Write Them with OpenCV

BMP - Windows Bitmap is often used in operating systems because it stores uncompressed image data, making it suitable for high‑quality applications like desktop wallpapers or icon design.

import cv2
# Read BMP file
img = cv2.imread('example.bmp')
# Display image
cv2.imshow('BMP Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Save image as BMP
cv2.imwrite('output.bmp', img)

PNG - Portable Network Graphics supports an alpha channel, making it ideal for web design, logos, and any graphics that require transparent backgrounds.

# Read PNG file (including alpha channel)
img = cv2.imread('example.png', cv2.IMREAD_UNCHANGED)
# Display image (OpenCV windows do not show transparency)
cv2.imshow('PNG Image', img[:, :, :3])  # show without alpha
cv2.waitKey(0)
cv2.destroyAllWindows()
# Save image as PNG
cv2.imwrite('output.png', img)

JPEG/JPG - Joint Photographic Experts Group is a widely used lossy compression format suitable for photographs and web images due to its high compression ratio and acceptable quality loss.

# Read JPEG file
img = cv2.imread('example.jpg')
# Display image
cv2.imshow('JPEG Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Save image as JPEG with quality parameter
cv2.imwrite('output.jpg', img, [int(cv2.IMWRITE_JPEG_QUALITY), 90])

JPE - Alternative JPEG Extension is simply another file extension for the same JPEG format; handling is identical.

# Read JPE file
img = cv2.imread('example.jpe')
# Save image as JPE
cv2.imwrite('output.jpe', img)

TIF/TIFF - Tagged Image File Format is commonly used in publishing and high‑end image editing because it can store multiple layers and supports lossless compression.

# Read TIFF file
img = cv2.imread('example.tiff')
# Save image as TIFF
cv2.imwrite('output.tiff', img)

WEBP - WebP Image Format offers better compression efficiency for both lossy and lossless use cases, making it suitable for reducing website image load times.

# Read WEBP file
img = cv2.imread('example.webp')
# Save image as WEBP
cv2.imwrite('output.webp', img)

SR - Sun Raster is an older format mainly used on Sun Microsystems operating systems and is rarely used today.

# Read SR file
img = cv2.imread('example.sun')
# Save image as SR
cv2.imwrite('output.sun', img)

PBM, PGM, PPM - Portable Bitmap/Graymap/Pixmap are simple ASCII or binary formats for black‑and‑white, grayscale, and color images respectively, often used in teaching and testing image‑processing algorithms.

# Read PPM file (similar for PBM/PGM)
img = cv2.imread('example.ppm')
# Save image as PPM
cv2.imwrite('output.ppm', img)

PFM - Portable FloatMap stores floating‑point pixel values, useful for scientific visualisation and certain image‑processing tasks; OpenCV does not natively support PFM.

EXR - OpenEXR High Dynamic Range is designed for visual effects, supporting multiple channels and high‑precision data; OpenCV may require a build with EXR support.

# Read EXR file (ensure OpenCV built with EXR support)
img = cv2.imread('example.exr', cv2.IMREAD_ANYDEPTH | cv2.IMREAD_COLOR)
# Save image as EXR
cv2.imwrite('output.exr', img)

JPEG 2000 family (JP2, JPC, J2K, J2C, PGX, PXM, PNM) provides advanced compression for archival and medical imaging, preserving quality at higher compression ratios.

# Read JPEG 2000 file (support depends on OpenCV build)
img = cv2.imread('example.jp2')
# Save image as JP2
cv2.imwrite('output.jp2', img)
PythonImage ProcessingFile I/OopencvImage Formats
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.