Page 1 of 1

fade image to transparency?

Posted: 2010-08-26T02:26:51-07:00
by heep
I have a jpg with an arbitrary size.

using the commandline, I'd like to:

1. Crop the image at 100px height
2. Fade the last 30% of the image into transparency
3. Save as png

I've been reading the documentation for hours, but I can't figure this out for the life of me. How can I do this?

Re: fade image to transparency?

Posted: 2010-08-26T03:33:54-07:00
by heep
Here's a goofy version that works.



#!/bin/bash

h=100

# Get width
w=`convert source.jpg -format "%w" info:`

# Create gradient
convert -size ${w}x$h gradient:black-none alpha_grad_src.png

# Crop and apply gradient
convert source.jpg -crop ${w}x${h}+0+0 \
alpha_grad_src.png -compose CopyOpacity -composite \
output.png

Re: fade image to transparency?

Posted: 2010-08-26T09:22:23-07:00
by fmw42
heep wrote:I have a jpg with an arbitrary size.

using the commandline, I'd like to:

1. Crop the image at 100px height
2. Fade the last 30% of the image into transparency
3. Save as png

I've been reading the documentation for hours, but I can't figure this out for the life of me. How can I do this?

1) You don't say what part of the image to crop. I assume the center.
2) Is that a gradual fade to transparent or a sudden change. I assume gradual.

This puts it all in one command line:


convert \( yourimage -gravity center -crop x100+0+0 +repage \) \
\( +clone -sparse-color Barycentric '0,0 white 0,%[fx:h-1] black' -level 0x30% -write zelda3_tmp.png \) \
-alpha off -compose copy_opacity -composite yourimage_result.png

-crop x100 allows the width to stay as is and +repage removes the virtual canvas
-sparse-color Barycentric with two points creates a gradient, but has the advantage of the points coming from calculations, %[fx:h-1], in this case, which is just the bottom row of the image

see
http://www.imagemagick.org/Usage/crop/#crop
http://www.imagemagick.org/Usage/canvas ... _gradients

Re: fade image to transparency?

Posted: 2010-08-26T18:24:42-07:00
by anthony
fmw42 wrote:-sparse-color Barycentric with two points creates a gradient, but has the advantage of the points coming from calculations, %[fx:h-1], in this case, which is just the bottom row of the image
Warning those points are the very top edge, and the top edge of the last row.
The mathematics is perfect and in image coordinates.

It means the top pixel will not be white. and the only reason the bottom pixel is black
is because color limit was reached at the edge of the pixel.

I have updated the IM examples section Generating the Perfect Gradient to better detail the slight differences in the different gradients you can generate.

For this image You probably what the first pixel in the gradient to be very slightly transparent and that last pixel to be almost but not perfectly transparent, so as to get the best use of the gradient range.

So I would suggest using...

Code: Select all

    -sparse-color Barycentric '0,-0.5 white 0,%[fx:h+0.5] black'
With this the first pixel will be slightly off-white and the last pixel slightly-off black
but all color differences from one pixel to the next at the edges the same.
That is opaque to first pixel, and last pixel to transparent (off the image) is the same as the rest of the gradent over the full range of the gradient.

however really this minor point will probably not really be noticeable. Just depends how 'perfect' you need it :-) But a little thought can be useful.

Re: fade image to transparency?

Posted: 2010-08-26T18:45:59-07:00
by anthony
-sparse-color CAN generate alpha gradients directly!!!! Expecially in nonHDRI so out-of bound results are clipped!!!

Code: Select all

convert yourimage -gravity center -crop x100+0+0 +repage -alpha set \
     -channel A -sparse-color Barycentric ' 0,%[fx:h*2/3] opaque    0,%[fx:h+0.5] transparent ' \
     yourimage_result.png
HOLD that should have worked... the result however was 'black' with transparency!!!
But the alpha channel was modified correctly, just all the other color channels were modified too!

It appears a bug has been found!

Re: fade image to transparency?

Posted: 2011-02-05T03:28:28-07:00
by webad20
yes, I find the so called transparency is black too. I have to use -colorize with -fill being my background color.