1

I have two images, one overlay and one background. I want to create a new image, by editing overlay image and manipulating it to show only the pixels which have blue colour in the background image. I dont want to add the background, it is only for selecting the pixels. Rest part should be transparent. Any hints or ideas please? PS: I edited result image with paint so its not perfect.

Image 1 is background image.

Image 2 is overlay image.

Image 3 is the check I want to perform. (to find out which pixels have blue in background and making remaining pixels transparent)

Image 4 is the result image after editing.

backgroundoverlaycheckresult

0

1 Answer 1

1

I renamed your images according to my way of thinking, so I took this as image.png:

enter image description here

and this as mask.png:

enter image description here

Then I did what I think you want as follows. I wrote it quite verbosely so you can see all the steps along the way:

#!/usr/local/bin/python3
from PIL import Image
import numpy as np

# Open input images
image = Image.open("image.png")
mask  = Image.open("mask.png")

# Get dimensions
h,w=image.size

# Resize mask to match image, taking care not to introduce new colours (Image.NEAREST)
mask = mask.resize((h,w), Image.NEAREST)    
mask.save('mask_resized.png')

# Convert both images to numpy equivalents
npimage = np.array(image)
npmask  = np.array(mask)

# Make image transparent where mask is not blue
# Blue pixels in mask seem to show up as RGB(163 204 255)
npimage[:,:,3] = np.where((npmask[:,:,0]<170) & (npmask[:,:,1]<210) & (npmask[:,:,2]>250),255,0).astype(np.uint8)

# Identify grey pixels in image, i.e. R=G=B, and make transparent also
RequalsG=np.where(npimage[:,:,0]==npimage[:,:,1],1,0)
RequalsB=np.where(npimage[:,:,0]==npimage[:,:,2],1,0)
grey=(RequalsG*RequalsB).astype(np.uint8)
npimage[:,:,3] *= 1-grey

# Convert numpy image to PIL image and save
PILrgba=Image.fromarray(npimage)
PILrgba.save("result.png")

And this is the result:

enter image description here


Notes:

a) Your image already has an (unused) alpha channel present.

b) Any lines starting:

npimage[:,:,3] = ...

are just modifying the 4th channel, i.e. the alpha/transparency channel of the image

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.