I have made a recipe to colorize and add an alpha channel to grayscale images. It is my first attempt at using numpy. It is efficient enough with a 0.3 mega pixel test image (takes only 2.3 seconds on my 1.8GHz machine):
but with 7 mega pixel images it is too slow (more like a minute per image). How can I make the code more efficient? It is making a 256 numpy masks for every image which I guess is probably not the best way.
#!/usr/bin/env python3
from PIL import Image
import numpy
# a test image is loaded and converted to a numpy array
img = Image.open( 'lena.jpg' ).convert( 'RGBA' )
img = numpy.array( img )
# color point objects represent how pixels with a specific luminescence are to colourized
class ColorPoint():
def __init__( self, luminescence=0, red=0, green=0, blue=0, alpha=255 ):
self.luminescence = luminescence
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
# colour points are stored in a list, defining the colorization
color_points = []
color_points += [ ColorPoint( luminescence=0, red=255 ) ]
color_points += [ ColorPoint( luminescence=85, green=255 ) ]
color_points += [ ColorPoint( luminescence=170, blue=255 ) ]
color_points += [ ColorPoint( luminescence=255, alpha=0 ) ]
if color_points[0].luminescence!=0 or color_points[-1].luminescence!=255:
print( 'color points do not span full luminescence range!' )
sys.exit()
# red, green and blue, alpha values are read in from the numpy array
red, green, blue, alpha = img[:,:,0], img[:,:,1], img[:,:,2], img[:,:,3]
for luminescence in range( 256 ):
# the luminescence value is either equal to that of a colour point or falls inbetween two
cp = next((x for x in color_points if x.luminescence==luminescence), None)
if( cp ):
# the current luminescence value matches a color point exactly
new_red = cp.red
new_green = cp.green
new_blue = cp.blue
new_alpha = cp.alpha
else:
# find the color points which the the current luminescence value lies between
start_cp = next((x for x in reversed(color_points) if x.luminescence<luminescence), None)
end_cp = next((x for x in color_points if x.luminescence>luminescence), None)
# this factor represents the position between the two colour points
factor = ( luminescence - start_cp.luminescence ) / ( end_cp.luminescence - start_cp.luminescence )
# new RGBA values are set based on the two colour points and the position between the two
new_red = start_cp.red + (end_cp.red-start_cp.red)*factor
new_green = start_cp.green + (end_cp.green-start_cp.green)*factor
new_blue = start_cp.blue + (end_cp.blue-start_cp.blue)*factor
new_alpha = start_cp.alpha + (end_cp.alpha-start_cp.alpha)*factor
# a mask is created for the current luminescence value used to apply the new values
mask = ( red == luminescence ) & ( green == luminescence ) & ( blue == luminescence )
img[:,:,:4][mask] = [ new_red, new_green, new_blue, new_alpha ]
# convert back to PIL image and show
img = Image.fromarray( img )
img.show()
You should be able to use one of the interpolation routines such as griddata
. Here's an example, where I've used a random look-up table which corresponds to color_points
in your code.
>>> from scipy.interpolate import griddata
>>> LUT = np.random.random_sample((256, 3))
>>> griddata(np.arange(256), LUT, image)
To use this in your code, you will need to build a 1d array with the gray/luminescence values and another 2d
array with the corresponding rgb values. These are the first two parameters to griddata
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.