Git Product home page Git Product logo

image-blender's Introduction

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.

image-blender's People

Contributors

evgenko423 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

image-blender's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.