Content

Written by Esben Elfving   
Wednesday, 23 January 2013 19:21

Working with clipping paths, alpha channels and transparency

This article shows how to work with clipping paths using version 2.6 of the MDKGraphicsEngine (MDK) Imaging SDK. A clipping path will be used to do a cut-out of an image which will be saved with a transparent background. A different technique is also shown where an image is first created with a transparent background from a clipping path and then used as a layer mask on a saved PSD image.

For all sample code used in this article error handling, color space and dimension checks are left out for simplicity. These checks should be added to the code for safe execution in real world applications. Sample code in this article uses C# syntax but MDK supports all managed .NET languages and direct unmanaged C/C++.

The samples will be using the following two images. The first image includes an embedded clipping path and the second image is used as background in the last sample where a clipping path is first converted to an alpha channel and then the channel is used to create a layer mask.

 

The first example shows how to use an embedded clipping path and using this path to do a cut-out of the image. The resulting image is a JPG with the cut-out part of the image set to light grey. The cut-out of the image is done by ApplyClippingPath which if successful strips the picture of all embedded clipping paths. The JPG format which is used for output supports embedded clipping paths but no clipping paths are transferred to the ClippingCutOut.jpg image

// enable anti-aliased edges
MDK.GraphicsEngine.GetSystemProperties().SetBooleanProperty(MDK.BooleanProperty.AntiAliasClippingPathEdges, true);
// first we open the image has an embedded clipping path
MDK.Image image = new MDK.Image(@"media03_clip.tif", MDK.GraphicsEngine.GetFileformat(@"media03_clip.tif"));
// make the cut-out and use a gray background RGB(192,192,192)
image.ApplyClippingPath(image.GetClippingPath(0), MDK.Pixel.FromRGB(192, 192, 192));
//save the image to a JPG
image.SaveToFile(@"ClippingCutOut.jpg", MDK.FileFormat.JPG);
//dispose the image
image.Dispose();

The result looks like this:

 

The second example does the same cut-out as the first but saves the resulting image as a PNG image with the background made transparent.

// enable anti-aliased edges
MDK.GraphicsEngine.GetSystemProperties().SetBooleanProperty(MDK.BooleanProperty.AntiAliasClippingPathEdges, true);
// first we open the image has an embedded clipping path
MDK.Image image = new MDK.Image(@"media03_clip.tif", MDK.GraphicsEngine.GetFileformat(@"media03_clip.tif"));
// make the cut-out and use a gray background RGB(192,192,192)
image.ApplyClippingPath(image.GetClippingPath(0), MDK.Pixel.FromRGB(192, 192, 192));
//now enable transparency 'ApplyClippingPath' changes the default background color to (192,192,192)
image.GetProperties().SetBooleanProperty(MDK.BooleanProperty.TransparentBackground, true);
//save the image to a PNG with background color (192,192,192) transparent
image.SaveToFile(@"ClippingCutOutTransparent.png", MDK.FileFormat.PNG);
//dispose the image
image.Dispose();

The output PNG image from the above sample is the same image but stored as a PNG image with a transparent background.

 

The result of sample three is the same as for the first sample but instead of using the ApplyClippingPath API call the new ConvertClippingPathToAlphaChannel is used. By first converting a clipping path to an alpha channel and then using this newly created channel as a base for the SetMatteColor API call the embedded clipping paths are transferred to the output image as long as the output format supports clipping paths.

// enable anti-aliased edges
MDK.GraphicsEngine.GetSystemProperties().SetBooleanProperty(MDK.BooleanProperty.AntiAliasClippingPathEdges, true);
// first we open the image which has an embedded clipping path
MDK.Image image = new MDK.Image(@"media03_clip.tif", MDK.GraphicsEngine.GetFileformat(@"media03_clip.tif"));
// convert the first clipping path to an alpha channel
image.ConvertClippingPathToAlphaChannel(image.GetClippingPath(0),false);
// we are saving as jpg so use the newly created alpha channel to set the matte color to RGB(192,192,192)
image.SetMatteColor(0, MDK.Pixel.FromRGB(192, 192, 192));
//save the image to a JPG
image.SaveToFile(@"ClippingPathToAlpha.jpg", MDK.FileFormat.JPG);
// dispose the image
image.Dispose();

Result:

 

The last sample shows how to first convert an embedded clipping path to a transparency alpha channel and then use the image with transparent background as an extra layer on another image.

// enable anti-aliased edges
MDK.GraphicsEngine.GetSystemProperties().SetBooleanProperty(MDK.BooleanProperty.AntiAliasClippingPathEdges, true);
//Enable support for PSD layers
MDK.GraphicsEngine.GetSystemProperties().SetBooleanProperty(MDK.BooleanProperty.WritePhotoshopLayerData, true);
// We need transparency support for this sample
MDK.GraphicsEngine.GetSystemProperties().SetBooleanProperty(MDK.BooleanProperty.TransparentBackground, true);
// open the background image
MDK.Image backgroundImage = new MDK.Image(@"media00.tif", MDK.GraphicsEngine.GetFileformat(@"media00.tif"));
// next we open the image has an embedded clipping path
MDK.Image image = new MDK.Image(@"media03_clip.tif", MDK.GraphicsEngine.GetFileformat(@"media03_clip.tif"));
// convert the first clipping path to an alpha channel. Second parameter set to true creates
// a special transparency alpha channel and not a "normal" alpha channel
image.ConvertClippingPathToAlphaChannel(image.GetClippingPath(0), true);
// add the image as a layer to the background image
// Since the clipping path is converted to a transparent alpha channel it is automatically converted to a 
// layer mask when the image is added as a layer.
backgroundImage.GetMetadata().AddLayerFromImage(image);
// dispose the clipping path image as it is not needed anymore
image.Dispose();
//save the image to a PSD
backgroundImage.SaveToFile(@"ClippingPathToLayerMask.psd", MDK.FileFormat.PSD);
// dispose the background image
backgroundImage.Dispose();

The resulting PSD file can be downloaded here

The composite part of the output PSD file saved as a PNG image looks like this:

[the end..]

Last Updated on Saturday, 08 June 2013 15:30
 
 
Copyright © 2013, Middleware Denmark ApS