How to apply a selective transparency to an image?
How to apply a selective transparency to an image?
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.
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: How to apply a selective transparency to an image?
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):
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 ^
in.png ^
( -size 1280x720 xc:white -fill gray(50%%) -draw "rectangle 0,620 1279,719" ) ^
-alpha off -compose Copy Opacity -composite ^
out.png
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
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How to apply a selective transparency to an image?
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
convert image \
\( -clone 0 -fill white -colorize -size 1280x100 xc:gray50 -gravity south -compose over -composite \) \
-alpha off -compose copy_opacity -composite result.png
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How to apply a selective transparency to an image?
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?
That's awesome! I knew it could be done!
Thanks
Thanks

Re: How to apply a selective transparency to an image?
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.
Then:
When I'm doing the transparency part, I get this:
What am I missing here?
If I use temp\background.png for the transparency, the last command works. What's going on there?
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
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
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.
If I use temp\background.png for the transparency, the last command works. What's going on there?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How to apply a selective transparency to an image?
Syntax is different on Windows. You need to use %% rather than %. See http://www.imagemagick.org/Usage/windows/tools\convert.exe temp\canvas.png -alpha set -channel rgba -region 1280x100+0+621 -channel a -evaluate set 50% temp\canvas.png
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?
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...

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...

Last edited by Znuff on 2014-03-01T11:52:30-07:00, edited 1 time in total.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How to apply a selective transparency to an image?
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.-page -30-130
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?
That didn't work out either 



- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How to apply a selective transparency to an image?
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.
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?
Canvas.png:

Background.png:

I used +repage at the end of the annotate command (+repage temp\canvas.png).
Geometry on both files say 1280x720

Background.png:

I used +repage at the end of the annotate command (+repage temp\canvas.png).
Geometry on both files say 1280x720
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How to apply a selective transparency to an image?
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?
It's my own directory.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How to apply a selective transparency to an image?
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
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