0

I am trying to separate the CMYK channels from an RGB image to be used later. The question np.dstack() does not recreate original image in OpenCV is my earlier question using a test image. I decided to run it again with a different image and it returned some results and I don't know what's happening.

The issue with this is the results do not match the original image as they are supposed to.

Left: Original Image
Top Right: Result as .jpg
Bottom Right: Result as .tif
test.png

Code:

import cv2
import numpy as np

# Load image
bgr = cv2.imread('xpwallpaper.jpg')

# Make float and divide by 255 to give BGRdash
bgrdash = bgr.astype(np.float)/255.

# Calculate K as (1 - whatever is biggest out of Rdash, Gdash, Bdash)
K = 1 - np.max(bgrdash, axis=2)

# Calculate C
C = (1-bgrdash[...,2] - K)/(1-K)

# Calculate M
M = (1-bgrdash[...,1] - K)/(1-K)

# Calculate Y
Y = (1-bgrdash[...,0] - K)/(1-K)

# Combine 4 channels into single image and re-scale back up to uint8
CMYK = (np.dstack((C,M,Y,K))*255).astype(np.uint8)
cv2.imwrite("CMYK.jpg", CMYK)
  • When you save your 4 channels to JPG, it does not know it is CMYK and discards the alpha channel and thinks the image is RGB. cv2.imwrite() does not seem to have a JPG flag to tell it that the data is CMYK. I think you need to use PIL to attach a CMYK profile to your 4 channel image when writing to disk. – fmw42 Apr 21 '22 at 21:56
  • Alternately, read your input in PIL and convert and save to CMYK JPG – fmw42 Apr 21 '22 at 21:59
  • How exactly are the results not what you expected — what's wrong with them? Please indicate this in your question (not down here). – martineau Apr 21 '22 at 22:36
  • I have updated it, I forgot to put that in as it is a development of the question I linked. I asked again as I used a different image that actually shows these colors and I realized np.dstack() is not at fault. – codingMakesMeCry Apr 22 '22 at 00:00

0 Answers0