I am using python and PIL to manipulate two images. I have successfully placed one image onto another by using getpixel and putpixel. We are not allowed to use any copy/paste function that pil provides(hence the getpixel and putpixel). Now I'm basically trying to place the first image (let's say the template image) onto a user-defined location of the destination image. I know how to accept the user input, but I can't figure out where to put these variables to make the template image appear at the user's coordinates. I basically want to make these coordinates the new origin for the template image. I tried using the coordinates as the putpixel x and y but that, I think, is just stacking the pixels on top of each other at the user's coordinates. I'm sure it's something simple I'm missing. Any help would be appreciated. I'm using Python 2.7 by the way.
from PIL import Image
import sys
print "Image Manipulation\n"
tempImg = Image.open("template.png")
destImg = Image.open("destination.jpg")
tempWidth,tempHeight = tempImg.size
destWidth,destHeight = destImg.size
if tempWidth >= destWidth and tempHeight >= destHeight:
print "Error! The template image is larger than the destination image."
sys.exit()
else:
print "The template image width and height: ",tempWidth,tempHeight
print "The destination image width and height: ",destWidth,destHeight
x_loc = raw_input("Enter the X coordinate: ")
y_loc = raw_input("Enter the Y coordinate: ")
x_loc = int(x_loc) # <--where do I put these?
y_loc = int(y_loc)
tempImg = tempImg.convert("RGBA")
destImg = destImg.convert("RGBA")
img = tempImg.load()
for x in xrange(tempWidth):
for y in xrange(tempHeight):
if img[x,y][1] > img[x,y][0] + img[x,y][2]:
img[x,y] = (255,255,255,0)
else:
destImg.putpixel((x,y),tempImg.getpixel((x,y)))
destImg.show()
I figured it out! After a lot of playing around with the variables, I figured out where to add them. I kept trying to use them as the x,y coordinates of the getpixel function, but I knew that would just stack them on top of each other basically, because these variables were not being updated in the loop.
getpixel((x_loc,y_loc),...) #<--wrong
Then it clicked: I just had to add the user's coordinates to the x and y of getpixel. For instance:
getpixel((x+x_loc,y+y_loc),...) #<--Eureka!!
This basically sets the origin of the template image to the user's input location. I know there are other ways of doing this much faster, but we were instructed to only use getpixel, putpixel, and open/save image. Of course, here I'm just displaying the image instead of saving it. Feel free to try it out.
print "Image Manipulation\n"
template = raw_input("Enter the template image: ")
destination = raw_input("Enter the destination image: ")
tempImg = Image.open(template)
destImg = Image.open(destination)
tempWidth,tempHeight = tempImg.size
destWidth,destHeight = destImg.size
if tempWidth >= destWidth and tempHeight >= destHeight:
print "Error! The template image is larger than the destination image."
sys.exit()
else:
print "The template image width and height: ",tempWidth,tempHeight
print "The destination image width and height: ",destWidth,destHeight
x_loc = raw_input("Enter the X coordinate: ")
y_loc = raw_input("Enter the Y coordinate: ")
x_loc = int(x_loc)
y_loc = int(y_loc)
tempImg = tempImg.convert("RGBA")
destImg = destImg.convert("RGBA")
img = tempImg.load()
for x in range(tempWidth):
for y in range(tempHeight):
if img[x,y][1] > img[x,y][0] + img[x,y][2]: #<-- removes green border from image
img[x,y] = (255,255,255,0)
else:
destImg.putpixel((x+x_loc,y+y_loc),tempImg.getpixel((x,y)))
destImg.show()
**NOTE: This part of the program is optional:
if img[x,y][1] > img[x,y][0] + img[x,y][2]:
img[x,y] = (255,255,255,0)
The template picture we were given has a green border around it and we were asked to remove it when we place it onto the destination image.