Python Jes Help in modifying function in order to incorporat
Python Jes- Help in modifying function in order to incorporate overlapAmount into the parameters .
the first line should resemble def blendedPicture(pic1,pic2,overlapAmount):
The idea here is to create a more general version of the blendPictures function that can be used with any two pictures. By using parameters you will be able to call the function from the command area, and pass any two pictures that you would like to blend, and you will also be able to specify the amount of overlap (in pixels) between the two pictures.
def blendedPicture(pic1,pic2):
width=getWidth(pic1)
height=getHeight(pic1)
blendedPic=makeEmptyPicture(width,height)
for y in xrange(height):
for x in xrange(width):
affectedpixel=getPixel(blendedPic,x,y)
pixel_1=getPixel(pic1,x,y)
pixel_2=getPixel(pic2,x,y)
Red1=getRed(pixel_1)
Green1=getGreen(pixel_1)
Blue1=getBlue(pixel_1)
Red2=getRed(pixel_2)
Green2=getGreen(pixel_2)
Blue2=getBlue(pixel_2)
blendedColor=makeColor((Red1+Red2)/2,(Green1+Green2)/2,(Blue1+Blue2)/2)
setColor(affectedpixel, blendedColor)
return blendedPic
Solution
Since you have not given enough information about the pic1, pic2 parameters.
Also, you have not provided the method signatures for all the methods such as getPixel(), getRed(), getGreen(), getBlue(), makeColor()
So, what I suppose you need is to find if the two pics overlap or not.
You are iterating it correct using nested loops for x and y and calculating the respective values for pixels.
In the end you should not return anything.
def blendedPicture(pic1,pic2):
width=getWidth(pic1)
height=getHeight(pic1)
blendedPic=makeEmptyPicture(width,height)
for y in xrange(height):
for x in xrange(width):
affectedpixel=getPixel(blendedPic,x,y)
pixel_1=getPixel(pic1,x,y)
pixel_2=getPixel(pic2,x,y)
Red1=getRed(pixel_1)
Green1=getGreen(pixel_1)
Blue1=getBlue(pixel_1)
Red2=getRed(pixel_2)
Green2=getGreen(pixel_2)
Blue2=getBlue(pixel_2)
blendedColor=makeColor((Red1+Red2)/2,(Green1+Green2)/2,(Blue1+Blue2)/2)
setColor(affectedpixel, blendedColor)
Or you can use PIL library. I have written a small script, may be of some use to you.
from PIL import Image
from PIL import ImageChops
from PIL import ImageOps
import numpy as np
a = Image.open(\"pic1.png\")
b = Image.open(\"pic2.png\")
# Finds the difference between the two images. Then inverts so differences are black.
diff = ImageOps.invert(ImageChops.difference(pic1, pic2))
# Then we use numpy to convert black to red.
image = diff.convert(\'RGBA\')
data = np.array(image)
r1,g1,b1 = 0,0,0
r2,g2,b2 = 255,0,0
red, green, blue = data[:,:,0], data[:,:,1], data[:,:,2]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:3][mask] = [r2, g2, b2]
# Then we convert the array back to an image.
overlay = Image.fromarray(data)
# And blend with the original.
imgblend = ImageChops.add(pic1.convert(\'RGBA\'), overlay,2)
imgblend.show()

