Package: image-blender
image-blenderis a Python extension which provides a fast implementation of
Adobe Photoshop’s blend modes. It is written using Cython. It was supposed to be
a helper module forpsd-toolspackage back in 2015, but ended up in release hell
as I’ve lost an inspiration.Usageimage-blenderis not a complete solution, it only provides you with blend
functions themselves, so you can blend two images together. You should use some
additional Python package to work with images and alpha-composite them (e.g.Pilloworpymaging).There are some requirements that should be met to apply a blend function to a
pair of images:Blend functions work with bytes, so you must pass a raw image data into them;Both images must be inRGBAmode and have the same size;Both images must have a bit depth of 32 bits (8 bits per channel).Let’s take a look at some use cases, but first let’s define one helper
function and make some preparations. From now on it’s assumed you’re using
Pillow and all of the above requirements are already met:1fromPILimportImage2importimage_blender34defapply_opacity(im,opacity):5ifopacity==255:6returnim78alpha_index=len(im.mode)-19a=im.split()[alpha_index]10opacity_scale=opacity/25511a=a.point(lambdai:i*opacity_scale)12im.putalpha(a)13returnim1415image_bottom=Image.open("image1.png")16image_top=Image.open("image2.png")1718opacity=200The above function applies a constant opacity to an image with existing
alpha channel. Now let’s go to the examples:Blend two images usingNormalmode with some opacity:20# apply opacity to the top image first...21image_top=apply_opacity(image_top,opacity)22# ... then simply alpha-composite them, no blend function is needed...23result=Image.alpha_composite(image_bottom,image_top)2425result.save("normal_with_opacity.png")Blend two images usingMultiplymode (without opacity):20# apply a blend function to a raw image data...21tmp_top_bytes=image_blender.multiply(image_bottom.tobytes(),image_top.tobytes())22# ... then create a new Image object from the resulting data...23# Note: Images' sizes are the same.24tmp_top=Image.frombytes("RGBA",image_top.size,tmp_top_bytes)25# ... finally, alpha-composite a new top image with a bottom one...26#27# Note: In these examples images have an alpha channel.28#       That's why we still need to alpha-composite them!29result=Image.alpha_composite(image_bottom,tmp_top)3031result.save("multiply.png")Blend two images usingMultiplymode with some opacity:20# simply combine the above examples...21image_top=apply_opacity(image_top,opacity)22tmp_top_bytes=image_blender.multiply(image_bottom.tobytes(),image_top.tobytes())23tmp_top=Image.frombytes("RGBA",image_top.size,tmp_top_bytes)24result=Image.alpha_composite(image_bottom,tmp_top)2526result.save("multiply_with_opacity.png")Blend two images usingDissolvemode with some opacity:20image_top=apply_opacity(image_top,opacity)21result_bytes=image_blender.dissolve(image_bottom.tobytes(),image_top.tobytes())22result=Image.frombytes("RGBA",image_top.size,result_bytes)23# This one is a bit different here:24# you should NOT alpha-composite the images when using Dissolve mode!2526result.save("dissolve_with_opacity.png")LicenseCopyright 2015-2023 Evgeny Kopylov. Licensed under theMIT License.