How to create bump map using as little RAM as possible?

793 Views Asked by At

I am working with Python and NumPy. I have an image:

enter image description here

Its heightmap:

enter image description here

I want to combine them to create a bump map like:

enter image description here

I need to use as little RAM as possible. The formula is:

newColor = oldColor+(tint-oldColor)*abs(difference)*k

difference is the difference in value between 2 adjacent points. tint is zero if difference is negative and 255 when difference is positive. K is 0.5. Basically a pixel lighter if the slope faces north, and darker if the slope faces south. I am allocating too many ndarrays, using too much ram. Is there a way of doing it with fewer arrays?

image is a NumPy ndarray(shape=(8192,3192,3),dtype=np.uint8). heightmap is a NumPy ndarray(shape=(8192,3192),dtype=np.float32) with values from 0 to 255. My code:

#Store the difference in height between two adjacent points
deltas = np.roll(heightmap,1,0)
np.subtract(heightmap,deltas,out=deltas)
difference = np.repeat(deltas[:,:,np.newaxis],3,axis=2)
del deltas

#newColor = oldColor+(shade-oldColor)*difference*k
shades = np.where(difference>0,np.int16(0),np.int16(255))
np.absolute(difference,out=difference)
np.multiply(difference,K,out=difference)
np.subtract(shades,image,out=shades)
np.multiply(difference,shades,out=difference)
del shades
np.add(image,difference,out=image,casting='unsafe')
del difference

#Save image
preview = I.fromarray(np.flip(image,0))
preview.save('image.png')
0

There are 0 best solutions below