0

My program breaks an image into 4 channels of CMYK. When I go to re stack these images something isn't working, it does not look like the original image although it does have color.

Input Image: "xpwallpaper.jpg"

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)

Resulting Image: "CMYK.jpg"

This is an oddly specific issue I am having trouble searching.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • How does np.dstack() know it is CMYK? I suspect it is being treated as BGRA. Perhaps convert your result from BGRA to CMYK using cvtColor(). What happens? – fmw42 Apr 21 '22 at 18:49
  • I used code from [here](https://stackoverflow.com/questions/60814081/how-to-convert-a-rgb-image-into-a-cmyk) How do convert with cvtColor, I cant find a cv2 code that will do it, its just rgb to gray. – codingMakesMeCry Apr 21 '22 at 21:16
  • try `cv2.cvtColor(CMYK, cv2.COLOR_BGRA2CMYK)` – fmw42 Apr 21 '22 at 21:20
  • `module 'cv2' has no attribute 'COLOR_BGRA2CMYK'` – codingMakesMeCry Apr 21 '22 at 21:23
  • Possibly use PIL to save to disk as CMYK with a CMYK profile. – fmw42 Apr 21 '22 at 21:33
  • [I posted this question with another image attempt](https://stackoverflow.com/questions/71961147/opencv-rgb-to-cmyk-conversion-not-working-with-python) – codingMakesMeCry Apr 21 '22 at 21:39

0 Answers0