Remove Alpha Channel in an Image

Remove alpha channel in an image

Assuming you don't have another image editor, then you can open it in Preview on your Mac, and use the Export option to resave it in a different format- to ensure you get rid of the alpha channel, it might be best to export to JPG (best quality), then open that and export it as a PNG again.

Having said that, I suspect you're probably OK submitting an icon with a transparency channel as long as there's no actual transparency.

Imagemagick: remove alpha component (replace all intermediate alpha pixel with solid pixel)

To remove the alpha channel from single image use this command:

convert input.png -alpha off output.png

To remove the alpha channel from all images inside a folder, make use find to first find all PNG files, and then run 'm through convert:

find . -name "*.png" -exec convert "{}" -alpha off "{}" \;

Please test on a COPY of your files to be sure.

...

see dialog below, and the answer is based on that "we need to remove alpha that is not 255"

convert input.png -channel A -threshold 254 output.png

and for batch

mkdir batch
FOR %G IN (*.png) DO convert %G -channel A -threshold 254 batch\%G

Removing alpha channels from grayscale images

You can try out this.

import cv2

image = cv2.imread('path to your image')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

cv2.imshow('Gray image', gray)

cv2.waitKey(0)
cv2.destroyAllWindows()

for multiple images

import cv2
from os import listdir,makedirs
from os.path import isfile,join

source = r'path to source folder'
destination = r'path where you want to save'

files = [f for f in listdir(source) if isfile(join(source,f))]

for image in files:
try:
img = cv2.imread(os.path.join(source,image))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dstPath = join(destination,image)
cv2.imwrite(destination,gray)
except:
print ("{} is not converted".format(image))

Stripping Alpha Channel from Images

Thanks to Rob's answer, we now know why the colors are messed up.

The problem is twofold:

  • The default JPEGImageWriter that ImageIO uses to write JPEG, does not write JPEGs with alpha in a way other software understands (this is a known issue).
  • When passing null as the destination to ResampleOp.filter(src, dest) and the filter method is FILTER_TRIANGLE, a new BufferedImage will be created, with alpha (actually, BufferedImage.TYPE_INT_ARGB).

Stripping out the alpha after resampling will work. However, there is another approach that is likely to be faster and save some memory. That is, instead of passing a null destination, pass a BufferedImage of the appropriate size and type:

public static void main(String[] args) throws IOException {
// Read input
File input = new File(args[0]);
BufferedImage inputImage = ImageIO.read(input);

// Make any transparent parts white
if (inputImage.getTransparency() == Transparency.TRANSLUCENT) {
// NOTE: For BITMASK images, the color model is likely IndexColorModel,
// and this model will contain the "real" color of the transparent parts
// which is likely a better fit than unconditionally setting it to white.

// Fill background with white
Graphics2D graphics = inputImage.createGraphics();
try {
graphics.setComposite(AlphaComposite.DstOver); // Set composite rules to paint "behind"
graphics.setPaint(Color.WHITE);
graphics.fillRect(0, 0, inputImage.getWidth(), inputImage.getHeight());
}
finally {
graphics.dispose();
}
}

// Resample to fixed size
int width = 100;
int height = 100;

BufferedImageOp resampler = new ResampleOp(width, height, ResampleOp.FILTER_TRIANGLE);

// Using explicit destination, resizedImg will be of TYPE_INT_RGB
BufferedImage resizedImg = resampler.filter(inputImage, new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB));

// Write output as JPEG
ImageIO.write(resizedImg, "JPEG", new File(input.getParent(), input.getName().replace('.', '_') + ".jpg"));
}

Python PIL remove every alpha channel completely

You question is kind of misleading as You stated:-

I want to convert it to 8bit Bitmap and colour every invisible(alpha) pixels to purple(#FF00FF) and set them to dot zero. (very first palette)

But in the description you gave an input image having no alpha channel. Luckily, I have seen your previous question Convert PNG to 8 bit bitmap, therefore I obtained the image containing alpha (that you mentioned in the description) but didn't posted.

HERE IS THE IMAGE WITH ALPHA:-

Sample Image

Now we have to obtain .bmp equivalent of this image, in P mode.

from PIL import Image

image = Image.open(r"Image_loc")

new_img = Image.new("RGB", (image.size[0],image.size[1]), (255, 0, 255))

cmp_img = Image.composite(image, new_img, image).quantize(colors=256, method=2)

cmp_img.save("Destination_path.bmp")

OUTPUT IMAGE:-

Sample Image

Discarding alpha channel from images stored as Numpy arrays

Just slice the array to get the first three entries of the last dimension:

image_without_alpha = image[:,:,:3]


Related Topics



Leave a reply



Submit