2

I'm trying to convert an image to cmyk array and seperate Cyan,Magentha,Yellow,Black values .Is it possible with python .How to convert image to Rgb array and then to cmyk?

from PIL import Image

im = Image.open('apple.png')

pixels = list(im.getdata())

print (pixels)

i tried the above code but the ide got stuck after running the code.is there any mistake in that code ?(in above code i just print the rgb array only).

2
  • 1
    Your title says you want to convert to CMYK, then your question says you want to go to RGB then CMYK, then it says you want to get the RGB array. I'm confused! Commented Oct 15, 2018 at 11:56
  • my actual application is for an id card printer so it needs a CMYK image that's Cyan,Magenta,Yellow,Black values in separate array.I notice that by using an equation we can convert RGB array to CMYK elements.That's why i ask that question Commented Oct 16, 2018 at 5:30

1 Answer 1

4

Covert to CMYK:

im = Image.open('apple.png').convert('CMYK')

I would recommend numpy (imported as np conventionally) for working with the pixel data. Conversion between the two is simple.

Image->ndarray: np.array(Image)

ndarray->Image: Image.fromarray(ndarray)

So covert your image to an ndarray:

import numpy as np

np_image = np.array(im)

Let's check the dimensions of the image:

print(np_image.shape) # (rows, columns, channels)

(400, 600, 4)

And finally print the actual pixel values:

print(np_image)

[[[173 185 192   0]
  [174 185 192   0]
  [173 185 192   0]
  ...
  [203 208 210   0]
  [203 209 210   0]
  [202 207 209   0]]
 ...
 [[180 194 196   0]
  [182 195 198   0]
  [185 197 200   0]
  ...
  [198 203 206   0]
  [200 206 208   0]
  [198 204 205   0]]]

To get each of the individual channels we can use numpy slicing. Similar to Python's list slicing, but works across n dimensions. The notation can look confusing, but if you look at the individual slices per dimension it is easier to break down.

# [:,      :,      0] 
#   ^ Rows  ^ Cols  ^ Channel
# The colon alone indicates no slicing, so here we select
# all rows, and then all columns, and then 0 indicates we
# want the first channel from the CMYK channels.

c = np_image[:, :, 0]
m = np_image[:, :, 1]
y = np_image[:, :, 2]
k = np_image[:, :, 3]

What we have now are four ndarrays of shape (400, 600) for each of the channels in the original CMYK np_image.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks ,How can we separate Cyan,Magenta,Yellow,Black values from the numpy array ?Tht's i need cyan alone ,magenta alone yellow alone and black also .Is it possible ?
I have updated the answer with channel separation example.
Thank ,the code works fine .is it possible to create an image with the single color using python?(for example create image with cyan only)
import matplotlib.pyplot as plt plt.imshow(c, cmap='Blues') plt.show()
ok but the black content of every image is zero why? "en.wikipedia.org/wiki/CMYK_color_model" in this link i noticed there have an image which separate into CMYK and there have a black component .then i tries to convert the same image to CMYK by using the python code but the black component is zero,why?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.