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).

Aju S
  • 41
  • 1
  • 7
  • 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! – Mark Setchell Oct 15 '18 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 – Aju S Oct 16 '18 at 05:30

1 Answers1

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.

dwagnerkc
  • 488
  • 4
  • 8
  • 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 ? – Aju S Oct 16 '18 at 06:56
  • I have updated the answer with channel separation example. – dwagnerkc Oct 16 '18 at 10:47
  • 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) – Aju S Oct 16 '18 at 12:47
  • `import matplotlib.pyplot as plt` `plt.imshow(c, cmap='Blues')` `plt.show()` – dwagnerkc Oct 16 '18 at 15:32
  • ok but the black content of every image is zero why? "https://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? – Aju S Oct 17 '18 at 04:54
  • Not 100% sure, has something to do with PIL being aware of color profile. See https://mail.python.org/pipermail/image-sig/2009-July/005793.html and this SO answer https://stackoverflow.com/questions/38855022/conversion-from-cmyk-to-rgb-with-pillow-is-different-from-that-of-photoshop – dwagnerkc Oct 17 '18 at 15:25
  • i tried another way that i'm manually convert rgb array to cmyk array but there is problem in inputting the value in cmyk 3d array..example c[z][y][0]=cyan it got an error 'TypeError: 'float' object is not subscriptable' .why this happened?(z=0,y=0 and cyan=73) – Aju S Oct 18 '18 at 07:33
  • Looks like c is a 2d array of floats. So c[z][y] will access a float value and floats can be sub scripted with brackets. i. e. 3.4[0] doesn’t make any sense in Python. – dwagnerkc Oct 18 '18 at 10:06
  • Thanks,i need one more answer .i post the link here "https://stackoverflow.com/questions/52873574/convert-image-to-rgb-array-using-pil-and-manually-convert-it-to-cmyk-array" please answer it – Aju S Oct 18 '18 at 12:05