H as my name suggests I am new to code, am working with Python and would really appreciate some help with using the Python Imaging Library to complete a task.
I have 2 bitmap images. The first image is of a color painting and the second contains just black and white pixels... I would like to develop a function that accepts two images as parameters to create a new image that is essentially the first image (the painting), with only the black pixels from the second image, overwritten on top of it. The second image is smaller than the first and the overwritten pixels need to be positioned in the center of the new image.
I have installed the PIL and have been able to use the following code to successfully display the first image using the following code:
import PIL
from PIL import Image
painting = Image.open("painting.bmp")
painting.show()
I have ruled out using .blend and .composite functions or the .multiply function from the ImageChops module, as the images are not the same size.
I believe I'll need to use either .getpixel / .putpixel or .getdata / .putdata to find the black pixels which are identified by tuple (0,0,0). Also PIL's crop and paste functions I'm thinking should help work out the 'region' from the painting to overwrite with the black pixels, and could help center the black pixels over the painting?
So i'm looking at something like this maybe...
def overwrite_black(image1, image2):
image_1 = Image.open('painting.bmp')
image_2 = Image.open('black_white.bmp')
pixels = list(image_2.getdata())
for y in xrange(image_2.size[1]):
for x in xrange(image_2.size[0]):
if pixels ==(o,o,o):
image_2.putdata(pixels((x,y),(0,0,0)))
image_2.save('painted.bmp')
Again, i'm new so go easy on me and any help would be greatly appreciated.
Cheers.