This tutorial shows how to create and save a transparent area in an image applying two different techniques in a Python application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to add transparency to images in a Python application. |
Completion Time | 20 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | Python Console Application |
IDE | Visual Studio 2022 |
Runtime Target | Python 3.10 or Higher |
Development License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Add Transparency to an Image - Python tutorial.
Start with a copy of the project created in the Add References and Set a License tutorial. If the project is not available, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project.
This tutorial requires the following DLLs:
Leadtools.dll
Leadtools.Codecs.dll
For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.
There are two types of runtime licenses:
With the project created, the references added, and the license set, coding can begin.
In the Solution Explorer, open Project-Name.py
and place the following references below the "Add references to LEADTOOLS" comment
# Add references to LEADTOOLS
from leadtools import LibraryLoader
LibraryLoader.add_reference("Leadtools")
from Leadtools import *
LibraryLoader.add_reference("Leadtools.Codecs")
from Leadtools.Codecs import *
from Leadtools.ImageProcessing import *
from System.IO import *
In the Project-Name.py
file add a new method called add_alpha_transparency()
that takes an image and sets all white and near-white areas as transparent by manipulating the image's alpha channel.
def add_alpha_transparency(input_image):
# create an 8-bit image to use later as Alpha channel
alpha = input_image.CreateAlphaImage()
# Fill the alpha channel with white to cause all pixels to be opaque
fill = FillCommand(RasterColor.White)
fill.Run(alpha)
# Find white and near-white pixels in the input image and create a region from them
color_low = RasterColor(245, 245, 245)
color_hi = RasterColor.White
input_image.AddColorRgbRangeToRegion(color_low, color_hi, RasterRegionCombineMode.Set)
# copy the region to the alpha image
alpha.SetRegion(None, input_image.GetRegion(None), RasterRegionCombineMode.Set)
input_image.MakeRegionEmpty()
# Fill the region with black to represent transparent pixels
fill.Color = RasterColor.Black
fill.Run(alpha)
output_image = input_image.Clone()
output_image.SetAlphaImage(alpha)
return output_image
Add a new method called add_transparent_color()
that takes an image and converts all white and near-white areas to pure white, then marks the white color as transparent.
def add_transparent_color(input_image):
output_image = input_image.Clone()
# Find white and near-white pixels in the input image and create a region from them
color_low = RasterColor(245, 245, 245)
color_hi = RasterColor.White
output_image.AddColorRgbRangeToRegion(color_low, color_hi, RasterRegionCombineMode.Set)
# Fill the region with a single white color to enable setting it as a unique transparent color
fill = FillCommand(RasterColor.White)
fill.Run(output_image)
output_image.Transparent = True
output_image.TransparentColor = RasterColor.White
output_image.MakeRegionEmpty()
return output_image
In the main()
method, load a base image to have transparency added to it and pass it to the 2 methods created above:
add_alpha_transparency()
, then save the resulting image as 32-bit PNG to store the transparency alpha channel with it.add_transparent_color()
, then save the resulting image either as PNG or GIF, since both formats support transparent color.# Add the below code to the `main()` method after the `Support.set_license()` method call
codecs = RasterCodecs()
image = codecs.Load(r"C:\LEADTOOLS22\Resources\Images\testframe1.jpg")
# Create a new image with a transparency alpha channel from near-white parts of the image
image_with_alpha = add_alpha_transparency(image)
# Save as 32-bit PNG to store alpha channel with image
codecs.Save(image_with_alpha, r"C:\LEADTOOLS22\Resources\Images\imagePlusAlpha.png", RasterImageFormat.Png, 32)
# Create a new image with a transparency alpha channel from near-white parts of the image
image_with_transparent_color = add_transparent_color(image)
# Save as 8-bit GIF to store image with transparent color
codecs.Save(image_with_transparent_color, r"C:\LEADTOOLS22\Resources\Images\imageWithTranspColor.gif", RasterImageFormat.Gif, 8)
To load the file using MemoryStream
, replace the code in the main()
method with the following:
def main():
Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS22/Support/Common/License"))
codecs = RasterCodecs()
filepath = r"C:\LEADTOOLS22\Resources\Images\testframe1.jpg"
bytes = File.ReadAllBytes(filepath)
ms = MemoryStream(bytes)
image = codecs.Load(ms)
#image = codecs.Load(r"C:\LEADTOOLS22\Resources\Images\testframe1.jpg")
# Create a new image with a transparency alpha channel from near-white parts of the image
image_with_alpha = add_alpha_transparency(image)
# Save as 32-bit PNG to store alpha channel with image
codecs.Save(image_with_alpha, r"C:\LEADTOOLS22\Resources\Images\imagePlusAlpha.png", RasterImageFormat.Png, 32)
# Create a new image with a transparency alpha channel from near-white parts of the image
image_with_transparent_color = add_transparent_color(image)
# Save as 8-bit GIF to store image with transparent color
codecs.Save(image_with_transparent_color, r"C:\LEADTOOLS22\Resources\Images\imageWithTranspColor.gif", RasterImageFormat.Gif, 8)
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps are followed correctly, the application runs and loads a JPEG image, adds transparency to it in two different ways and saves the resulting images with transparency to two different files.
This tutorial showed how to add the necessary references to produce images with transparency.