Python Detect and Remove Image Alpha Channel with ImageMagick Wand – Python Wand Tutorial

By | July 22, 2019

To remove image alpha channel, we can use ImageMagick application.

A Simple Guide to Remove Image Alpha Channel with ImageMagick in Win 10 – ImageMagick Tutorial

However, this way is hard to use in python application. In this tutorial, we will introduce how to detect and remove image alpha channel in python application.

Preliminaries

1.Install ImageMagick Dll Version

2.install python wand

pip install Wand

pip install Wand

Detect an image contanis alpha channel or not

1.Load library

import wand.image

2.Define a funtion to detect

def hasAlpha(image_path):
    with wand.image.Image(filename=image_path) as img:
        #print(dir(img))
        alpha = img.alpha_channel
        return alpha

If return True, this image contains an alpha channel.

Remove image alpha channel

1.Define a function to remove

def removeAlpha(image_path, new_image_path):
    with wand.image.Image(filename=image_path) as img:
        img.alpha_channel = 'remove' #close alpha channel   
        img.background_color = wand.image.Color('white')
        img.save(filename=new_image_path)

Then all has been done.

Leave a Reply

Your email address will not be published. Required fields are marked *