PIL transparency gradient

A

Allard Warrink

I would like to create a transparency gradient over an image using
PIL. But I don't have a clue how to do this...
Is there anyone out here who could give me some advise?
 
S

SPE - Stani's Python Editor

I would like to create a transparency gradient over an image using
PIL. But I don't have a clue how to do this...
Is there anyone out here who could give me some advise?

Phatch (PHoto & bATCH) is an application based on PIL. Its actions
contain many useful PIL recipes, such as applying a transparency
gradient to an image. You basically need to create an 1pxx256px
grayscale image which goes from 0 to 255. After that you can stretch
it to the image size and add it as an alpha channel. You'll find the
source code for this in the phatch/actions/mask.py Every action
contains a 'raw' PIL function that can work independently outside the
context of Phatch. This is by design, inspired by the MVC model in
which you could see PIL as the model and Phatch as the View.

You can get Phatch at:
http://photobatch.stani.be

Read first *carefully* the installation instructions for your
platform:
http://photobatch.wikidot.com/install

Stani
 
A

Allard Warrink

Thanks for the inspiration!
This what I did (Using your Python Editor (SPE), I really like to work
with SPE, keep up the good work!!):

#############################################
import Image, os
p = # path to image file

im = Image.open(p)
# check if im has Alpha band...
if im.mode != 'RGBA':
im = im.convert('RGBA')
# create a vertical gradient...
gradient = Image.new('L', (1,255))
for y in range(255):
gradient.putpixel((0,254-y),y)
# resize the gradient to the size of im...
alpha = gradient.resize(im.size)
# put alpha in the alpha band of im...
im.putalpha(alpha)
# Save as png...
im.save(os.path.splitext(p)[0] + '.png', 'PNG

#############################################
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top