简体   繁体   中英

“Error: bad transparency mask” when converting PNG with alpha transparency to JPEG

I'm writing a short function that converts PNGs with alpha transparent background to JPEGs with white backgrounds. Despite the error, the code works by successfully finding all PNGs within a directory and saving JPEGs. Why am I getting this error?

The commented-out line of code shows a previous method I tried, which without running error converted PNGs to JPEGs using PIL's convert function. However it also converted alpha values to black, giving the JPEGs black backgrounds instead of white ones as desired.

from PIL import Image
import os, sys, fnmatch

path_in = 'path_in/'
dir = os.listdir(path_in)

for image in dir:
    #checks if image is png
    if fnmatch.fnmatch(image, '*.png'):
        png_image = Image.open(path_in + image)
        f, e = os.path.splitext(path_in + image)

        #creates new all-white jpg based on size of jpg
        jpg_image = Image.new('RGB', png_image.size, (255,255,255))
        #pastes png over jpg
        jpg_image.paste(png_image, (0,0), png_image)

        #code that made transparent backgrounds black, so not in use
        #jpg_image = jpg_image.convert('RGB')

        #saves as jpg to same path
        jpg_image.save(f + '.jpg', 'JPEG')

I want to know why this code is producing an error message despite functioning properly. Otherwise, I would like to use the simpler method of convert built into PIL , but converting alpha values of PNGs to white instead of black.

***Edit: It seems to be an issue with one particular PNG. This is where it gets interesting. The first image produces an error. The second and third images produce no errors. The second image is an edit of the first. The third image is the exact same as the first image, just opened in paint and saved as is. The code functioned properly iterating over all the PNG's in the directory up to this final PNG, which was the last in the directory. Thus it appeared to be functioning as all the other PNG's were successfully converted to JPEG's, but this final PNG would crash the function.

This is the PNG in question that produces the error.

This is the same PNG, with the logo deleted in paint. No errors.

This is the EXACT same PNG, with no editing, just opened in paint and saved as is. No errors.

Using convert() to change color mode from RGBA to RGB won't work, as a .png uses Non-Premultiplied Alpha . Due to which, when am image is converted from RGBA to RGB, the alpha channel get's removed from the original image, But the color value underneath the alpha still exist.

The code in the description, works for me (for some reason). But a plausible reason for the error could be the images being of different color spaces. So we can create the white background image using new() of color mode RGBA, and after pasting the original image over it, we can convert it back to RGB. What happens here is that after pasting the original image, the new image will have alpha values with white background having pixel values like (255, 255, 255, x) (where x could be an integer in range(256)). So after removing the alpha we end up with white background.

png_image = Image.open(path_in + image)

jpg_image = Image.new('RGBA', png_image.size, (255,255,255))

jpg_image.paste(png_image, (0,0), png_image)

jpg_image = jpg_image.convert("RGB")

If that doesn't work for you then you can try compositing the two images, using composite() .

jpg_image = Image.new('RGB', png_img.size, 'white')

jpg_image = Image.composite(png_image, new_img, png_image)

The output image of composite() would be of mode RGB, so you can save it as a .jpg .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM