How to modify and save a 16-bit depth RGBA image with icc color profile in python?

454 Views Asked by At

I am searching for a long time on net. But found nothing useful. Please help or try to give some ideas how to achieve this. I have some 16-bit depth per channel RGBA with icc color profile png images, and want to remove their Alpha channel and save it with original icc profile. I tried python-opencv and PIL.Image. Opencv could save 16-bit RGBA image but without icc profile, PIL.Image could save image with icc profile but it doesn't support 16-bit RGBA image. Am I using these two modules wrong? Or is there any other modules could handle this?

Here`s my code(simplified):

opencv part:

src_image = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8), cv2.IMREAD_UNCHANGED)

detect bit depth
if src_image.dtype == "uint8":
    depth_value = 255
if src_image.dtype == "uint16":
    depth_value = 65535

# if alpha channel is full white, then remove alpha channel
b, g, r, a = cv2.split(src_image)
if np.all(a == depth_value):
    merged = cv2.merge([b,g,r])
    cv2.imwrite(out_path,merged,[cv2.IMWRITE_PNG_COMPRESSION,png_comp])
else:
    pass

PIL.Image part:

image = Image.open(image_path)
image.load()

r, g, b, a = image.split()
print(r.mode) #it prints "L"
image = Image.merge('BGR;16',(b,g,r)) #ValueError: wrong number of bands

image.save(out_path,bits = 'BGR;16',icc_profile=image.info.get('icc_profile')) #I only found "BGR;16" and no "RGB;16"

And modes in PIL.Image module documentation attched below:

Modes

The mode of an image is a string which defines the type and depth of a pixel in the image. Each pixel uses the full range of the bit depth. So a 1-bit pixel has a range of 0-1, an 8-bit pixel has a range of 0-255 and so on. The current release supports the following standard modes:

1 (1-bit pixels, black and white, stored with one pixel per byte)

L (8-bit pixels, black and white)

P (8-bit pixels, mapped to any other mode using a color palette)

RGB (3x8-bit pixels, true color)

RGBA (4x8-bit pixels, true color with transparency mask)

CMYK (4x8-bit pixels, color separation)

YCbCr (3x8-bit pixels, color video format)Note that this refers to the JPEG, and not the ITU-R BT.2020, standard

LAB (3x8-bit pixels, the Lab color space)

HSV (3x8-bit pixels, Hue, Saturation, Value color space)

I (32-bit signed integer pixels)

F (32-bit floating point pixels)

Pillow also provides limited support for a few additional modes, including:

LA (L with alpha)

PA (P with alpha)

RGBX (true color with padding)

RGBa (true color with premultiplied alpha)

La (L with premultiplied alpha)

I;16 (16-bit unsigned integer pixels)

I;16L (16-bit little endian unsigned integer pixels)

I;16B (16-bit big endian unsigned integer pixels)

I;16N (16-bit native endian unsigned integer pixels)

BGR;15 (15-bit reversed true colour)

BGR;16 (16-bit reversed true colour)

BGR;24 (24-bit reversed true colour)

BGR;32 (32-bit reversed true colour)

0

There are 0 best solutions below