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 | Not yet | 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 | * | Bayer | .plain16 | |
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 only Python 3.11 and 3.12 on Windows and Linux.
The user need to install python 3.11 or 3.12,
Then install the numpy>=1.26
by pip
pip install numpy==1.26
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
image, metadata = read_image('/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('Type:', metadata.fileInfo.pixelType)
print('Precision:', metadata.fileInfo.pixelPrecision)
print('Layout:', metadata.fileInfo.imageLayout)
The result could be like this:
Type: PixelType.RGB
Precision: 8
Layout: ImageLayout.INTERLEAVED
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/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 could be like this:
Type: PixelType.BAYER_GBRG
Precision: 16
Layout: ImageLayout.PLANAR
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.
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
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/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/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
exif = read_exif('/path/to/image.jpg')
print('model:', exif.model)
print('make:', exif.make)
print('exposureTime:', exif.exposureTime.asDouble())
print('focalLength:', exif.focalLength.asDouble())
print('exposureTime:', exif.exposureTime.asDouble())
print('focalLength:', exif.focalLength.asDouble())
print('fNumber:',exif.fNumber.asDouble())
print('isoSpeedRatings:',exif.isoSpeedRatings)
write_exif('path/to/new_image.jpg', exif)
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/to/image.jpg')
metadata.exifMetadata.make = 'Custom'
write_options = ImageWriter.Options(metadata)
write_image('/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.10-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1b56663bdc23ac526347b68f268b1aaf74de387a891ab8c08b0d6159f6f6b3c |
|
MD5 | ca5cbb51c7dc609e59ceb9a25df3d7d9 |
|
BLAKE2b-256 | 045fd5c1770f46e34ed022a73dc9db862e7254b8d92898d4efc39493b91a9097 |
Hashes for cxx_image_io-0.0.10-cp312-cp312-manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fea7e6e7465f97d4866f9bfd487346487f4e9060562518975f1e5484d0f0e991 |
|
MD5 | 00631f22a3687c1500557c416116b5f5 |
|
BLAKE2b-256 | 2b3ba313081038cb15927475d9e9df6f264194e5496646c35a45146141c6f57b |
Hashes for cxx_image_io-0.0.10-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea71e545fc0c1d7d90f6684a3f0e8a35517044aa20e98abcc2014837e256b9fd |
|
MD5 | b1ee03745ed93227a7503a9e2d38d05c |
|
BLAKE2b-256 | 0bf3382dd8a2d6bce8598d4dd62e8e5bcb33b6b160368efed481767e551dfb3f |
Hashes for cxx_image_io-0.0.10-cp311-cp311-manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8150275d2010cd257a1f9ce1bb9472b7532513a98cbeaa7dff058c0d76c6440a |
|
MD5 | edbdd93892a1f60ad095ef18f540625a |
|
BLAKE2b-256 | dd74b5528db2d5f5af3856c817bedab2d4df7178324808f06e52f8a08335bbc6 |