Skip to main content

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.

Metadata

Release files for image-blender 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for image-blender 0.1.1
File Size Uploaded
image-blender-0.1.1.tar.gz 41.7 kB Details

Release files / image-blender-0.1.1.tar.gz

Download URL image-blender-0.1.1.tar.gz
Size 41.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5169cc78a7c91b176055a612b41bb44eb6fa0704174eed8eba690947165400d1
BLAKE2b-256 checksum
How to use checksums
39c5b0f423e5c2a73c6b15cf92c4b2ad5a7a9fb9affe388e8b9ae09ea083f2b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.8.2 requests/2.21.0 setuptools/43.0.0 requests-toolbelt/1.0.0 tqdm/4.64.1 CPython/3.4.3

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page