1 Replies - 7288 Views - Last Post: 12 October 2012 - 06:40 PM Rate Topic: -----

#1 kehara15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-September 12

Morphing two images in python

Posted 11 October 2012 - 06:13 PM

I am working on a code and I am a little confused at the instructions.

These are the instructions that I am to go by:

PIL Library contains a function called blend:

Image.blend(image1, image2, alpha)
Returns: Image

that creates a new image by morphing image1 and image2, using a constant alpha.

outImage = image1 * (1.0 - alpha) + image2 * alpha

If alpha is 0.0, a copy of the first image is returned. If alpha is 1.0, a copy of the second image is returned. There are no restrictions on the alpha value. If necessary, the result is clipped to fit into the allowed output range.

Note: Both images must have the same size.

In this task, you will create a function called morphPicture that has two arguments representing the file names of image1 and image2. Your function will use a for loop to call Image.blend with the following alpha values: 0, 1/10, 2/10,...1

Your function then saves and displays the result image for each alpha.

The name of the files created by your function must have the following format: “morph”+str(k)+”.gif”. Note that (k) means the value of the variable that controls the for loop.

So far this is what I think it is asking for but not sure if I am going in the right direction:

def morphPicture(filename,filename1):
    myImage = Image.open(filename)
    myImage1 = Image.open(filename1)

    for k in range(alpha)
        Image.blend(filename, filename1, alpha)
        outImage = filename * (1.0 - alpha) + filename1 * alpha

    newfilename = filename[:-4]
    newfilename = "morph"+str(k)+".gif"
    newImage = Image.open(newfilename)

Is This A Good Question/Topic? 0
  • +

Replies To: Morphing two images in python

#2 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 698
  • Joined: 19-January 09

Re: Morphing two images in python

Posted 12 October 2012 - 06:40 PM


I had a look over the question and your code and I think that your for loop might be slightly wrong, but otherwise you seem to be going in the right direction.
In your loop, you are using the variable alpha to control how many times the loop goes. You are also using alpha to subtract to make the image name. Through this method, you are always going to get the same alpha value, from your question it sounds like you want the alpha values to change though. So I think maybe using a list of alpha values and using k to go through that list and then to get the number of times the loop has to loop, you can just count the number of items in alpha. I am not sure if you wanted some code to help, or just an explanation, so I will put this in spoiler tags and you can decide if you want to see it or not :) .

Note: That code is just making the changes that I just explained, nothing else.

Hopefully that will help you!

This post has been edited by Eric115: 12 October 2012 - 06:42 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1