Python image IO module with binding cxx image code
Project description
CXX Image IO
CXX Image IO is a Python project which provides the image IO interfaces, binding with the C++ library: https://github.com/emmcb/cxx-image, These IO interfaces are designed to read and write images in many file formats in generic way and to interact nicely with numpy array.
Image format | Read | Write | EXIF | Pixel precision | Pixel type | File extension |
---|---|---|---|---|---|---|
BMP | x | x | 8 bits | Grayscale, RGB, RGBA | .bmp | |
CFA | x | x | 16 bits | Bayer | .cfa | |
DNG | x | x | x | 16 bits, float | Bayer, RGB | .dng |
JPEG | x | x | x | 8 bits | Grayscale, RGB | .jpg, .jpeg |
MIPIRAW | x | x | 10 bits, 12 bits | Bayer | .RAWMIPI, .RAWMIPI10, .RAWMIPI12 | |
PLAIN | x | x | * | * | .plain16, .nv12, * | |
PNG | x | x | 8 bits, 16 bits | Grayscale, RGB, RGBA | .png | |
TIFF | x | x | x | 8 bits, 16 bits, float | Bayer, RGB | .tif, .tiff |
Getting Started
Prerequisites
This projet currently supports Python 3.12, 3.13 on Windows and Linux.
The user need to install python 3.12 or 3.13.
numpy >= 1.26.4 is necessary.
Installation
The python package cxx_image_io
is to be installed by pip
pip install cxx_image_io
Usage example
Image reading
read_image
is able to read a image file and return a numpy array and ImageMetadata object.
from cxx_image_io import read_image
from cxx_image_io import ImageMetadata
import numpy as np
from pathlib import Path
image, metadata = read_image(Path('/path/to/image.jpg'))
assert isinstance(image, np.ndarray)
print('Type:', image.dtype)
print('Shape:', image.shape)
image is a numpy array which is suitable for the image processing afterwards.
The result could be like this:
Type: uint8
Shape: (551, 603, 3)
ImageMetadata is the information about the image, including the pixel type, pixel precision and image layout, which define fundamentally how the pixels arranged in buffer.
print(metadata.fileInfo)
The result could be like this:
{'pixelPrecision': 8, 'imageLayout': 'interleaved', 'pixelType': 'rgb'}
Some file formats need to know in advance some informations about the image. For example, the PLAIN format is just a simple dump of a buffer into a file, thus it needs to know how to interpret the data.
image, metadata = read_image(Path('/path/to/image.plain16'))
In this case, user need to have an image sidecar JSON located next to the image file as the same name and path '/path/to/image.json'
{
"fileInfo": {
"format": "plain",
"height": 3072,
"width": 4080
"pixelPrecision": 16,
"pixelType": "bayer_gbrg",
}
}
After image reading, the information in JSON sidecar is parsed in ImageMetadata object.
The result of print(metadata.fileInfo)
could be like this:
{'width': 4080, 'height': 3072, 'pixelPrecision': 16, 'imageLayout': 'planar', 'pixelType': 'bayer_gbrg'}
Image sidecar is not mandatory, for the other formats which have already image information in their header, like jpg, png, tif, cfa. we don't need to provide image metadata.
Split and merge image channels
After calling read_image
, cxx-image-io
provides a public API split_image_channels
which helps to split to different colors channels, so that user can do the different processes on them. The function return type is a dictionary which contains the different color channel name as keys, and the value in numpy array of one single channel.
before calling write_image
, cxx-image-io
provides a public API merge_image_channels
which helps to merge different colors channels to a numpy array buffer.
from cxx_image_io import read_image, split_image_channels, merge_image_channels, ImageLayout, ImageMetadata, PixelRepresentation, PixelType
import numpy as np
from pathlib import Path
rgb, metadata = read_image(Path('rgb_8bit.jpg'))
channels = split_image_channels(rgb, metadata)
# print(channels['r']) # Red channel
# print(channels['g']) # Green channel
# print(channels['b']) # Blue channel
rgb_post = merge_image_channels(channels, metadata)
np.array_equal(rgb, rgb_post)
cfa, metadata = read_image(Path('bayer_16bit.plain16'))
channels = split_image_channels(cfa, metadata)
# print(channels['gr']) # Bayer Gr pixels
# print(channels['r']) # Bayer R pixels
# print(channels['b']) # Bayer B pixels
# print(channels['gb']) # Bayer Gb pixels
cfa_post = merge_image_channels(channels, metadata)
np.array_equal(cfa, cfa_post)
yuv, metadata = read_image(Path('raw.nv12'))
channels = split_image_channels(yuv, metadata)
# print(channels['y']) # Y plane
# print(channels['u']) # U plane
# print(channels['v']) # V plane
yuv_post = merge_image_channels(channels, metadata)
np.array_equal(yuv, yuv_post)
Image writing
write_image
is able to write a numpy array to image file.
To write the pure numpy array to different image file extensions. User need to define the following fundamental parameters in ImageMetadata which is part of ImageWriter.Options. In order to call the specific C++ image libraries with them.
from cxx_image_io import ImageMetadata, ImageWriter, FileFormat, PixelType, ImageLayout
from cxx_image_io import write_image
import numpy as np
from pathlib import Path
metadata = ImageMetadata()
metadata.fileInfo.pixelType = PixelType.RGB
metadata.fileInfo.imageLayout = ImageLayout.INTERLEAVED
write_options = ImageWriter.Options(metadata)
assert isinstance(image, np.ndarray)
write_image(Path('/path/to/image.jpg'), image, write_options)
write_image
can determine the image format by file extensions, but some formats don't not rely on a specific extension, for example the PLAIN format that allows to directly dump the image buffer to a file. In this case, the format can be specified through ImageWriter.Options.
write_options = ImageWriter.Options(metadata)
write_options.fileFormat = FileFormat.PLAIN
assert isinstance(image, np.ndarray)
write_image(Path('/path/to/image.plain16'), image, write_options)
EXIF
Some image formats, like JPEG and TIFF, support EXIF reading and writing.
If supported, EXIF can be read by calling read_exif
and be written by calling write_exif
.
from cxx_image_io import read_exif, write_exif
from pathlib import Path
exif = read_exif(Path('/path/to/image.jpg'))
print(exif)
write_exif(Path('path/to/new_image.jpg'), exif)
print(exif)
will give the following output like:
{'make': 'Canon', 'model': 'Canon EOS 40D', 'orientation': 1, 'software': 'GIMP 2.4.5', 'exposureTime': [1, 160], 'fNumber': [71, 10], 'isoSpeedRatings': 100, 'dateTimeOriginal': '2008:05:30 15:56:01', 'exposureBiasValue': [0, 1], 'focalLength': [135, 1]}
user can use help(exif)
to see the definition of Exif metdata.
EXIF metadata can be read and written along with an image by specifying them in the ImageMetadata. In this case, the EXIF wil be read and written when calling read_image
and write_image
.
image, metadata = read_image(Path('/path/to/image.jpg'))
metadata.exifMetadata.make = 'Custom'
write_options = ImageWriter.Options(metadata)
write_image(Path('/path/to/image.jpg'), image, write_options)
License
This project is licensed under the MIT License - see the LICENSE.md file for details
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for cxx_image_io-0.0.16-cp313-cp313-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98856f111c4a7c0cbad795da15f25db66fbcba81033d83fb772a66ea1b5c0339 |
|
MD5 | 9d9d89a7ef81753b9933af727397baf7 |
|
BLAKE2b-256 | e6ba1d14f27dd14393dd69080869d18300b3733e12971d7d540e93e72ba52153 |
Hashes for cxx_image_io-0.0.16-cp313-cp313-manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f625fab196bf143c7fc20386c39233aeac8bb9c13e8069153f4e30d5165b9be3 |
|
MD5 | 73cc81ef5cde682525c47f622fa25c9b |
|
BLAKE2b-256 | 65a5caea2f013f4920983e7bf942fdc9f5737d1645c725fa6065301978856da8 |
Hashes for cxx_image_io-0.0.16-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56a35aa75dd7ac4c90db45890a54d5fe5d1182df83cd79d68a6bb2085744584c |
|
MD5 | 597b77f2ff214920bd17f1635dd86b75 |
|
BLAKE2b-256 | 56adbc5ee5522aeaf61bfd237f1a858a0fdbf25043a38a58882b64980058530e |
Hashes for cxx_image_io-0.0.16-cp312-cp312-manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc19228a32a8b2b4b7aeea4f0eefccf1e1673415c26a3144db228f652eadf763 |
|
MD5 | ccdc1a3119b1b72dc8d48acb06594ae9 |
|
BLAKE2b-256 | 86844dfad26ed2bef90ce670691f308c447d34014717f4ec0ab81f724bd6fc4d |