如何将RGB图像转换为cmyk? | 您所在的位置:网站首页 › rgb怎么转换成cmyk › 如何将RGB图像转换为cmyk? |
你可以让PIL/Pillow像这样为你做这件事。 from PIL import Image # Open image, convert to CMYK and save as TIF Image.open('drtrump.jpg').convert('CMYK').save('result.tif')如果我使用IPython,我可以将加载、转换和保存的时间控制在13ms。in toto like this: %timeit Image.open('drtrump.jpg').convert('CMYK').save('PIL.tif') 13.6 ms ± 627 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)如果你想通过实施你的公式来自己做,你最好使用vectorisedNumpy而不是for的循环。这需要35ms。 #!/usr/bin/env python3 import cv2 import numpy as np # Load image bgr = cv2.imread('drtrump.jpg') # Make float and divide by 255 to give BGRdash bgrdash = bgr.astype(np.float)/255. # Calculate K as (1 - whatever is biggest out of Rdash, Gdash, Bdash) K = 1 - np.max(bgrdash, axis=2) # Calculate C C = (1-bgrdash[...,2] - K)/(1-K) # Calculate M M = (1-bgrdash[...,1] - K)/(1-K) # Calculate Y Y = (1-bgrdash[...,0] - K)/(1-K) # Combine 4 channels into single image and re-scale back up to uint8 CMYK = (np.dstack((C,M,Y,K)) * 255).astype(np.uint8)如果你想检查你的结果,你需要注意一些事情。不是所有的图像格式都能保存CMYK,这就是为什么我保存为TIFF。第二,你的公式把你所有的值都留给了0...1范围内的浮点,所以你可能想通过乘以255并转换为uint8来回溯规模。 最后,你可以保证正确的结果是什么,只需使用形象化的Magick在终端中。 magick drtrump.jpg -colorspace CMYK result.tif |
CopyRight 2018-2019 实验室设备网 版权所有 |