1

I have been searching relevant strings for days, but couldn't find a good answer for me. my problem is how I can draw a transparent red rectangle on top of a blue rectangle. Here is how i am doing now.

im=Image.new('RGBA',(400,400),'white')
draw=ImageDraw.Draw(im)
draw.rectangle((100,100,200,200),fill=(0,255,0,0)) #big 100*100 green rectangle
draw.rectangle((80,80,130,130),fill=(255,0,0,0)) #small 50*50 red rectangle
im.show()

what I got now is the read rectangle totally cover overlapping part of the green one, but I want overlapping part be transparent, so that I can see under the red rectangle it is the part of green one and the overlapping part will be another color.

Any help would be appreciated!

xiaozhu123
  • 121
  • 2
  • 6
  • Do you want the entire rectangle to be slightly transparent or just the overlapping part? – supakeen Feb 22 '13 at 10:47
  • I want entire red rectangle to be slightly transparent, sorry for the unclear – xiaozhu123 Feb 22 '13 at 10:49
  • possible duplicate of [How do you draw transparent polygons with Python?](http://stackoverflow.com/questions/359706/how-do-you-draw-transparent-polygons-with-python) – chrisst Aug 25 '14 at 19:00

2 Answers2

1

Several good answers in another thread. Including explaining that if you are going to use the 4th argument then the draw object needs to be 'RGBA' and the base images must be 'RGB' mode for this to work.

Community
  • 1
  • 1
chrisst
  • 1,696
  • 5
  • 19
  • 32
0

If you just do a rectangle the 4th argument in fill is an opacity where 0 is fully opaque.

supakeen
  • 2,876
  • 19
  • 19
  • I first tried blend method, which is working! Thanks! but by changing 4th argument (green one with 200, read one with 50) did not work :( – xiaozhu123 Feb 22 '13 at 11:09