Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). 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.

like image 876
Ctrlspc Avatar asked Feb 08 '12 12:02

Ctrlspc


People also ask

What is ImageMagick used for?

Use ImageMagick® to create, edit, compose, or convert digital images. It can read and write images in a variety of formats (over 200) including PNG, JPEG, GIF, WebP, HEIC, SVG, PDF, DPX, EXR and TIFF.

What is XC in ImageMagick?

Yes it is purely an alias for xc: which stood for "X window color", which came from the extreme early days of ImageMagick (long before my time).

What is ImageMagick Hdri?

High Dynamic-range Imaging HDRI accurately represents the wide range of intensity levels found in real scenes ranging from the brightest direct sunlight to the deepest darkest shadows. The HDR imaging approach includes: render/capture floating-point color space.

What is ImageMagick command?

ImageMagick includes a number of command-line utilities for manipulating images. Most of you are probably accustomed to editing images one at a time with a graphical user interface (GUI) with such programs as Gimp or Photoshop. However, a GUI is not always convenient.


2 Answers

EDIT: As mentioned by Matt in the comments, this functionality is now available in skimage.color.label2rgb.

In the latest development version, we've also introduced a saturation parameter, which allows you to add overlays to color images.


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()

Here's the output:

enter image description here

like image 59
Stefan van der Walt Avatar answered Oct 26 '22 10:10

Stefan van der Walt


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. Code below (adapted to convert every image in a folder called data, outputting into a folder called 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.

like image 20
Ctrlspc Avatar answered Oct 26 '22 12:10

Ctrlspc