Skip to content

Python extension which provides a fast implementation of Adobe Photoshop's blend modes

License

Notifications You must be signed in to change notification settings

psd-tools/image-blender

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

image-blender

image-blender is 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 for psd-tools package back in 2015, but ended up in release hell as I've lost an inspiration.

Development status PyPI version

Usage

image-blender is 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. Pillow or pymaging).

There are some requirements that should be met to apply a blend function to a pair of images:

  1. Blend functions work with bytes, so you must pass a raw image data into them;
  2. Both images must be in RGBA mode and have the same size;
  3. 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:

 1 from PIL import Image
 2 import image_blender
 3 
 4 def apply_opacity(im, opacity):
 5     if opacity == 255:
 6         return im
 7 
 8     alpha_index = len(im.mode) - 1
 9     a = im.split()[alpha_index]
10     opacity_scale = opacity / 255
11     a = a.point(lambda i: i * opacity_scale)
12     im.putalpha(a)
13     return im
14 
15 image_bottom = Image.open("image1.png")
16 image_top = Image.open("image2.png")
17 
18 opacity = 200

The above function applies a constant opacity to an image with existing alpha channel. Now let's go to the examples:

  • Blend two images using Normal mode with some opacity:

    20 # apply opacity to the top image first...
    21 image_top = apply_opacity(image_top, opacity)
    22 # ... then simply alpha-composite them, no blend function is needed...
    23 result = Image.alpha_composite(image_bottom, image_top)
    24 
    25 result.save("normal_with_opacity.png")
  • Blend two images using Multiply mode (without opacity):

    20 # apply a blend function to a raw image data...
    21 tmp_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.
    24 tmp_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!
    29 result = Image.alpha_composite(image_bottom, tmp_top)
    30 
    31 result.save("multiply.png")
  • Blend two images using Multiply mode with some opacity:

    20 # simply combine the above examples...
    21 image_top = apply_opacity(image_top, opacity)
    22 tmp_top_bytes = image_blender.multiply(image_bottom.tobytes(), image_top.tobytes())
    23 tmp_top = Image.frombytes("RGBA", image_top.size, tmp_top_bytes)
    24 result = Image.alpha_composite(image_bottom, tmp_top)
    25 
    26 result.save("multiply_with_opacity.png")
  • Blend two images using Dissolve mode with some opacity:

    20 image_top = apply_opacity(image_top, opacity)
    21 result_bytes = image_blender.dissolve(image_bottom.tobytes(), image_top.tobytes())
    22 result = 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!
    25 
    26 result.save("dissolve_with_opacity.png")

License

Copyright 2015-2023 Evgeny Kopylov. Licensed under the MIT License.