Page 1 of 2

How to apply a selective transparency to an image?

Posted: 2014-02-28T10:42:59-07:00
by Znuff
Hello,

I've been trying to figure out the best way to add a selective transparency to an image.

Let's say I have 1280x720 image and I want to apply a 50% transparency over the last 100 bottom pixels (with 100% width).

Could this be done without creating a mask first? I haven't managed to figure out a decent solution.

Re: How to apply a selective transparency to an image?

Posted: 2014-02-28T11:22:08-07:00
by snibgo
I suppose you could use "-fx" but that would be very slow. I think you need a mask of some sort, for example (Windows syntax):

Code: Select all

convert ^
  in.png ^
  ( -size 1280x720 xc:white -fill gray(50%%) -draw "rectangle 0,620 1279,719" ) ^
  -alpha off -compose Copy Opacity -composite ^
  out.png
If you are doing this many times (eg subtitles for video), it may be faster to create a file once, then use that for each frame:

Code: Select all

convert ^
  -size 1280x720 xc:white -fill gray(50%%) -draw "rectangle 0,620 1279,719" ^
  mask.mpc

convert ^
  inframe.png ^
  mask.mpc ^
  -alpha off -compose Copy Opacity -composite ^
  outframe.png

Re: How to apply a selective transparency to an image?

Posted: 2014-02-28T11:23:07-07:00
by fmw42
I think you have to create a mask first, but the whole thing can be done in one command line using parenthesis processing. see http://www.imagemagick.org/Usage/basics/#parenthesis

convert image \
\( -clone 0 -fill white -colorize -size 1280x100 xc:gray50 -gravity south -compose over -composite \) \
-alpha off -compose copy_opacity -composite result.png

Re: How to apply a selective transparency to an image?

Posted: 2014-02-28T11:28:43-07:00
by fmw42
On second thought, here is a way that does not need a mask

Code: Select all

convert image -alpha set -channel rgba -region 1280x100+0+621 -channel a -evaluate set 50% +channel show:

Re: How to apply a selective transparency to an image?

Posted: 2014-03-01T07:45:38-07:00
by Znuff
That's awesome! I knew it could be done!

Thanks :-)

Re: How to apply a selective transparency to an image?

Posted: 2014-03-01T10:34:37-07:00
by Znuff
Hmmm, I seem to be running into a weird issue when using some already processed images. I'm doing this:

convert.exe: geometry does not contain image `temp\canvas.png' @ warning/transform.c/CropImage/666.

Code: Select all

tools\convert.exe -background #181818 file.jpg -resize 1280x1280^> -gravity center -extent 1280x720 -font Calibri -pointsize 14 -gravity north-east -stroke #000C -undercolor #00000022 -strokewidth 3 -annotate 0 " made with stics - http://linge-ma.ws/stics " -stroke none -fill white -annotate 0 " made with stics - http://linge-ma.ws/stics " temp\background.png
Then:

Code: Select all

tools\convert.exe temp\background.png -font ./tools/impact.ttf -pointsize 42 -gravity south-east -page -30-130 -stroke #000C -strokewidth 3 -annotate 0 "BTSTU (JAI PAUL COVER)" -stroke none -fill white -annotate 0 "BTSTU (JAI PAUL COVER)" -page -30-172 -stroke #000C -strokewidth 3 -annotate 0 "NIIA" -stroke none -fill white -annotate 0 "NIIA" temp\canvas.png
When I'm doing the transparency part, I get this:

Code: Select all

tools\convert.exe temp\canvas.png -alpha set -channel rgba -region 1280x100+0+621 -channel a -evaluate set 50% temp\canvas.png
convert.exe: geometry does not contain image `temp\canvas.png' @ warning/transform.c/CropImage/666.
What am I missing here?

If I use temp\background.png for the transparency, the last command works. What's going on there?

Re: How to apply a selective transparency to an image?

Posted: 2014-03-01T10:56:33-07:00
by fmw42
tools\convert.exe temp\canvas.png -alpha set -channel rgba -region 1280x100+0+621 -channel a -evaluate set 50% temp\canvas.png
Syntax is different on Windows. You need to use %% rather than %. See http://www.imagemagick.org/Usage/windows/

It is always a good idea to identify your version of IM and platform when asking a question.

Also you have to change the region for each different image size. Check the size of your input image canvas.png

Re: How to apply a selective transparency to an image?

Posted: 2014-03-01T11:44:04-07:00
by Znuff
I'm using ImageMagick 6.8.8-3 Q16 x64 2014-01-19 on Windows.

The % doesn't seem to be the issue, as, like I said, it works fine if I use background.png instead of canvas.png

My image size never changes, it's always 1280x720.

Something is going wrong when I'm annotating the image (the 2nd step) and I can't seem to figure out what...

Image

Re: How to apply a selective transparency to an image?

Posted: 2014-03-01T11:51:17-07:00
by fmw42
-page -30-130
This may be the issue. It appears to be shifting the image outside the bounds. Check its verbose information and see if Page geometry: has offsets and different size.

Otherwise, just try using +repage before saving the image.

Also the parens in your text may have to be escaped. But I do not use Windows and so am not sure about that.

Re: How to apply a selective transparency to an image?

Posted: 2014-03-01T11:54:51-07:00
by Znuff
That didn't work out either :(

Image

Re: How to apply a selective transparency to an image?

Posted: 2014-03-01T12:03:34-07:00
by fmw42
I do not see +repage anywhere. Am I missing it?

Did you get the verbose information

identify -verbose yourimage

and check the Page Geometry?

Can you post a link to the input image for the transparency command? I can then try that image with my command.

Re: How to apply a selective transparency to an image?

Posted: 2014-03-01T12:10:06-07:00
by Znuff
Canvas.png:
Image


Background.png:
Image

I used +repage at the end of the annotate command (+repage temp\canvas.png).

Geometry on both files say 1280x720

Re: How to apply a selective transparency to an image?

Posted: 2014-03-01T12:11:02-07:00
by fmw42
Is temp your directory or the system directory. If the latter, it is not wise to do processing there. You could be running out of space.

Re: How to apply a selective transparency to an image?

Posted: 2014-03-01T12:13:54-07:00
by Znuff
It's my own directory.

Re: How to apply a selective transparency to an image?

Posted: 2014-03-01T12:15:31-07:00
by fmw42
Your canvas image still has an offset. Its Page geometry: 1280x720-30-172. So your +repage either was not there or did not work, which is strange.

But this works for me

convert canvas.png +repage -alpha set -channel rgba -region 1280x100+0+621 -channel a -evaluate set 50% +channel show:

What is the purpose of -page -30-172 in your earlier command?

If you want to offset the annotation, -annotate has offset arguments. See
http://www.imagemagick.org/script/comma ... p#annotate
http://www.imagemagick.org/Usage/text/#annotate