1

i am trying to get one image over the other i.e. if two images greenapple.png and redcolor.png images are there with me, now i want to multiply that redcolor.png to greenapple.png so that the image in greenapple.png will be covered up with redcolor.png only at the place where greeapple.png has and leave the rest. i have tried ImageChops to do it and the code is

import Image
import bakepass
from PIL import ImageChops

im1 = Image.open("greenapple.png")
im2 = Image.open("redcolor.png")
image = Image.open("new.png")

image.save(ImageChops.multiply(im1,im2))

but using the above code i am getting Value error : images do not match i am using the files of same size 512X512 plz help me out

MultiVAC
  • 354
  • 1
  • 10
Dinesh
  • 93
  • 3
  • 13
  • 1
    `from PIL import Image` please. See also: http://pillow.readthedocs.org/en/latest/ – Kos Jun 05 '14 at 07:08

1 Answers1

1

This is probably what you're looking for: changing hue

But if you for some reason don't want to use numpy, you could play around with composite and the alpha channels (the below puts an alpha value across the entire image, you could calculate via the green/red color of img1 where to change the alpha of img2):

from PIL import Image, ImageEnhance

img1 = Image.open('apple.png')
img2 = Image.open('green.png')
img2.putalpha(ImageEnhance.Brightness(img2.split()[3]).enhance(0.75))
img1 = Image.composite(img2, img1, img2)
img1.save('out.png')

Here are the two images (al tho a bit large perhaps) I used for the above test, and the third image is the result of the above code:

apple.png

Imgur

green.png

Imgur

out.png

Imgur

There's also PIL's paste() function:

from PIL import Image, ImageEnhance
img = Image.open('greenapple.png', 'r')
img_w, img_h = img.size

red = Image.open('redcolor.png', 'r')
# red_w, red_h = red.size

new = Image.new('RGBA', (1024,769), (255, 255, 255, 255))
new_w, new_h = new.size
offset=((new_w-img_w)/2,(new_h-img_h)/2)

red.putalpha(ImageEnhance.Brightness(red.split()[3]).enhance(0.75))

new.paste(img, offset)
new.paste(red, offset)
new.save('out.png')

Play around with img.split() which gives you red, green, blue, alpha and use the green/red color patches when determining where to put the overlay.

Here are some more calculated alternatives where you for instance can use black as the exclusion color:

Community
  • 1
  • 1
Torxed
  • 22,866
  • 14
  • 82
  • 131
  • i have checked it, no errors but no changes to the image also – Dinesh Jun 05 '14 at 06:58
  • @user3453803 check my edit, forgot to add alpha and also the order of img2/img1 was off on the composite. Again, sorry for coding out of my head, gets a bit tricky doing it :) – Torxed Jun 05 '14 at 07:06
  • thanks for such a clear description sir but can we get area in which apple is present as green and leave the rest – Dinesh Jun 05 '14 at 08:27
  • @user3453803 either you create an overlay that's the same size as the parts you want to overlay OR you grab the red channel from `img.getpixel((x, y))` and loop over the overlay only reading `(x,y)` matching the red channel in the first image. This is manual work you'd have to do, there's no predefined function in PIL for this (hence my reference links) – Torxed Jun 05 '14 at 08:36
  • its throwing an error "NoneType" object has no attribute 'bands' – Dinesh Jun 05 '14 at 09:17
  • @user3453803 I'm not using 'bands' anywhere in my code. Not sure what you're doing. – Torxed Jun 05 '14 at 10:15