Package: blend-modes
This Python package implements blend modes for images.DescriptionThe Blend Modes package enables blending different images, or image
layers, by means of blend modes. These modes are commonly found in
graphics programs likeAdobe
PhotoshoporGIMP.Blending through blend modes allows to mix images in a variety of ways.
This package currently supports the following blend modes (name of the
respective functions in the package initalics):Soft Light (blend_modes.soft_light)Lighten Only (blend_modes.lighten_only)Dodge (blend_modes.dodge)Addition (blend_modes.addition)Darken Only (blend_modes.darken_only)Multiply (blend_modes.multiply)Hard Light (blend_modes.hard_light)Difference (blend_modes.difference)Subtract (blend_modes.subtract)Grain Extract (known from GIMP,blend_modes.grain_extract)Grain Merge (known from GIMP,blend_modes.grain_merge)Divide (blend_modes.divide)Overlay (blend_modes.overlay)Normal (blend_modes.normal)The intensity of blending can be controlled by means of anopacityparameter that is passed into the functions. SeeUsagefor
more information.The Blend Modes package is optimized for speed. It takes advantage of
vectorization through Numpy. Further speedup can be achieved when
implementing the package in Cython. However, Cython implementation is
not part of this package.UsageThe blend mode functions take image data expressed as arrays as an
input. These image data are usually obtained through functions from
image processing packages. Two popular image processing packages in
Python arePILor its forkPillowandOpenCV. The examples in this chapter show how
to blend images using these packages.Input and Output FormatsA typical blend mode operation is called like this:blended_img=soft_light(bg_img,fg_img,opacity)The blend mode functions expectNumpyfloat arrays in the
format [pixels in dimension 1,pixels in dimension 2,4] as an
input. Both images needs to have the same size, so thepixels in
dimension 1must be the same forbg_imgandfg_img. Same
applies to thepixels in dimension 2. Thus, a valid shape of the
arrays would bebg_img.shape == (640,320,4)andfg_img.shape == (640,320,4).The order of the channels in the third dimension should beR, G, B, A,
whereAis the alpha channel. All values should befloatsin the
range0.0 <= value <= 255.0.The blend mode functions return arrays in the same format as the input
format.ExamplesThe following examples show how to use the Blend Modes package in
typical applications.The examples are structured in three parts:Load background and foreground image. The foreground image is to be
blended onto the background image.Use the Blend Modes package to blend the two images via the “soft
light” blend mode. The package supports multiple blend modes. See theDescriptionfor a full list.Display the blended image.PIL/Pillow ExampleThe following example shows how to use the Blend Modes package with thePILorPillowpackages.fromPILimportImageimportnumpyfromblend_modesimportsoft_light# Import background imagebackground_img_raw=Image.open('background.png')# RGBA imagebackground_img=numpy.array(background_img_raw)# Inputs to blend_modes need to be numpy arrays.background_img_float=background_img.astype(float)# Inputs to blend_modes need to be floats.# Import foreground imageforeground_img_raw=Image.open('foreground.png')# RGBA imageforeground_img=numpy.array(foreground_img_raw)# Inputs to blend_modes need to be numpy arrays.foreground_img_float=foreground_img.astype(float)# Inputs to blend_modes need to be floats.# Blend imagesopacity=0.7# The opacity of the foreground that is blended onto the background is 70 %.blended_img_float=soft_light(background_img_float,foreground_img_float,opacity)# Convert blended image back into PIL imageblended_img=numpy.uint8(blended_img_float)# Image needs to be converted back to uint8 type for PIL handling.blended_img_raw=Image.fromarray(blended_img)# Note that alpha channels are displayed in black by PIL by default.# This behavior is difficult to change (although possible).# If you have alpha channels in your images, then you should give# OpenCV a try.# Display blended imageblended_img_raw.show()OpenCV ExampleThe following example shows how to use the Blend Modes package withOpenCV.importcv2# import OpenCVimportnumpyfromblend_modesimportsoft_light# Import background imagebackground_img_float=cv2.imread('background.png',-1).astype(float)# Import foreground imageforeground_img_float=cv2.imread('foreground.png',-1).astype(float)# Blend imagesopacity=0.7# The opacity of the foreground that is blended onto the background is 70 %.blended_img_float=soft_light(background_img_float,foreground_img_float,opacity)# Display blended imageblended_img_uint8=blended_img_float.astype(numpy.uint8)# Convert image to OpenCV native display formatcv2.imshow('window',blended_img_uint8)cv2.waitKey()# Press a key to close window with the image.InstallationThe Blend Modes package can be installed through pip:$ pip install blend_modesDependenciesThe Blend Modes package needsNumpyto function correctly.
For loading images the following packages have been successfully used:PILPillowOpenCVSee AlsoBlend modes are further described onWikipedia. An actual
implementation can be found in theGIMP source
code, e.g.
in the file that describes thedivisionoperation,gimpoperationdividecode.c.ContributionI am happy about any contribution or feedback. Please let me know about
your comments via the Issues tab onGitHub.LicenseThe Blend Modes package is distributed under theMIT License
(MIT).
Please also take note of the licenses of the dependencies.