在PIL和Imagemagick中将颜色叠加应用于图像

时间:2012-02-08 12:48:26

标签: python image-processing python-imaging-library

我是图像处理的新手,我猜这很容易,但我只是不知道术语。

基本上我有一个黑白图像,我只想在图像上涂上彩色叠加层,这样我就可以将图像覆盖为蓝绿色读取和黄色,如下图所示(实际上我不能显示,因为我没有足够的声誉这样做 - grrrrrr)。想象一下,我有一个物理图像,以及绿色/红色/蓝色/黄色覆盖图,我将其置于图像上方。

理想情况下我想使用Python PIL这样做,但我会很乐意使用ImageMagik来做,但无论哪种方式我都需要能够编写脚本,因为我需要100个左右的图像执行这个过程。

3 个答案:

答案 0 :(得分:24)

以下是一段代码摘要,其中显示了如何使用scikit-image覆盖灰度图像上的颜色。我们的想法是将两个图像转换为HSV颜色空间,然后将灰度图像的色调和饱和度值替换为颜色蒙版的颜色和饱和度值。

from skimage import data, color, io, img_as_float
import numpy as np
import matplotlib.pyplot as plt

alpha = 0.6

img = img_as_float(data.camera())
rows, cols = img.shape

# Construct a colour image to superimpose
color_mask = np.zeros((rows, cols, 3))
color_mask[30:140, 30:140] = [1, 0, 0]  # Red block
color_mask[170:270, 40:120] = [0, 1, 0] # Green block
color_mask[200:350, 200:350] = [0, 0, 1] # Blue block

# Construct RGB version of grey-level image
img_color = np.dstack((img, img, img))

# Convert the input image and color mask to Hue Saturation Value (HSV)
# colorspace
img_hsv = color.rgb2hsv(img_color)
color_mask_hsv = color.rgb2hsv(color_mask)

# Replace the hue and saturation of the original image
# with that of the color mask
img_hsv[..., 0] = color_mask_hsv[..., 0]
img_hsv[..., 1] = color_mask_hsv[..., 1] * alpha

img_masked = color.hsv2rgb(img_hsv)

# Display the output
f, (ax0, ax1, ax2) = plt.subplots(1, 3,
                                  subplot_kw={'xticks': [], 'yticks': []})
ax0.imshow(img, cmap=plt.cm.gray)
ax1.imshow(color_mask)
ax2.imshow(img_masked)
plt.show()

这是输出:

enter image description here

答案 1 :(得分:12)

我最终使用PIL找到了答案,基本上创建了一个带有块颜色的新图像,然后使用定义透明alpha图层的蒙版,使用这个新图像合成原始图像。下面的代码(适用于转换名为data的文件夹中的每个图像,输出到名为output的文件夹中):

from PIL import Image
import os

dataFiles = os.listdir('data/')

for filename in dataFiles:

    #strip off the file extension
    name = os.path.splitext(filename)[0]

    bw = Image.open('data/%s' %(filename,))

    #create the coloured overlays
    red = Image.new('RGB',bw.size,(255,0,0))
    green = Image.new('RGB',bw.size,(0,255,0))
    blue = Image.new('RGB',bw.size,(0,0,255))
    yellow = Image.new('RGB',bw.size,(255,255,0))

    #create a mask using RGBA to define an alpha channel to make the overlay transparent
    mask = Image.new('RGBA',bw.size,(0,0,0,123))

    Image.composite(bw,red,mask).convert('RGB').save('output/%sr.bmp' % (name,))
    Image.composite(bw,green,mask).convert('RGB').save('output/%sg.bmp' % (name,))
    Image.composite(bw,blue,mask).convert('RGB').save('output/%sb.bmp' % (name,))
    Image.composite(bw,yellow,mask).convert('RGB').save('output/%sy.bmp' % (name,))

由于缺乏代表,无法发布输出图像。

答案 2 :(得分:2)

请参阅我的要点https://gist.github.com/Puriney/8f89b43d96ddcaf0f560150d2ff8297e

通过opencv的核心功能描述如下。

def mask_color_img(img, mask, color=[0, 255, 255], alpha=0.3):
    '''
    img: cv2 image
    mask: bool or np.where
    color: BGR triplet [_, _, _]. Default: [0, 255, 255] is yellow.
    alpha: float [0, 1]. 

    Ref: http://www.pyimagesearch.com/2016/03/07/transparent-overlays-with-opencv/
    '''
    out = img.copy()
    img_layer = img.copy()
    img_layer[mask] = color
    out = cv2.addWeighted(img_layer, alpha, out, 1 - alpha, 0, out)
    return(out)

在RGB或灰色图像上添加彩色和透明叠加可以起作用: highlight-on-color highlight-on-gray