0
def apply_alpha(img, alpha_value):
    print("alpha_value" + str(alpha_value))
    mask_value = int(alpha_value * 255)
    print("mask_value" + str(mask_value))
    img.putalpha(mask_value)
    return img

def apply_alpha(img, alpha_value):
    import copy
    tmp = copy.copy(img)
    print("alpha_value" + str(alpha_value))
    mask_value = int(alpha_value * 255)
    print("mask_value" + str(mask_value))
    tmp.putalpha(mask_value)
    return tmp

working_image = apply_alpha(obs, alpha)

I tried both of the above apply_alpha functions, where "img" is a PIL image, and neither of them correctly apply alpha (nothing changes).

I am stitching together individual tiles of a composite image, and using "put alpha" to set the transparency of each individual tile. I believe the 'paste' in the merging of the individual tiles is erasing the putalpha for each individual image. How can I get this to work?

I'm using this merge_images to stitch together the individual tile images: Stitching Photos together

This scenario is distinct from other questions asked because the img.putalpha(...) is used within a function, which causes it to not work

user3180
  • 1,369
  • 1
  • 21
  • 38
  • Can you give an example of your input parameters – ybl Jan 07 '19 at 07:12
  • Possible duplicate of [Python PIL: how to make area transparent in PNG?](https://stackoverflow.com/questions/4379978/python-pil-how-to-make-area-transparent-in-png) – buran Jan 07 '19 at 07:25
  • @buran It is precisely not a duplicate because using the "putalpha" syntax inside a function affecting the function parameter Does not work – user3180 Jan 07 '19 at 07:42
  • @SamakshJain obs is any image you load with PIL, and alpha is a scalar describing the alpha value of transparency – user3180 Jan 07 '19 at 07:42
  • Have you tried to draw an image inside the function to see if the operations you do really apply the alpha to an image? Maybe the operations are invalid and that's why you don't see any changes in the image outside the function. Also, I would recommend using the second function because it preserves the original value of `img` – Novak Jan 07 '19 at 08:00
  • @Novak Yes - within the function, I ran img.show() and saw the correct transparency applied – user3180 Jan 07 '19 at 08:08
  • Maybe outalpha is not a method but a function so you should use `tmp = tmp.putalpha(mask_value)` and then `return tmp` – Novak Jan 07 '19 at 08:09
  • @Novak No, because like I said it works within the function. Also, documentation and other questions support the idea that img.putalpha(...) mutates img not returns result – user3180 Jan 07 '19 at 08:11
  • if it mutates the image inside the function, try to show the same image outside the function. So, if you pass the `img` variable to the function, do the `img.putalpha(mask_value)` and `img.show()` inside the function and then without the `return` statement try `img.show()` outside the function. That should tell you if you're mutating the image or the error is in returning the value. – Novak Jan 07 '19 at 08:14
  • Actually, I believe the problem is because I am stitching together images using merge_images here: https://stackoverflow.com/questions/10657383/stitching-photos-together – user3180 Jan 07 '19 at 08:17
  • Does PIL paste() ignore the alpha from previous putalpha's? – user3180 Jan 07 '19 at 08:18
  • Yes - confirmed that the alpha is lost when merging... – user3180 Jan 07 '19 at 08:42

1 Answers1

0

I figured it out: the cause of the issue was that, in the merge function for the images, there is this code:

    result = Image.new('RGB', (result_width, result_height))
    result.paste(im=img1, box=(0, 0), mask=img1)
    result.paste(im=img2, box=(width1, 0), mask=img2)

Because the image type was "RGB", the alpha channels were being ignored when composing the tiles. Make sure the image type is "RGBA"

user3180
  • 1,369
  • 1
  • 21
  • 38