Douglas Heydt
Douglas Heydt

Reputation: 13

How to blend images in python (without blend method)

this is an assignment on my class, so i need to blend to images together with python using interpolation but i am missing something, perhaps you can help me understand what.

Heres my code so far:

from PIL import Image
import numpy as np
image_one=Image.open('capone.pgm')
image_two=Image.open('escobar.pgm')

out=Image.new(image_one.mode, image_two.size)

(l,h)=image_one.size
for j in range(0, h):
    for i in range(0, l):
          out.getpixel((i,j)),(image_one.getpixel((i,j)) * (1.0 - 0.3) +  image_two.getpixel((i,j)) * 0.3 )

out.save("testaando.jpg","JPEG")
out.show()

0.3 is the alpha i want for the blending the two original images are sime size and mode

Upvotes: 0

Views: 933

Answers (2)

Mika
Mika

Reputation: 243

getpixel method of PIL.Image returns value of a pixel, but to modify it you need to use putpixel method. So instead of

out.getpixel((i,j)),(image_one.getpixel((i,j)) * (1.0 - 0.3) +  image_two.getpixel((i,j)) * 0.3 )

use

out.putpixel((i,j), (image_one.getpixel((i,j)) * (1.0 - 0.3) +  image_two.getpixel((i,j)) * 0.3 ))

Upvotes: 1

Mailerdaimon
Mailerdaimon

Reputation: 6090

This is just a guess as there currently is not much information.

The Line:

out.getpixel((i,j)),(image_one.getpixel((i,j)) * (1.0 - 0.3) +  image_two.getpixel((i,j)) * 0.3 )

Should be:

out[i, j] = (image_one.getpixel((i,j)) * (1.0 - 0.3) +  Image_two.getpixel((i,j)) * 0.3 )

Upvotes: 0

Related Questions