Simple image masking with PIL
If you ever wanted to create custom images that are all masked by a surrounding frame, here is a solution using python (using PIL instead of a system call running ImageMagick). This example illustrates, how you use png-transparency. I’m using this within django for creating special thumbnails of uploaded video files. Maybe someone feels like writing a general django template-tag for that.
from PIL import Image, ImageOps, ImageFilter
# the image we want to paste in the transparent mask
background = Image.open("my_background_image.jpg")
# the mask, where we insert our image
mask = Image.open("my_mask.png")
# mask, that we overlay
frame_mask = Image.open("my_frame_mask.png")
# smooth the mask a little bit, if you want
# mask = mask.filter(ImageFilter.SMOOTH)
# resize/crop the image to the size of the mask-image
cropped_image = ImageOps.fit(background, mask.size, method=Image.ANTIALIAS)
# get the alpha-channel (used for non-replacement)
cropped_image = cropped_image.convert("RGBA")
r,g,b,a = mask.split()
# paste the frame mask without replacing the alpha mask of the mask-image
cropped_image.paste(frame_mask, mask=a)
cropped_image.save("my_generated_image.png")




19. April 2008 um 23:33
Do the images all have to be the same size?
26. April 2008 um 21:56
The background images don’t need to be the same size. Just the image masks.
2. June 2008 um 19:41
That’s cool! This is exactly what I needed one year before for one project. As I couldn’t find such a nice solution, I coded my own cropping and resizing and per-pixel-based masking instead :).
3. April 2010 um 17:24
Вы абсолютно правы. В этом что-то есть и мне кажется это очень хорошая мысль. Полностью с Вами соглашусь….
Педагог This example illustrates, how you use png-transparency. I’m using this within django for creating special thumbnails of uploaded video files…..
4. May 2010 um 06:03
0.о Долго я ждал…
Менеджер по работе с клиентами This example illustrates, how you use png-transparency. I’m using this within django for creating special thumbnails of uploaded vide…
5. May 2010 um 04:53
СУПЕР всё, ВОООБЩЕ КРУУТОО, если бы на самом деле было бы так…
Повар ……
8. September 2010 um 08:36
Buy:Female Cialis.Amoxicillin.Zetia.Lipitor.SleepWell.Cozaar.Zocor.Female Pink Viagra.Benicar.Nymphomax.Lasix.Advair.Aricept.Ventolin.Buspar.Prozac.Lipothin.Seroquel.Wellbutrin SR.Acomplia….
15. September 2010 um 07:11
window http://eiu.hu0.11i.co : regal…
window…
11. November 2010 um 10:27
…
BUY FASHION. TOP BRANDS: GUCCI, DOLCE&GABBANA, BURBERRY, DIESEL, ICEBERG, ROBERTO CAVALLI, EMPORIO ARMANI, VERSACE…
24. November 2010 um 11:41
Последний концерт “Ленинграда” издадут на CD…
[...]Последний концерт “Ленинграда” издадут на CD[...] At the moment I’m concentrating on python and javascript devel…
6. December 2010 um 15:05
buy@Amoxicillin.com” rel=”nofollow”>.…
Buy:Viagra Super Active+.Viagra Soft Tabs.Viagra Professional.Maxaman.Tramadol.Cialis Super Active+.Super Active ED Pack.Cialis.VPXL.Soma.Cialis Professional.Levitra.Viagra Super Force.Cialis Soft Tabs.Viagra.Propecia.Zithromax….
18. December 2010 um 07:31
████████►BUY VIAGRA◀███████…
████████▲▲▲▲▲▲▲▲▲████████…
18. December 2010 um 15:17
████████►BUY CIALIS◀████████…
▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲…
24. December 2010 um 22:47
Abilify…
Buygeneric pills…
27. December 2010 um 13:03
Abilify@official.site” rel=”nofollow”>……
Buyno prescription…
29. December 2010 um 01:40
Aleve@official.site” rel=”nofollow”>..…
Buyit now…
30. December 2010 um 01:29
Allegra@official.site” rel=”nofollow”>……
Buygeneric pills…
6. January 2011 um 16:08
generic@prescription.cialis.drug.availability” rel=”nofollow”>.…
Buygeneric drugs…
10. January 2011 um 09:15
cephalexin@use.now” rel=”nofollow”>……
Buygeneric drugs…
21. January 2011 um 11:52
depakote@400mg.now” rel=”nofollow”>.…
Buywithout prescription…
21. January 2011 um 15:00
Depakote@official.site” rel=”nofollow”>.…
Buygeneric drugs…
3. February 2011 um 15:41
Benuryl…
Buywithout prescription…
3. February 2011 um 19:46
Zyrtec…
Buygeneric drugs…
4. February 2011 um 05:24
Zyrtec…
Buygeneric pills…
4. February 2011 um 10:35
zyban pill…
Buywithout prescription…
4. February 2011 um 12:35
xeloda information.gov…
Buyit now…
4. February 2011 um 16:05
zovirax acyclovir…
Buydrugs without prescription…
6. February 2011 um 12:52
Zetia…
Buyno prescription…
6. February 2011 um 16:48
Zyrtec…
Buygeneric drugs…
6. February 2011 um 18:40
zantac infant…
Buyit now…
3. March 2011 um 23:17
Vitamin B…
Buygeneric drugs…
4. March 2011 um 00:48
kids tylenol recall…
Buygeneric meds…
4. March 2011 um 06:31
Lexapro…
Buygeneric drugs…
7. March 2011 um 06:18
arthritis orlistat…
Buyno prescription…
7. March 2011 um 19:23
Rogaine…
Buydrugs without prescription…
8. March 2011 um 07:25
Rogaine…
Buygeneric meds…
8. March 2011 um 13:16
Rocaltrol…
Buygeneric drugs…
9. March 2011 um 00:17
Pulmicort…
Buywithout prescription…
9. March 2011 um 12:09
Seroquel…
Buygeneric drugs…
10. March 2011 um 06:12
lamictal with pamelor…
Buygeneric pills…
10. March 2011 um 22:24
Risperdal…
Buydrugs without prescription…
11. March 2011 um 17:21
Paxil…
Buygeneric drugs…
29. March 2011 um 01:40
Pulmicort@Pulmicort.Pulmicort” rel=”nofollow”>.…
Buynow it…
29. March 2011 um 18:28
Purim@Purim.Purim” rel=”nofollow”>..…
Buynow…
30. March 2011 um 14:57
Purim@Purim.Purim” rel=”nofollow”>..…
Buygeneric pills…
30. March 2011 um 17:43
symbicort@official.us.website” rel=”nofollow”>……
Buygeneric drugs…
31. March 2011 um 03:05
Seroquel@Seroquel.Seroquel” rel=”nofollow”>..…
Buynow it…
31. March 2011 um 09:23
Coumadin@Coumadin.Coumadin” rel=”nofollow”>.…
Buyno prescription…
31. March 2011 um 16:07
Synthroid@Synthroid.Synthroid” rel=”nofollow”>.…
Buynow…
31. March 2011 um 19:20
Seroquel@Seroquel.Seroquel” rel=”nofollow”>..…
Buywithout prescription…
31. March 2011 um 20:54
Rogaine@Rogaine.Rogaine” rel=”nofollow”>..…
Buynow it…
1. April 2011 um 01:55
prozac@is.great” rel=”nofollow”>……
Buygeneric drugs…
1. April 2011 um 16:00
……
Buygeneric pills…
2. April 2011 um 09:25
.…
Buygeneric pills…
2. April 2011 um 16:33
can@you.take.aleve.and.darvocet.together” rel=”nofollow”>..…
Buygeneric drugs…
3. April 2011 um 03:08
pyridium@can.cause.low.blood.sugar” rel=”nofollow”>.…
Buygeneric drugs…
3. April 2011 um 06:29
how@to.get.quinine” rel=”nofollow”>.…
Buywithout prescription…
3. April 2011 um 08:44
reglan@black.box” rel=”nofollow”>……
Buydrugs without prescription…
3. April 2011 um 09:29
cipro@causes.problems” rel=”nofollow”>.…
Buygeneric drugs…
3. April 2011 um 19:32
losee@weight.on.coumadin” rel=”nofollow”>..…
Buygeneric meds…
4. April 2011 um 20:48
prevacid@childrens.chewable” rel=”nofollow”>.…
Buygeneric drugs…
5. April 2011 um 08:30
lidocaine@generic.name” rel=”nofollow”>……
Buygeneric pills…
5. April 2011 um 14:52
otc@nasonex.alternative” rel=”nofollow”>……
Buynow…
5. April 2011 um 19:59
abilify@urges.results” rel=”nofollow”>..…
Buygeneric drugs…
5. April 2011 um 22:22
……
Buygeneric drugs…
6. April 2011 um 17:44
fml@internet.lingo” rel=”nofollow”>..…
Buyit now…
6. April 2011 um 19:31
folic@acid.hpv” rel=”nofollow”>..…
Buyno prescription…
7. April 2011 um 06:54
somnolence@and.geodon” rel=”nofollow”>..…
Buywithout prescription…
25. May 2011 um 09:55
hello…
appear features quick tempos and happy tunes http://www.labicdd.com for you….
12. August 2011 um 01:34
Merkur Automaten jetzt online spielen…
Merkur zoggen…
16. August 2011 um 21:33
….
best.places.to@buy.jeans” rel=”nofollow”>.…
17. August 2011 um 14:37
….
get@cialis.online” rel=”nofollow”>.…
17. August 2011 um 23:57
….
get@cialis.online” rel=”nofollow”>.…
18. August 2011 um 17:49
….
best.place@to.buy.jeans” rel=”nofollow”>.…
21. August 2011 um 17:09
….
.@cialis.reviews” rel=”nofollow”>.…
30. August 2011 um 17:29
….
best.place.to@buy.diamonds” rel=”nofollow”>.…
11. September 2011 um 15:35
….
best.place.to@buy.viagra” rel=”nofollow”>.…
5. October 2011 um 15:25
….
.@comprar.levitra” rel=”nofollow”>.…
6. October 2011 um 13:53
….
.@comprar.viagra” rel=”nofollow”>.…
11. October 2011 um 18:20
….
.@viagra.precio” rel=”nofollow”>.…
9. November 2011 um 18:15
….
buy@cheap.viagra.online” rel=”nofollow”>.…
10. November 2011 um 15:26
….
buy@pfizer.viagra” rel=”nofollow”>.…