On 8/2/05, Shi, Xuan <[EMAIL PROTECTED]> wrote: > Please help me to figure out how to make such composite images. Currently, I > have one png image with transparent background and want to overlay it on top > of another image (jpeg). However, what I want to do is to make only > transparent color as transparent on the composite image while keep all other > non-transparent colors unchanged. In my approach, all colors in the png file > are transparent. Any suggestions will be greatly appreciated. I am looking > forward to hearing from you.
Hi Sam, Its not real clear to me what you are trying to do, but it sounds you're having a problem that I had at first. Without knowing exactly what you're seeking (sending an example of code that doesn't quite work may help illustrate what you're trying to do), I'll have to guess. You have a jpg, which is an RGB image and you have a png, which is an RGBA image (it has alpha transparency). You're trying to composite the RGBA image onto the RGB image. However, the entire RGBA image is transparent, so you see only the RGB jpg. My first attempt at RGBA resulted in opposite pixels being transparent. I'd guess that if you loaded an image as RGB when you meant it to be RGBA, and then got your transparency backwards you'd get something like you described. Of course, this is pure conjecture. Here is some code that just creates a rectangle and puts a drop shadow behind it. This is kind of old, and was my first attempt at using transparency. It was actually the foundation for a much more complex program, which is why it may be a little overly complex to achieve its goal. (Although I would like to know how to get guassian blur - I had to use two blurs to get what I considered adequate) Notice the line, "c = ImageChops.invert(c)" which inverts the alpha channel before using putalpha(). #!/usr/bin/python import Image import ImageFilter import ImageChops import sys width = 400 height = 300 save_name = "trans_test.gif" # a little time saver from: # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/266466 def HTMLColorToRGB(colorstring): """ convert #RRGGBB to an (R, G, B) tuple """ colorstring = colorstring.strip() if colorstring[0] == '#': colorstring = colorstring[1:] if len(colorstring) != 6: raise ValueError, "input #%s is not in #RRGGBB format" % colorstring r, g, b = colorstring[:2], colorstring[2:4], colorstring[4:] r, g, b = [int(n, 16) for n in (r, g, b)] return (r, g, b) color = "#FF0000" shadow_color = HTMLColorToRGB(color) white = HTMLColorToRGB("#FFFFFF") black = HTMLColorToRGB("#000000") new = Image.new("RGB", (width+20, height+20), white) new.paste(shadow_color, (8,8,width+12,height+12)) tmp = new.filter(ImageFilter.BLUR) new = tmp.filter(ImageFilter.BLUR) new = new.convert("RGBA") c = new.convert("L") c = ImageChops.invert(c) bands = c.split() new.putalpha(bands[0]) new.show() new.save(save_name) #end I'm no guru, so any critique is welcome. Also, Sam, don't get the impression that I'm extremely knowledgable about this stuff, I'm a major newbie. -- Matthew Nuzum www.bearfruit.org _______________________________________________ Image-SIG maillist - [email protected] http://mail.python.org/mailman/listinfo/image-sig
