Page 1 of 1

Add moderately transparent gray colored top bar to an image

Posted: 2018-07-06T15:34:12-07:00
by Michael Gruenstaeudl
Assume a color image. I would like to give the top bar of the image (i.e., a rectangular section with a width equal to the total width of the image) a moderately transparent gray color, such that a regular black text can be placed onto and easily read from it, irrespective of the actual image color. The height of said bar is, say, 30% of the total height of the image. How do I do that using ImageMagick?

Code: Select all

$ uname
Linux
$ convert -v
Version: ImageMagick 7.0.8-5 Q16 x86_64 2018-07-05 https://www.imagemagick.org
$ convert input.jpg sought_options output.jpg

Re: Increase opacity only of top bar of an image

Posted: 2018-07-06T15:38:15-07:00
by fmw42
Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at http://www.imagemagick.org/discourse-se ... f=1&t=9620

For novices, see

http://www.imagemagick.org/discourse-se ... f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
https://github.com/ImageMagick/usage-markdown

Just to be clear, are you asking to make it more opaque or more transparent? JPG does not support transparency and so you cannot make it more opaque.

Post your image to some free hosting service and put the URL here, so we can see exactly what you are trying to do and what you start with.

Re: Increase opacity only of top bar of an image

Posted: 2018-07-06T15:46:39-07:00
by snibgo
What version of IM, on what platform? I'll assume v7 on Windows CMD.

Assuming you want to set opacity in that area to 25% (so we don't care what it used to be):

Code: Select all

magick ^
  in.png ^
  ( +clone -alpha extract -fill gray(25%) ^
    -draw "rectangle 0,0 %[fx:w-1],%[fx:h*0.3]" ^
  ) ^
  -alpha off -compose CopyOpacity -composite ^
  out.png

Re: Add moderately transparent gray colored top bar to an image

Posted: 2018-07-06T15:56:46-07:00
by Michael Gruenstaeudl
Note: I edited the original question to provide the relevant details on IM version and platform.

Code: Select all

$ uname
Linux
$ convert -v
Version: ImageMagick 7.0.8-5 Q16 x86_64 2018-07-05 https://www.imagemagick.org

Here is an example input: Image

And here is an example of the sought output: Image

Re: Increase opacity only of top bar of an image

Posted: 2018-07-06T16:28:22-07:00
by Michael Gruenstaeudl
snibgo wrote: 2018-07-06T15:46:39-07:00 What version of IM, on what platform? I'll assume v7 on Windows CMD.

Assuming you want to set opacity in that area to 25% (so we don't care what it used to be):

Code: Select all

magick ^
  in.png ^
  ( +clone -alpha extract -fill gray(25%) ^
    -draw "rectangle 0,0 %[fx:w-1],%[fx:h*0.3]" ^
  ) ^
  -alpha off -compose CopyOpacity -composite ^
  out.png
Your answer looks intriguing, but I cannot replicate your solution due to working on a different platform. What would the equivalent command be for the lastest IM version under Linux? I am working with ImageMagick 7.0.8-5 on Linux.

Re: Add moderately transparent gray colored top bar to an image

Posted: 2018-07-06T16:51:18-07:00
by fmw42
In Unix syntax that would be:

Code: Select all

magick \
in.png \
\( +clone -alpha extract -fill "gray(25%)" \
-draw "rectangle 0,0 %[fx:w-1],%[fx:h*0.3]" \
\) \
-alpha off -compose CopyOpacity -composite \
out.png

Re: Add moderately transparent gray colored top bar to an image

Posted: 2018-07-06T17:07:02-07:00
by Michael Gruenstaeudl
fmw42 wrote: 2018-07-06T16:51:18-07:00 In Unix syntax that would be:

Code: Select all

magick \
in.png \
\( +clone -alpha extract -fill "gray(25%)" \
-draw "rectangle 0,0 %[fx:w-1],%[fx:h*0.3]" \
\) \
-alpha off -compose CopyOpacity -composite \
out.png
Yes, that is what I was looking for! Thank you.

Re: Add moderately transparent gray colored top bar to an image

Posted: 2018-07-07T03:55:26-07:00
by snibgo
If the input is 1000 pixels high, we want the area to be 1000*0.3 = 300 pixels high, which is from y=0 to y=299. So we need to subtract one from the height. Where I had "%[fx:h*0.3]", I should have subtracted one, so "%[fx:h*0.3-1]".

As always, there are alternative methods. For example:

Code: Select all

magick in.png -region %[fx:w]x%[fx:h*0.3]+0+0 -channel A -evaluate set 25% +channel out.png
"-region" needs the width and height, not the start and end coordinates, so we don't subtract one.