Why does Python return inverted image channel?

44 views Asked by At

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

0

There are 0 answers