0

I'm trying to convert an image to cmyk array manually and reconstruct the image but i didn't get any image then i decide to separate c,m,y,k and display it but there is no proper image .so i just tried to save every data in file for verification,i can't find any error in that data.can any one tell me why this happens and what is the error that i done here.i post my entire code below.

from PIL import Image
import numpy as np

im = Image.open('idcard.jpg').convert('RGB')
np_image = np.array(im)
num_list = np_image.tolist()
print( len(num_list))
str1 =str(num_list)
print( len(str1))
f=open("idcardforgb.txt","w")
f.write(str1)
f.close()

cyan = 0
magenta = 0
yellow = 0
key = 0
cmyk_scale = 255
t=np.shape(np_image)
print (t)
i=(int)(t[0])
j=(int)(t[1])
k=(int)(t[2])

c_final=[[[0 for f in range(4)]for g in range(j)]for h in range(i)]

for z in range(i):
    temp_z=z
    for y in range(j):
        temp_y=y
        for x in range(3):
            if x==0:
                r = np_image[z][y][x]
            if x==1:
                g = np_image[z][y][x]
            if x==2:
                 b = np_image[z][y][x]
        if (r == 0) and (g == 0) and (b == 0):
            cyan=0
            magenta=0
            yellow=0
            key=cmyk_scale
            c_final[temp_z][temp_y][0]=cyan
            c_final[temp_z][temp_y][1]=magenta
            c_final[temp_z][temp_y][2]=yellow
            c_final[temp_z][temp_y][3]=key
        else:
            c = 1 - r / 255.
            m = 1 - g / 255.
            y = 1 - b / 255.
            min_cmy = min(c,m,y)
            c = (c - min_cmy) / (1 - min_cmy)
            m = (m - min_cmy) / (1 - min_cmy)
            y = (y - min_cmy) / (1 - min_cmy)
            k = min_cmy
            cyan =(int) (c*cmyk_scale)
            magenta =(int) (m*cmyk_scale)
            yellow = (int)(y*cmyk_scale)
            key = (int)(k*cmyk_scale)

            c_final[temp_z][temp_y][0]=cyan
            c_final[temp_z][temp_y][1]=magenta
            c_final[temp_z][temp_y][2]=yellow
            c_final[temp_z][temp_y][3]=key

np_image1 = np.array(c_final)
t1=np.shape(np_image1)
print(t1)
cnum_list = np_image1.tolist()
print( len(cnum_list))
str1c =str(cnum_list)
print( len(str1c))
f=open("idcardcmyk.txt","w")
f.write(str1c)
f.close()
im = Image.fromarray(np_image1, mode='CMYK')
im.save('testing.jpg')
##im = Image.fromarray(im, mode="CMYK")
print("test ok")
ct = np_image1[:, :, 0]
cyan_list=ct.tolist()
str_c =str(cyan_list)
f=open("idcard_cp.txt","w")
f.write(str_c)
f.close()
print("test ok")
img= Image.fromarray(ct)
img.save('idcard_c.png')
mt = np_image1[:, :, 1]
imm= Image.fromarray(mt)
imm.save('idcard_m.png')
yt = np_image1[:, :, 2]
imm = Image.fromarray(yt)
imm.save('idcard_y.png')
kt = np_image1[:, :, 3]
imm = Image.fromarray(kt)
imm.save('idcard_k.png')
2
  • You should include the stack trace in this answer. Also where did you get the code for the RGB to CMYK conversion? Commented Oct 18, 2018 at 12:34
  • "stackoverflow.com/questions/14088375/…" conversion that i done based on the link.please tell me what is the error in this code Commented Oct 18, 2018 at 12:56

0

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.