Convert RGB to Grayscale in ImageMagick (but not the entire image) - imagemagick

I have the following bar chart:
I transformed it to greyscale using the following command:
convert image.tif -set colorspace Gray -separate -average image_greyscale.tif
and my result was
It is greyscale indeed, but the axes and the legend are grey as well. This is obvious now, but I'd like them to be black, like in the original image. Something like this:
How can I do it? Remake the bar charts again, with a greyscale palette, is not possible right now.

I think what you want to do in ImageMagick is simpler than your command. Just do
Input:
convert barchart.png -colorspace gray result.png
Result:

You can select the plum colour and change that to gray20 and then select the lime colour and change it to gray80 like this:
magick chart.png -fuzz 10% \
-fill gray20 -opaque "rgb(68,1,84)" \
-fill gray80 -opaque "rgb(122,209,81)" result.png
Or, as a one-liner:
magick chart.png -fuzz 10% -fill gray20 -opaque "rgb(68,1,84)" -fill gray80 -opaque "rgb(122,209,81)" result.png

Related

ImageMagick replace white with red not working

This is the image:
The light part isn't perfect white but FDFDFD
I tried:
old.png -fuzz 5% -fill red -opaque white new.png
but the white becomes black!
Your image is greyscale. Try making it colour first:
magick old.png -colorspace sRGB -fuzz 5% -fill red -opaque white new.png

Auto crop text(signature) from image and change background color using Imagemagick

Need to auto crop text(signature)from an images(sample image:Image1 )and Need to change background color of cropped image.
Need to achieve this using Imagemagick.
Is it any possible way to achieve these ?
I am using version ImageMagick 7.0.7-28
williamson image
example image
What do you mean by auto-crop? You can use ImageMagick -trim to get the signature cropped to its bounding box and then use -fuzz -opaque to change the background.
Input:
magick signature.png -fuzz 20% -trim +repage -fill pink -opaque white result.png
Or you can just make the background transparent.
magick signature.png -fuzz 20% -trim +repage -transparent white result2.png
I do not see any color change in the text, though it is not quite as smooth. Adjust the fuzz value as desired to remove the gray and keep the text as smooth as you can.
Input:
ImageMagick 7 command:
magick signature3.jpeg -fuzz 15% -fill white +opaque "#5B000C" -trim +repage result.png

ImageMagick: How to make both black and white transparent?

Converting black and dark shades to transparent works fine with ImageMagick.
I even managed to perform a crop and resize in the same line.
convert input.png -background none -fuzz 45% -transparent black -flatten -crop 640x480+12+9 -resize 105% output.png
However, the input image also contains a number of almost white lines, which I also would like to convert to transparent in the output.
How do I go about that? Is it possible to do it within the same command line?
Sure, just add a second -transparent.
convert -size 512x512 gradient:black-white a.png # create initial black-to-white gradient
convert -fuzz 20% a.png -transparent black -transparent white result.png # lose 20% off black end and white end
Or, with extra fuzz...
convert -fuzz 40% a.png -transparent black -transparent white result.png

How to output white png version of my image ?**

With ImageMagick shell command (convert?)...
Given a colorful input.png image.
How to us input.png to produce a white output.jpg version with similar dimensions an opacity of 100% ?
I will later on use this layer in my workflow.
This should work:
convert input.png -threshold -1 output.jpg
This transforms any pixel with an intensity greater than (-1), i.e., all of them, to white.
It does not work with GraphicsMagick, though, because in GM the threshold value is unsigned (in ImageMagick it's a signed "double"). Neither of the applications documents exactly what is supposed to happen when the threshold is negative.
Here's a command that works on both ImageMagick and GraphicsMagick, and is documented:
[gm] convert input.png -fuzz 100% -fill white -opaque gray output.jpg
You could use fill, like this:
convert input.png -fill "#ffffff" output.jpg
or
convert input.png -fill white output.jpg
Or you can convert all three channels (red, green and blue) to "1" which is full intensity, like this:
convert input.png -channel red -fx "1" -channel green -fx "1" -channel blue -fx "1" output.jpg

Set any white pixels to transparent in ImageMagick using a threshold?

Using ImageMagick, I want to find any pixels that are white and make them transparent. I'm thinking the key is -threshold but I can't work out how to change a white pixel to transparent using -threshold. Any suggestions most appreciated.
convert input.png -fuzz 10% -transparent white output.png
Note that the order of the arguments is important - -fuzz must come before -transparent.
Would something like this work?
convert input.jpg -fuzz 5% -fill to_color -opaque from_color output.jpg

Resources