Some pdf pages turn out black due to alpha channel
Some pdf pages converted by watermark.py get an entirely black background in the current state of the script. Removing the transparency alpha channel worked out for me:
def watermark_img(img_file, template, dpi): """Watermarks image with watermark template
Args:
img_file (str): path to image file
template (PIL.Image.Image): watermark template
dpi (int): dots per inch
Returns:
str: path to watermarked image
"""
# Open image
image = Image.open(img_file).convert('RGBA')
image = remove_transparency(image) # <------------------ Add this line in watermark_img call
width, height = image.size
[...]
def remove_transparency(im, bg_colour=(255, 255, 255)): # <------------------ Add this function
# Only process if image has transparency
if im.mode in ('RGBA', 'LA') or (im.mode == 'P' and 'transparency' in im.info):
# Need to convert to RGBA if LA format due to a bug in PIL
alpha = im.convert('RGBA').split()[-1]
# Create a new background image of our matt color.
# Must be RGBA because paste requires both images have the same format
bg = Image.new("RGBA", im.size, bg_colour + (255,))
bg.paste(im, mask=alpha)
return bg
else:
return im