0

I am trying to extract the K channel of a CMYK image using Python. However, the result is an inverted channel. Here is the code I am using:

# Import Packages
import numpy as np
from PIL import Image

# Define method
def get_k_channel(filepath):
    # Read CMYK image
    img_cmyk = np.array(Image.open(filepath))
    # Extract K channel
    # I also tried with [:, :, -1] slicing
    img_k = img_cmyk[:, :, 3]
    # Image to uint8
    img_k = img_k.astype(np.uint8)
    # NumPy image to PIL object
    img_pil = Image.fromarray(img_k)
    # Mode L for grayscale images
    img_pil.mode = 'L'
    # Save image as TIF file
    img_pil.save('image_k_channel.tif')

Below is the original K channel, and the result K channel. Any ideas on what am I doing wrong? Thanks!

Original K Channel

Result K Channel

zgebac26
  • 81
  • 9
  • CYMK is a subtractive colour model (as opposed to RGB, which is additive). Assuming your image was taken in the day, I think that means you would expect a low K (black) value for the sky. – FiddleStix Feb 08 '23 at 09:07
  • @FiddleStix I just added an image of the original K channel. It should be the same color space, so the blacks and whites should also be the same. – zgebac26 Feb 08 '23 at 09:17
  • Did you consider `K = img_cmyk.split()[3]` ? – Mark Setchell Feb 08 '23 at 09:23
  • @MarkSetchell just tried, still the same result. – zgebac26 Feb 08 '23 at 09:36
  • Maybe you can share your CMYK image, using Dropbox or Google Drive or some service that doesn't *"mangle"* images. – Mark Setchell Feb 08 '23 at 10:16
  • @MarkSetchell here is the link to the original CMYK image: https://drive.google.com/file/d/14prPx0MOciqNrnKRujO8ysBimmxarq1d/view?usp=share_link – zgebac26 Feb 08 '23 at 11:22
  • I am not very familiar with CMYK printing, but I note that **ImageMagick** extracts the K channel just the same as PIL. I also note that there are lots of binary calibration curves embedded in your image (Gamut, AtoB0, Gray Tone Reproduction Curve and so on) that some packages may ignore, or not know how to process. – Mark Setchell Feb 08 '23 at 11:54
  • @MarkSetchell that does make sense, but this is an obvious problem in terms of inverting the channel, it is not a matter of calibration, etc. Anyways, I'll let you know if the problem is fixed. Thanks! – zgebac26 Feb 09 '23 at 12:26

0 Answers0