0

I don't really know where to start with this one. I'm trying to do something that I thought would be relatively simple to accomplish with imagemagick, but I don't know the exact command to start with. I need to draw a line through an image, and then make everything above the line transparent in the image, and make everything below the line, the orignal image. What would be the best way to accomplish this using imagemagick?

So what I've come up with for now is to crop the image, and then resize it to the original size, but with a transparent background. The command I use is this, but it always comes out black. I'm not understanding why.

convert -background none -gravity south out.png -resize 400x200 -extent 400x400 result.png

Thanks for all of the help!

  • try adding an alpha channel with its values being maximum for non-transparency and half that value for half-transparency. adding that alpha channel may be achieved by converting the image to RGBA. – user236012 Oct 14 '16 at 04:48
  • @user236012 That's not quite what I was going for. I really need the bottom half of an image to be 100% opaque, and the top half to be 100% transparent. –  Oct 14 '16 at 05:05
  • You need to add a mask. I can not test at the moment but this should give you some idea: https://www.imagemagick.org/Usage/photos/#overlap – Bonzo Oct 14 '16 at 06:54

1 Answers1

6

Here's a fairly easy way to do it. First, enable an alpha channel in case your image doesn't have one, then select the alpha channel for modification by the following -fx command. In there, if the current j is greater than half the height of the image, make the alpha layer opaque, else transparent. Easier than it sounds!

So, using this start image:

enter image description here

convert bean.jpg -alpha on -channel A -fx "j>h/2?1:0" result.gif

enter image description here

Or, the other way:

convert bean.jpg -alpha on -channel A -fx "j<h/2?1:0" result.gif

enter image description here

Or the other, other way:

convert bean.jpg -alpha on -channel A -fx "i<w/2?1:0" result.gif

enter image description here

Or, if you are feeling particularly silly on a Friday morning...

convert bean.jpg -alpha on -channel A -fx "hypot(i,j)/400-0.8" result.gif

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432