Add moderately transparent gray colored top bar to an image

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
Michael Gruenstaeudl
Posts: 4
Joined: 2018-07-06T15:19:21-07:00
Authentication code: 1152

Add moderately transparent gray colored top bar to an image

Post 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
Last edited by Michael Gruenstaeudl on 2018-07-06T15:55:32-07:00, edited 5 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Increase opacity only of top bar of an image

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Increase opacity only of top bar of an image

Post 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
snibgo's IM pages: im.snibgo.com
Michael Gruenstaeudl
Posts: 4
Joined: 2018-07-06T15:19:21-07:00
Authentication code: 1152

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

Post 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
Michael Gruenstaeudl
Posts: 4
Joined: 2018-07-06T15:19:21-07:00
Authentication code: 1152

Re: Increase opacity only of top bar of an image

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

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

Post 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
Michael Gruenstaeudl
Posts: 4
Joined: 2018-07-06T15:19:21-07:00
Authentication code: 1152

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

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply