- Download repo
- Run blending.py file
- You will see the results of alpha blending and blending with changing the position of two images.
Math behind alpha blending C = d*A + (1-d)*B
Where C is a new image made of blending B image over A with transparency d
Here we create an image of size 100 x 200. Furthermore, each pixel has RGBA values, thats why 3rd dimention is of size 4.
image_1 = np.zeros([100, 200, 4], dtype=np.uint8)
By default, our image has all RGBA values equal to zero, so it is white and fully transparent. To change its colour we change the RGBA values.
image_1[:, :100] = [255, 128, 0, 255] #Orange left side
image_1[:, 100:] = [0, 0, 255, 255] #Blue right side
Here, the dransparency is equal to 255, so the image will be 100% nontransparent.
We created two images for further blending:
For alpha blending, we need to have so called transparency (alpha) coeffitient d. This coeffitient determines the transparency of the images that we blend.
d = 0.5
Next, we are using the formula: C = d*A + (1-d)*B to blend the two images (image_1 and image_2) together. image_3, a resulting image was initialized in the same way as image_1.
for x in range(200):
for y in range(100):
image_3[y, x] = d * image_2[y, x] + (1 - d) * image_1[y, x]
We pixel by pixel using two for loops and give the image_3 pixel value of blended pixel from image_1 and image_2.
The result (image_3) of blending the created images:
For the purpose of shifting and blending we created a background - image_4 of size 300 x 300. It was initialized in the same way as all previous images and it is fully transparent. Then, we define the coordinates x and y (the left up corner) of both images that we will blend, and their blend transparency.
#first image coordinates
x1_coord = 10
y1_coord = 80
#transparency
d_1 = 0.9
After that, we perform the blending of image_4 (our background) with image_1, again using two for loops to go pixel by pixel. Notice that we start the iterations from the coordinates that we defined above, not from 0, so our image will be placed just where we want it to be. We assign the value of a pixel of image_1 to corresponding pixel of image_4. Since the background is white and fully transparent, we can ignore it in the blending equation. The if statement cuts out the excess of image_1, if it is placed too far away to fit within the background.
for x in range(x1_coord,200+x1_coord):
for y in range(y1_coord,100+y1_coord):
if(x < 300 and y <300):
image_4[y, x] = d_1*image_1[y - y1_coord, x - x1_coord]
So now, image_4 is image_1 blended with the background. We want now to blend in the second picture and we do it in exactly same way as before. Here, the image_4 is already after the first blending, so it's not an empty background, but image_1 is on it. That is why we have to have the second component in our blending equation.
for x in range(x2_coord,200+x2_coord):
for y in range(y2_coord,100+y2_coord):
if(x < 300 and y <300):
image_4[y, x] = d_2*image_2[y - y2_coord, x - x2_coord]+(1-d_2)*image_4[y, x]
The result of shifting and blending the two created pictures:
We used following code to convert the picture from array to png and save it.
img = Image.fromarray(image_1)
img.save('first_image.png')
It uses function from PIL package.
To use existing image we have to convert it first to a RGBA table. We do it, using a function from the numpy package.
image_5 = np.array(Image.open('flower.jpg'))
Although the mechanism behind blending two imported images is the same, in the range function as argument we put a numpy fuction that gives tupple of array dimentions of our image.
for x in range(np.shape(image_5)[1]):
for y in range(np.shape(image_5)[0]):
image_5[y, x] = d * image_5[y, x] + (1 - d) * image_6[y, x]
As an example we blended the following two pictures together:
The result is as follows:
Converting equation to integers
for x in range(200):
for y in range(100):
image_clone_1[y, x] = image_1[y, x]
image_clone_2[y, x] = image_2[y, x]
image_3[y,x] = (alpha * image_clone_2[y, x] + (256 - alpha) * image_clone_1[y, x]) >> 8