Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fading a picture gradually

The idea of this function is to fade the top half only of the picture (make it gradually darker). Here is what I have but it seems to be making all of the top half solid black.

def fadeDownFromBlack(pic1):

w=getWidth(pic1)
h=getHeight(pic1)

for y in range(0,h/2):
     for x in range(0,w):
        px=getPixel(pic1,x,y) 
        setBlue(px,y*(2.0/h)) 
        setRed(px,y*(2.0/h)) 
        setGreen(px,y*(2.0/h))
like image 465
roger34 Avatar asked Feb 10 '10 21:02

roger34


People also ask

How do I gradually fade in Photoshop?

Select the Gradient tool on the main toolbar, hold down Shift, and draw a line across the area you want to fade. Drawing a longer line will create a more gradual effect. Finally, you can reposition either of the two images, even after you've applied a gradient.

What tool gives fade to pictures?

The easiest way to add faded effect on the photograph is by using the Fotophire Editing Toolkit.

How do I gradually fade an image in InDesign?

The Gradient Feather Tool (Keyboard shortcut: Shift + G) in InDesign allows for an image to fade from opaque to transparent along a line controlled by the user. With an image selected, drag the cursor from the part of the image that is to be opaque to the part of the image that is to be transparent.


2 Answers

To darken a pixel you multiply the red, green and blue levels by an appropriate fraction.

What you are doing:

setBlue(px,y*(2.0/h))

What you are being told to do:

setBlue(px,y*(2.0/h) * getBlue(px))
like image 196
badp Avatar answered Sep 28 '22 07:09

badp


Let's look at just one line here:

setBlue(px,y*(2.0/h))

and key part here is

y*(2.0/h)

y changes, as you go down. Let's try some simple values for y and h. Let's say h is 100 and we will examine when y is both 0 and 50 (h/2). For y = 0, we get 0. For y = 50, we get 1. If your range of values for the colors is 256 with 0 being the darkest, then no wonder this is black. What you have is a range of values from 0. to 1., but I'm guessing what you want is to take that number and times it by the old color value.

What you want is:

setBlue(px,y*(2.0/h)*getBlue(px))

and similar things for the other colors.

like image 41
Justin Peel Avatar answered Sep 28 '22 09:09

Justin Peel