'How to create an overlay for an image and its segmented mask?

I am trying to create an overlay with an image and its corresponding mask. Here is my code:

test_mask_list  = glob("output_images/austin1_0_10.tif")
testset = glob("train/dataset/test/images/austin1_0_10.tif")

    for i in range(len(testset)):
        image_name = testset[i]
        mask_name = test_mask_list[i]
        basename = os.path.basename(image_name)
        image = Image.open(image_name)
        mask = Image.open(mask_name)
        blended = Image.blend(image, mask, alpha=0.5)
        blended.save(overlay+basename)

The size of the image and its mask are the same which is 256X256. But still it is showing me error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_14688/3059464142.py in <module>
     10     image = Image.open(image_name)
     11     mask = Image.open(mask_name)
---> 12     blended = Image.blend(image, mask, alpha=0.5)
     13     blended.save(overlay+basename)

~\Anaconda3\envs\torchnet\lib\site-packages\PIL\Image.py in blend(im1, im2, alpha)
   3072     im1.load()
   3073     im2.load()
-> 3074     return im1._new(core.blend(im1.im, im2.im, alpha))
   3075 
   3076 

ValueError: images do not match 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source