Image fade out

The idea behind this feature is to fit the top half of the image only (make it darker gradually). Here is what I have, but it looks like it makes the entire upper 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)) 
+6
jython image-manipulation jes fade
source share
4 answers

Look at just one line:

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

and the key part is here

 y*(2.0/h) 

y changes when you go down. Let's try the simplest values ​​for y and h. Let them say that h is 100, and we will consider when y is 0 and 50 (h / 2). For y = 0 we get 0. For y = 50 we get 1. If your range of colors is 256 and 0 is the darkest, then it is not surprising that this is black. What you have is a range of values ​​from 0. to 1., but I assume that you want this number and time to be used by the value of the old color.

What would you like:

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

and similar things for other colors.

+3
source share

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

What are you doing:

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

What they say to you:

 setBlue(px,y*(2.0/h) * getBlue(px)) 
+4
source share

Find out what the scale is for setBlue / Red / Green methods. I suppose 0 matches black, but what is the brightest? You seem to assume this is 1, but in reality it could be 255 or something else. Even if it's 1, it looks like this code does not take into account the old pixel value, it just sets it to the exact color based on its vertical position. Perhaps this is what you want, but I doubt it. You probably want to multiply the value of the current pixel by something.

+2
source share

Just to share the advanced version and add some visual effects (because the visual effects are good) ...

 # 'divisor' : How much we expand the gradient (less is more) # 'switch' : If True, start gradient from bottom to top def fadeDownFromBlack(pic, divisor, switch=False): w = getWidth(pic) h = getHeight(pic) startY = 0 endY = min(h-1, int(h/float(divisor))) inc = 1 if (switch): startY = h-1 endY = max(0, h-1 - int(h/float(divisor))) inc = -1 color_ratio = float(divisor)/h for y in range(startY, endY, inc): for x in range(0,w): px = getPixel(pic, x, y ) setRed(px, abs(startY - y)*(color_ratio)*getRed(px)) setGreen(px, abs(startY - y)*(color_ratio)*getGreen(px)) setBlue(px, abs(startY - y)*(color_ratio)*getBlue(px)) file = pickAFile() picture = makePicture(file) # The following commented line answers the question #fadeDownFromBlack(picture, 2) fadeDownFromBlack(picture, 0.7, True) writePictureTo(picture, "/home/mad-king.png") show(picture) 


Exit ( Corneliu Baba Painting - Crazy King):


............ enter image description here ...................... enter image description here ............


+2
source share

All Articles