fade image to transparency?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
heep
Posts: 6
Joined: 2010-08-26T02:24:56-07:00
Authentication code: 8675308

fade image to transparency?

Post 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?
heep
Posts: 6
Joined: 2010-08-26T02:24:56-07:00
Authentication code: 8675308

Re: fade image to transparency?

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: fade image to transparency?

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: fade image to transparency?

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: fade image to transparency?

Post 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!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
webad20
Posts: 3
Joined: 2011-02-03T06:45:47-07:00
Authentication code: 8675308

Re: fade image to transparency?

Post by webad20 »

yes, I find the so called transparency is black too. I have to use -colorize with -fill being my background color.
Post Reply