【问题标题】:Combining 4 separate monochrome tiffs into 1 CMYK tiff将 4 个单独的单色 tiff 组合成 1 个 CMYK tiff
【发布时间】:2013-11-19 12:32:05
【问题描述】:

我的 tiff 图像以这样一种方式存储,即我将每个平面(颜色)存储在一个单独的文件中。每个文件(C、M、Y、K)都是一个大块 tiff,存储为每像素 8 位的单色文件。

我想使用 python Imaging library(PIL) 将这 4 个文件组合成一个 CMYK 彩色 tiff

这是我到目前为止的代码,但生成的输出 tiff 不正确,tiff 被组合成一个大部分只是黑色的文件。我已将这些文件与另一个实用程序合并,结果是正确的,所以我知道输入文件没有问题。

这是我目前的代码:

if len(sys.argv) <= 1:
    print "ERROR: Usage !"
    exit(1)

try:
    cFile = str(sys.argv[1])+"Cyan.tif"
    mFile = str(sys.argv[1])+"Magenta.tif"
    yFile = str(sys.argv[1])+"Yellow.tif"
    kFile = str(sys.argv[1])+"Black.tif"

    print "Opening files:"
    print cFile
    print mFile
    print yFile
    print kFile

    c_img = Image.open(cFile)
    c_img = c_img.convert("L")

    m_img = Image.open(mFile)
    m_img = m_img.convert("L")

    y_img = Image.open(yFile)
    y_img = y_img.convert("L")

    k_img = Image.open(kFile)
    k_img = k_img.convert("L")

except Exception, e:
    print "ERROR: Unable to open file..."
    print str(e)
    exit(1)
try:
    mergedRaster = Image.merge('CMYK', (c_img, m_img, y_img, k_img))
    mergedRaster = mergedRaster.convert("CMYK")

except Exception, e:
    print "ERROR: Merging plates"
    print str(e)
    exit(0)
#exit(0)
try:
    mergedRaster.save("output.tif", format="TIFF")

except Exception, e:
    print "ERROR: Writing tiff"

注意:我在没有任何 .convert 函数的情况下做了同样的事情,发现结果是一样的。

【问题讨论】:

    标签: python python-imaging-library tiff libtiff


    【解决方案1】:

    我发现解决方案是需要反转分离文件中的所有值,即 255 - 值。

    将每个文件转换为一个 numpy 数组,然后从 255 中减去它。\

    try:
        cArray = numpy.array(c_img)
        mArray = numpy.array(m_img)
        yArray = numpy.array(y_img)
        kArray = numpy.array(k_img)
    except Exception, e:
        print "ERROR: Converting to numpy array..."
        print str(e)
        exit(1)
    
    try:
        toSub = 255
        cInvArray = toSub - cArray
        mInvArray = toSub - mArray
        yInvArray = toSub - yArray
        kInvArray = toSub - kArray
    
    except Exception, e:
        print "ERROR: inverting !"
        print str(e)
    
    try:
        cPlate = Image.fromarray(cInvArray)
        mPlate = Image.fromarray(mInvArray)
        yPlate = Image.fromarray(yInvArray)
        kPlate = Image.fromarray(kInvArray)
    
    except Exception, e:
        print "ERROR: Creating image from numpy arrays"
        print str(e)
        exit(1)
    
    try:
        mergedRaster = Image.merge('CMYK', (cPlate, mPlate, yPlate, kPlate))
    

    我不知道为什么需要这样做,但如果有人能解释一下那就太好了。

    【讨论】:

    • 对不起我的回答(现已删除)。不知何故,我错过了你问题的底部(我的时间已经很晚了,很晚了)。无论如何,您不需要使用 numpy 来反转图像。 Image 对象有一个 point() 方法可以轻松做到这一点。我对为什么必须这样做的猜测是 CMYK 颜色空间是减色的,而不是像 RGB 那样的加色,因为它必须使用墨水颜色而不是浅色。
    • CMYK 是用于打印的subtractive color space。 “减色”意味着较大的值(墨水)转化为较深的颜色(减去光)。如果您的输入图像在被视为“加法”(默认)时大部分是白色的,那么当您以 CMYK 格式查看时,它将被转换为大部分是黑色的——除非您应用数值反转。
    猜你喜欢
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    相关资源
    最近更新 更多