I have an image created in the PIL library and there is a second image.
I need to take the second image, make this cross, for example red, and apply it to the overall outline. How to correctly replace the color, but not directly (FFF to F00), but leaving shadows and a three-dimensional effect?
Here are the steps:
ImageOps.colorize(..., white='red')
to make image vary from black through to red, rather than from black through to whiteImage.new()
paste(..., mask=originalImage)
- the mask ensures you only paste onto the background where the original image is opaque and not where it is transparentLike this:
from PIL import Image, ImageOps
# Load image
im = Image.open('cross.png')
# Colorize
red = ImageOps.colorize(im.convert('L'), black='black', white='red')
red.save('DEBUG-red.png')
# Make solid white background, same size as cross
bg = Image.new('RGB', size=im.size, color='white')
# Paste
bg.paste(red, mask=im)
Here's a slightly different approach leveraging HSV colourspace:
from PIL import Image, ImageOps
im = Image.open('cross.png')
brightness = im.convert('L')
# Set Hue angle to 0 degrees, i.e. red
Hue = Image.new('L', size=im.size, color=0)
# Set Saturation to 50%, not too vivid
Sat = Image.new('L', size=im.size, color=128)
coloured = Image.merge('HSV', (Hue, Sat, brightness)).convert('RGB')
coloured.save('DEBUG-coloured.png')
bg = Image.new('RGB', size=im.size, color='white')
bg.paste(coloured, mask =im)
bg.save('result.png')
Or making a desaturated blue - change 128 to 255 to make vivid and saturated:
from PIL import Image, ImageOps
im = Image.open('cross.png')
brightness = im.convert('L')
# Set Hue angle to 240 degrees scaled to unsigned char range
Hue = Image.new('L', size=im.size, color=int(240*255/360))
# Set Saturation to 50%, not too vivid
Sat = Image.new('L', size=im.size, color=128)
coloured = Image.merge('HSV', (Hue, Sat, brightness)).convert('RGB')
coloured.save('DEBUG-coloured.png')
bg = Image.new('RGB', size=im.size, color='white')
bg.paste(coloured, mask =im)
bg.save('result.png')