简体   繁体   English

将彩色叠加应用于 PIL 或 Imagemagik 中的图像

[英]Applying a coloured overlay to an image in either PIL or Imagemagik

I am a complete novice to image processing, and I am guessing this is quite easy to do, but I just don't know the terminology.我是图像处理的完全新手,我猜这很容易做到,但我只是不知道术语。

Basically, I have a black and white image, I simply want to apply a colored overlay to the image, so that I have got the image overlayed with blue green red and yellow like the images shown below (which actually I can't show because I don't have enough reputation to do so - grrrrrr).基本上,我有一个黑白图像,我只是想对图像应用彩色叠加层,这样我就可以将图像用蓝绿色红色和黄色叠加,如下图所示(实际上我无法显示,因为我没有足够的声誉这样做 - grrrrrr)。 Imagine I have a physical image, and a green/red/blue/yellow overlay, which I place on top of the image.想象一下,我有一个物理图像和一个绿色/红色/蓝色/黄色叠加层,我将其放置在图像顶部。

Ideally, I would like to do this using Python PIL but I would be just as happy to do it using ImageMagik, but either way, I need to be able to script the process as I have 100 or so images that I need to carry out the process on.理想情况下,我想使用 Python PIL 来执行此操作,但我也很乐意使用 ImageMagik 来执行此操作,但是无论哪种方式,我都需要能够编写该过程的脚本,因为我有 100 个左右的图像需要执行过程。

EDIT: As mentioned by Matt in the comments, this functionality is now available in skimage.color.label2rgb .编辑:正如马特在评论中提到的,这个功能现在在skimage.color.label2rgb可用。

In the latest development version, we've also introduced a saturation parameter, which allows you to add overlays to color images.在最新的开发版本中,我们还引入了saturation参数,它允许您为彩色图像添加叠加层。


Here's a code snippet that shows how to use scikit-image to overlay colors on a grey-level image.这是一个代码片段,展示了如何使用scikit-image在灰度图像上叠加颜色。 The idea is to convert both images to the HSV color space, and then to replace the hue and saturation values of the grey-level image with those of the color mask.想法是将两个图像都转换为 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()

Here's the output:这是输出:

在此处输入图片说明

I ended up finding an answer to this using PIL, basically creating a new image with a block colour, and then compositing the original image, with this new image, using a mask that defines a transparent alpha layer.我最终使用 PIL 找到了这个问题的答案,基本上是创建一个带有块颜色的新图像,然后使用定义透明 alpha 层的蒙版将原始图像与这个新图像合成。 Code below (adapted to convert every image in a folder called data, outputting into a folder called output):下面的代码(适用于转换名为 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,))

Can't post the output images unfortunately due to lack of rep.遗憾的是,由于缺少代表,无法发布输出图像。

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

Core function via opencv is described as below.通过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)

Add colored and transparent overlay on either RGB or gray image can work:在 RGB 或灰色图像上添加彩色和透明叠加可以工作: 高亮颜色 高亮灰色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM