Bz Burr
Bz Burr

Reputation: 101

how to take a grey scale numpy image and tint it red

I have a 2D grey scale image, loaded using imread.0

I want to colourise it.

whats the best way to use numpy/skimage/python to achieve this?

Upvotes: 1

Views: 1617

Answers (3)

Anil Kumar
Anil Kumar

Reputation: 844

import matplotlib.pyplot as plt
from skimage import color
from skimage import img_as_float
from PIL import Image

jpgfile = Image.open("pp.jpg")

grayscale_image = img_as_float(jpgfile)
image = color.gray2rgb(grayscale_image)

red_multiplier = [1, 0, 0]

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 4),
                               sharex=True, sharey=True)
ax1.imshow(red_multiplier * image)

plt.show()

Upvotes: 1

Paul Panzer
Paul Panzer

Reputation: 53039

It will depend a bit on the exact format of your input. But the basic procedure should be as simple as:

>>> import numpy as np
>>> from skimage import data, io
>>> 
# an example grey scale image
>>> grey = data.coins()
# a helper for convenient channel (RGB) picking
>>> RGB = np.array((*"RGB",))
# the actual coloring can be written as an outer product
>>> red = np.multiply.outer(grey, RGB=='R')
# save for posterity
>>> io.imsave('red.png', red)

Upvotes: 2

Sven Harris
Sven Harris

Reputation: 2939

if this is a single channel image you could convert it to a "redscale" image by doing something like this:

zero_channel = np.zeros_like(greyscale_array)
redscale = np.stack([greyscale_array, zero_channel, zero_channel], axis=2)

without fully understanding the shape of your array it's difficult to answer though

Upvotes: 1

Related Questions