Colorize part of 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
asdfasdfvful
Posts: 5
Joined: 2017-05-27T15:21:47-07:00
Authentication code: 1151

Colorize part of an image

Post by asdfasdfvful »

I have a black and white image and I wish to colorize every pixel except for the ones around the border. Currently, colorizing can be done by

Code: Select all

magick convert input.png -fill red -colorize 100% output.png
And does what I expect. But how will I be able to keep the border (1 pixel on each edge) from being colored?

There are two ways I can think of, but I'm not sure how to do them:

1. Chop the image to remove the border, colorize, then paste it on top of the original. In this case, I also need to remove the middle of the original image as there is transparency which might conflict if it was just added.
2. A "darken only" option. The borders are black (this is an Android 9.png image) and everything inside is white with varying opacity.

I'd like to do the following without knowing the image size in advance. But the border will always be 1 pixel wide.

Using "colorify" on GIMP gives me the output I desire, as it doesn't change black pixels, but I also couldn't find that option on ImageMagick.

I am using IM 7 in Bash for Windows

Thank you
Last edited by asdfasdfvful on 2017-05-27T19:44:52-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Colorize part of an image

Post by snibgo »

Another obvious method: colorize the image, "-shave" off one pixel from the edges, and composite that over the input (with "-gravity center").
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colorize part of an image

Post by fmw42 »

There are other ways to colorize, so look at the whole page (http://www.imagemagick.org/Usage/color_mods/) such as +level-colors "black,someothercolor", tinting and DIY coloring.

On IM 7, don't use "magick convert", use just "magick". The following is Unix syntax. For windows, I believe you need to remove the \ on each side of the parens. Adding to snibgo's suggestion of -shave, try

Code: Select all

magick  input.png \( +clone -shave 1x1 -fill red -colorize 100%" \) -gravity center -compose over -composite output.png
I am not sure about your transparency. What do you want to happen with its coloring if not fully transparent? With -colorize, you will see partially transparent red.

See also
http://www.imagemagick.org/Usage/crop/#shave
http://www.imagemagick.org/Usage/basics/#clone
http://www.imagemagick.org/Usage/basics/#parenthesis

Please always provide your IM version and platform/OS when asking questions on this forum.
asdfasdfvful
Posts: 5
Joined: 2017-05-27T15:21:47-07:00
Authentication code: 1151

Re: Colorize part of an image

Post by asdfasdfvful »

fmw42 wrote: 2017-05-27T16:11:40-07:00 There are other ways to colorize, so look at the whole page (http://www.imagemagick.org/Usage/color_mods/) such as +level-colors "black,someothercolor", tinting and DIY coloring.

On IM 7, don't use "magick convert", use just "magick". The following is Unix syntax. For windows, I believe you need to remove the \ on each side of the parens. Adding to snibgo's suggestion of -shave, try

Code: Select all

magick  input.png \( +clone -shave 1x1 -fill red -colorize 100%" \) -gravity center -compose over -composite output.png
I am not sure about your transparency. What do you want to happen with its coloring if not fully transparent? With -colorize, you will see partially transparent red.

See also
http://www.imagemagick.org/Usage/crop/#shave
http://www.imagemagick.org/Usage/basics/#clone
http://www.imagemagick.org/Usage/basics/#parenthesis

Please always provide your IM version and platform/OS when asking questions on this forum.
Thank you for the reply. I'm using git bash on windows so I still need the backslashes.

When I have a pixel with partial transparency, I want it to be colorized with the same transparency. Your method does do that, but once you overlay it, the transparent portions will be lighter than it should be as it is the red layer on top of the original white. I need to figure out a way to erase the contents inside the original before the composition. Typically this would involve selecting the image, shrinking the selection by 1px on each side, then erasing the selection.

I'm still looking on how that is done, or if I should simply shave 1px rows and columns and append it to the red image. Any ideas for that part will also be greatly appreciated.

Edit: After closer inspection, it seems that your result produces the same effect as a red screen. It's slightly different than GIMP's colorify but it will do. Another option for those interested is the darken composition:

Code: Select all

magick input.png \( +clone -fill red -colorize 100% \) -compose darken -composite test.png
Edit 2: This option seems to provide the closest output:

Code: Select all

magick input.png \( +clone -shave 1x1 \) -gravity center -compose out -composite \( input.png -shave 1x1 -fill red -colorize 100 \) -gravity center -compose over -composite output.png
1. Keep the border only
2. Shave and colorize
3. Combine the two layers into an output
Last edited by asdfasdfvful on 2017-05-27T20:17:27-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colorize part of an image

Post by fmw42 »

When I have a pixel with partial transparency, I want it to be colorized with the same transparency. Your method does do that, but once you overlay it, the transparent portions will be lighter than it should be as it is the red layer on top of the original white. I need to figure out a way to erase the contents inside the original before the composition. Typically this would involve selecting the image, shrinking the selection by 1px on each side, then erasing the selection.
Can you post your input image to some free hosting service and put the URL here.

Perhaps the other way to deal with it would be to just draw a black border around the colorized result, assuming you wanted to preserve a black border in your input already as I think you said earlier. You can shave 1 pixel all around and then add a 1 pixel black border if that is what you want.

Code: Select all

convert input.png -fill red -colorize 100% -shave 1x1 -bordercolor black -border 1 output.png
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Colorize part of an image

Post by GeeMack »

asdfasdfvful wrote: 2017-05-27T15:28:03-07:00I have a black and white image and I wish to colorize every pixel except for the ones around the border.

Using "colorify" on GIMP gives me the output I desire, as it doesn't change black pixels, but I also couldn't find that option on ImageMagick.
A quick easy way to get what you describe is to desaturate the image if it's not already grayscale, make a clone, tint the clone, shave 1 pixel from the edges of the clone, then composite it centered back on top of the gray input image.

Code: Select all

magick input.png -modulate 100,0 \
   \( +clone -shave 1 -fill red -tint 100 \) -gravity center -composite output.png
Desaturating then tinting like that should give you nearly the same effect as Gimp's "colorify".
asdfasdfvful
Posts: 5
Joined: 2017-05-27T15:21:47-07:00
Authentication code: 1151

Re: Colorize part of an image

Post by asdfasdfvful »

fmw42 wrote: 2017-05-27T20:04:27-07:00
When I have a pixel with partial transparency, I want it to be colorized with the same transparency. Your method does do that, but once you overlay it, the transparent portions will be lighter than it should be as it is the red layer on top of the original white. I need to figure out a way to erase the contents inside the original before the composition. Typically this would involve selecting the image, shrinking the selection by 1px on each side, then erasing the selection.
Can you post your input image to some free hosting service and put the URL here.

Perhaps the other way to deal with it would be to just draw a black border around the colorized result, assuming you wanted to preserve a black border in your input already as I think you said earlier. You can shave 1 pixel all around and then add a 1 pixel black border if that is what you want.

Code: Select all

convert input.png -fill red -colorize 100% -shave 1x1 -bordercolor black -border 1 output.png
http://i.imgur.com/T1B5u2A.png

You'll have to download it and open it to see it properly since it's white and transparent. This is a 9.png image used in Android development.

I have updated my comment above to reflect the closest command to what I want:

Code: Select all

magick input.png \( +clone -shave 1x1 \) -gravity center -compose out -composite \( input.png -shave 1x1 -fill red -colorize 100 \) -gravity center -compose over -composite output.png
Compose out is the shortest way I can think of of only keeping the border. The border is also not all black; it is either black with 100% opacity or 100% transparent.
asdfasdfvful
Posts: 5
Joined: 2017-05-27T15:21:47-07:00
Authentication code: 1151

Re: Colorize part of an image

Post by asdfasdfvful »

GeeMack wrote: 2017-05-27T20:17:31-07:00
asdfasdfvful wrote: 2017-05-27T15:28:03-07:00I have a black and white image and I wish to colorize every pixel except for the ones around the border.

Using "colorify" on GIMP gives me the output I desire, as it doesn't change black pixels, but I also couldn't find that option on ImageMagick.
A quick easy way to get what you describe is to desaturate the image if it's not already grayscale, make a clone, tint the clone, shave 1 pixel from the edges of the clone, then composite it centered back on top of the gray input image.

Code: Select all

magick input.png -modulate 100,0 \
   \( +clone -shave 1 -fill red -tint 100 \) -gravity center -composite output.png
Desaturating then tinting like that should give you nearly the same effect as Gimp's "colorify".
http://i.imgur.com/T1B5u2A.png

The image above is an example of what I'm working with. It seems like modulate gets rid of the transparency. And without it, the over composition faces the same issue as the other commands where overlaying the correctly tinted red onto the original white produces a pinkish color, since it is not opaque.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colorize part of an image

Post by fmw42 »

Can you show us your GIMP result that you want to achieve in ImageMagick?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colorize part of an image

Post by fmw42 »

try -compose copy

Code: Select all

convert  T1B5u2A.png \( +clone -shave 1x1 -fill red -colorize 100% \) -gravity center -compose copy -composite output.png
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Colorize part of an image

Post by GeeMack »

asdfasdfvful wrote: 2017-05-27T20:29:33-07:00 It seems like modulate gets rid of the transparency. And without it, the over composition faces the same issue as the other commands where overlaying the correctly tinted red onto the original white produces a pinkish color, since it is not opaque.
It's the "-tint" operator that gets rid of the transparency. The first example in the description at THIS link shows that tinting maintains the transparency when using "convert" from IM6. When using "magick" from IM7, that same example command fills the transparency with black. I've tested it several ways. I'd call it broken, but there are some changes to the alpha handling in IM7, and someone else may know how to get it to work as described.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colorize part of an image

Post by fmw42 »

GeeMack wrote: 2017-05-27T22:33:16-07:00

It's the "-tint" operator that gets rid of the transparency. The first example in the description at THIS link shows that tinting maintains the transparency when using "convert" from IM6. When using "magick" from IM7, that same example command fills the transparency with black. I've tested it several ways. I'd call it broken, but there are some changes to the alpha handling in IM7, and someone else may know how to get it to work as described.
Try adding -channel rgba to you command in IM 7 and see if that helps.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Colorize part of an image

Post by GeeMack »

fmw42 wrote: 2017-05-27T23:11:58-07:00Try adding -channel rgba to you command in IM 7 and see if that helps.
Using IM 7.0.5-8 on Windows and the sample image "test.png" as used in the examples HERE, all of these produce duplicate images and all will replace the transparency with black...

Code: Select all

magick test.png -fill red -channel R -tint 50 7tintR.png
magick test.png -fill red -channel G -tint 50 7tintG.png
magick test.png -fill red -channel B -tint 50 7tintB.png
magick test.png -fill red -channel A -tint 50 7tintA.png
magick test.png -fill red -channel RGB -tint 50 7tintRGB.png
magick test.png -fill red -channel RGBA -tint 50 7tintRGBA.png
The "-channel" setting seems to have no effect on the "-tint" operator, but it also had no effect in v6.9.7-6, my most recent version of IM6.

Replacing "-tint" with "-colorize" in those commands using "convert" or "magick" appears to obey the "-channel" setting and leaves the transparency as-is.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colorize part of an image

Post by fmw42 »

I believe that +level-colors also does not respect the channel setting and loses transparency also. The loss of transparency for both may be by design. I do not know. I have never before tried processing either with transparency.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Colorize part of an image

Post by GeeMack »

GeeMack wrote: 2017-05-28T13:29:35-07:00Replacing "-tint" with "-colorize" in those commands using "convert" or "magick" appears to obey the "-channel" setting and leaves the transparency as-is.
Revisiting this thread after working out some solutions for a similar task...

The "-colorize" and "-tint" operations will generate different results, so obviously one won't substitute directly for the other. If we require exactly the effect of "-tint", but also need to maintain areas of transparency, the OP's question can be done like this...

Code: Select all

magick input.png -modulate 100,0 \
   \( -size 100x100 gradient: -negate -fill red -tint 100 -write mpr:clut +delete \) \
   \( +clone -shave 1 -channel RGB mpr:clut -clut +channel \) -gravity center -composite output.png
The additional step there is to make a small black-white gradient image, "-tint" it, then put it in a "mpr:" to use as a color lookup table. When you add color to the original with that lookup table, by using "-channel RGB" and "-clut", the output is the same as tinting the original directly, but it will also keep any transparency.
Post Reply