将彩色叠加层应用于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.
基本上我有一个黑白图像,我只想在图像上涂上彩色叠加层,这样我就可以将图像覆盖为蓝绿色读取和黄色,就像下面显示的图像一样(实际上我无法显示,因为我不是有足够的声誉这样做 - grrrrrr)。想象一下,我有一个物理图像,以及绿色/红色/蓝色/黄色叠加层,我将其置于图像顶部。
Basically I have a black and white image, I simply want to appy a coloured overlay to the image, so that I have got the image overlayed with blue green read and yellow like the images shown below (which actually i can't show because I don't have enough reputation to do so - grrrrrr). Imagine i have a physical image, and a green/red/blue/yellow overlay, which I place on top of the image.
理想情况下我想这样做Python PIL,但我会很高兴使用ImageMagik来做,但无论哪种方式我都需要能够编写脚本,因为我需要执行100个左右的图像。
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 a 100 or so images that I need to carry out the process on.
推荐答案
这是一个代码片段,展示了如何使用 scikit-image 用于覆盖灰度图像上的颜色。我们的想法是将两个图像转换为HSV颜色空间,然后将灰度图像的色调和饱和度值替换为颜色蒙版的颜色和饱和度值。
Here's a code snippet that shows how to use scikit-image to overlay colors on a grey-level 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.
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()
这是输出:
这篇关于将彩色叠加层应用于PIL或Imagemagik中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!