如何从彩色图像创建CMYK半色调图像?

2024-05-23 18:01:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个项目,需要我将CYMK图像中的每种颜色分离出来,并生成一个半色调图像,该图像将在特殊的半色调打印机上打印。所使用的方法类似于丝绸筛选,因为过程几乎相同。拍摄一张照片,并打破每个颜色通道。然后为半色调生成屏幕。每个彩色屏幕必须使其屏幕倾斜15-45度(可调)。点大小和LPI必须根据用户可配置的值计算,以实现不同的效果。我听说这一过程用于丝印,但我一直无法找到任何解释CYMK半色调的信息。我发现有很多方法可以简化为单色并生成新的打印样式的黑白半色调图像

我想我需要:

  1. 将文件拆分为其颜色通道
  2. 为该频道生成单色半色调图像
  3. 将生成的半色调图像倾斜度数*通道数

有人知道这是否是正确的方法,是否有任何现有的python代码用于此目的?或者对这个过程或算法有什么好的解释


Tags: 项目方法用户图像屏幕颜色过程打印机
2条回答

我曾经经营过一家丝网印刷工作室(那是一家相当小的工作室),尽管我从未真正做过分色印刷,但我对分色印刷的原理相当熟悉。我会这样做:

  1. 将图像拆分为C、M、Y、K
  2. 将每个分离的图像分别旋转0、15、30和45度
  3. 取每个图像的半色调(网点大小将与强度成比例)
  4. 向后旋转每个半色调图像

现在你有了分色图像。正如您所提到的,旋转步骤减少了点对齐问题(这会把一切都搞糟),并且像Moiré pattern effects这样的事情将被合理地最小化

使用PIL编写代码应该很容易

更新2:

我为您编写了一些快速代码,其中还包括一个GCR函数(如下所述):

import Image, ImageDraw, ImageStat

def gcr(im, percentage):
    '''basic "Gray Component Replacement" function. Returns a CMYK image with 
       percentage gray component removed from the CMY channels and put in the
       K channel, ie. for percentage=100, (41, 100, 255, 0) >> (0, 59, 214, 41)'''
    cmyk_im = im.convert('CMYK')
    if not percentage:
        return cmyk_im
    cmyk_im = cmyk_im.split()
    cmyk = []
    for i in xrange(4):
        cmyk.append(cmyk_im[i].load())
    for x in xrange(im.size[0]):
        for y in xrange(im.size[1]):
            gray = min(cmyk[0][x,y], cmyk[1][x,y], cmyk[2][x,y]) * percentage / 100
            for i in xrange(3):
                cmyk[i][x,y] = cmyk[i][x,y] - gray
            cmyk[3][x,y] = gray
    return Image.merge('CMYK', cmyk_im)

def halftone(im, cmyk, sample, scale):
    '''Returns list of half-tone images for cmyk image. sample (pixels), 
       determines the sample box size from the original image. The maximum 
       output dot diameter is given by sample * scale (which is also the number 
       of possible dot sizes). So sample=1 will presevere the original image 
       resolution, but scale must be >1 to allow variation in dot size.'''
    cmyk = cmyk.split()
    dots = []
    angle = 0
    for channel in cmyk:
        channel = channel.rotate(angle, expand=1)
        size = channel.size[0]*scale, channel.size[1]*scale
        half_tone = Image.new('L', size)
        draw = ImageDraw.Draw(half_tone)
        for x in xrange(0, channel.size[0], sample):
            for y in xrange(0, channel.size[1], sample):
                box = channel.crop((x, y, x + sample, y + sample))
                stat = ImageStat.Stat(box)
                diameter = (stat.mean[0] / 255)**0.5
                edge = 0.5*(1-diameter)
                x_pos, y_pos = (x+edge)*scale, (y+edge)*scale
                box_edge = sample*diameter*scale
                draw.ellipse((x_pos, y_pos, x_pos + box_edge, y_pos + box_edge), fill=255)
        half_tone = half_tone.rotate(-angle, expand=1)
        width_half, height_half = half_tone.size
        xx=(width_half-im.size[0]*scale) / 2
        yy=(height_half-im.size[1]*scale) / 2
        half_tone = half_tone.crop((xx, yy, xx + im.size[0]*scale, yy + im.size[1]*scale))
        dots.append(half_tone)
        angle += 15
    return dots

im = Image.open("1_tree.jpg")

cmyk = gcr(im, 0)
dots = halftone(im, cmyk, 10, 1)
im.show()
new = Image.merge('CMYK', dots)
new.show()

这将改变这一点:

enter image description here

进入此状态(模糊您的眼睛并远离显示器):

enter image description here

请注意,图像采样可以逐像素进行(从而在最终图像中保持原始图像的分辨率)。通过设置sample=1来实现这一点,在这种情况下,您需要将scale设置为一个更大的数字,以便有许多可能的点大小。这也会导致输出图像的大小变大(原始图像大小*比例**2,所以要小心!)

默认情况下,从RGB转换为CMYKK通道(黑色通道)为空。是否需要K通道取决于打印过程。您可能想要它的原因有很多:获得比重叠的CMY更好的黑色、节省墨水、改善干燥时间、减少墨水溢出等等。无论如何,我还编写了一个小的Grey component replacement函数GCR,因此您可以设置要替换重叠的K通道的百分比(我在代码注释中对此进行了进一步解释)

这里有两个例子来说明。用sample=1scale=8处理图像中的letter F,分辨率相当高

4CMYK通道,带有percentage=0,因此空K通道:

enter image description hereenter image description hereenter image description hereenter image description here

联合生产:

enter image description here

CMYK通道,带有percentage=100,因此使用了K通道。您可以看到青色通道被完全抑制,洋红和黄色通道使用的墨水更少,在图像底部的黑色带中:

enter image description hereenter image description hereenter image description hereenter image description hereenter image description here

我的解决方案也使用PIL,但依赖于内部支持的内部抖动方法(Floyd Steinberg)。创建工件,所以我正在考虑重写它的C代码

    from PIL import Image

    im  = Image.open('tree.jpg')             # open RGB image
    cmyk= im.convert('CMYK').split()         # RGB contone RGB to CMYK contone
    c = cmyk[0].convert('1').convert('L')    # and then halftone ('1') each plane
    m = cmyk[1].convert('1').convert('L')    # ...and back to ('L') mode
    y = cmyk[2].convert('1').convert('L')
    k = cmyk[3].convert('1').convert('L')

    new_cmyk = Image.merge('CMYK',[c,m,y,k]) # put together all 4 planes
    new_cmyk.save('tree-cmyk.jpg')           # and save to file

隐式GCR PIL应用也可以扩展为更通用的解决方案,但我试图描述一个简单的解决方案,其中也忽略了分辨率和采样

相关问题 更多 >