How to replace one color in a image with another one? - colors

I've this image:
Its background (and only) color is "#687a40" but want to change it for the color "#9ea09a" (a light grey).
I've tried with this command...:
gm convert photo.png -fuzz 0% -fill "#9ea09a" -opaque "#678a40" photoGREY.png
...but I've not seen any effect on the image. I'm sure I'm doing something wrong but GM's documentation is not very friendly.

Your image has an alpha channel, so you need:
gm convert input.png -fill "#9ea09a" -opaque "#687a40ff" result.png

Related

Can't fit text to image with ImageMagick

I need to fit my text to image. My image have different sizes so i can't set constant pointsize.
My command looks something like this
convert
-fill white
-font Winter Calligraphy
-size `${options.width}x${options.height}`
label: KJHGFD
test.gif
on output you can see cropped part on top of picture.
Output:
I have problem only with this fonts, other fonts works great.
I tried to add white border on top. Unfortunately, this only moved the damaged text to bottom.
I can't change area size.
Text must fill as much space as possible.
I need to use Winter Calligraphy font
Here is a slightly kludgy way of getting the result you want. Here are the steps:
First, use caption: to get ImageMagick to tell you the pointsize it would use to fill your text box and extract that info
Create a canvas twice as wide and twice as tall as the one you really want and draw your text in the middle of that - it is bound to fit!
Now trim away the extraneous background around the text so you have the absolute minimum bounding box to contain the text
Resize the result to your desired size.
#!/bin/bash
# Width, height and text
w=600
h=150
text="KJHGFD"
# Get pointsize ImageMagick thinks is good
pointsize=$(convert -gravity center -background black -fill white -size ${w}x${h} \
-font "Winter Calligraphy.ttf" caption:"$text" -format "%[caption:pointsize]" info:)
echo ImageMagick likes pointsize: $pointsize
# So draw text in that size on larger canvas, trim to bounds of letters and resize to desired size
wb=$((w*2))
hb=$((h*2))
convert -gravity center -fill white -size ${wb}x${hb} xc:black \
-font "Winter Calligraphy.ttf" -pointsize $pointsize -annotate 0 "$text" \
-trim +repage -resize ${w}x${h}\! result.png
This works for me in ImageMagick 6.9.10.97 Q16 Mac OSX. I have added -background white -fill black -gravity center to your command.
convert -background white -fill black -font "/library/fonts/Winter Calligraphy.ttf" -size 569x196 -gravity center label:KJHGFD test.gif

How to change picture background color using ImageMagick?

I want to change my picture background color, This is my source http://cdn.mayike.com/emotion/img/attached/1/image/dt/20170916/18/20170916180007_833.png.
If I want to change the background color to another color, what should I do?
If I want to change the background color to transparent, what should I do?
I tried using convert aa.png -background '#0e0e0e' bb.png, but that doesn't work.
I do not think you will get a perfect result due to the fact that your image is not binary. Nevertheless in Imagemagick you have two choices. First you could just change all white to red:
convert 20170916180007_833.png.jpeg -fuzz 25% -fill red -opaque white -flatten result1.png
Or you can do a flood fill to just change the outer area:
convert 20170916180007_833.png.jpeg -fuzz 25% -fill none -draw "matte 0,0 floodfill" -background red -flatten result2.jpg
To apply any background color, first the program should know the edges and background.
The image you use doesn't do that.
Although your command is correct, it doesn't work since edges and background is not distinguished. So we use Masking first
First run this to get edges:
convert aa.png -bordercolor white -border 1x1 \
-alpha set -channel RGBA -fuzz 20% \
-fill none -floodfill +0+0 white \
-shave 1x1 aa.png
Now you'll get the edges saved in aa.png. At this point your background is transparent. Next run the background color change command:
convert aa.png -background blue -flatten bb.png
Now you'll have the expected result.
Output Final Image
Source:http://www.imagemagick.org/Usage/masking/#bg_remove
Usually -opaque, or -transparent, is all that's needed. But because the image is black & white, we need to isolate ROI to effect. I would recommend using the MVG draw commands.
If change to another color, what should I do?
convert 20170916180007_833.png \
-fill '#0e0e0e' -fuzz 20% \
-draw 'color 0,0 floodfill' \
output_color.png
If change to transparent, what should I do?
convert 20170916180007_833.png \
-fill 'transparent' -fuzz 20% \
-draw 'color 0,0 floodfill' \
output_transparent.png

GraphicsMagick / ImageMagick replace all non-transparent pixels (like Photoshop's Color Overlay)

I'm trying to replace all non-transparent pixels to a given color using GraphicsMagick for Node.
Using a composite image is not an option, I simply need to change every non-transparent pixel to a given color.
Original image:
Goal:
Transparent pixels should stay transparent. I'm trying to achieve Photoshop's Color Overlay effect:
This is a bit simpler. In ImageMagick do the following:
convert 84xHk.png -fill "#E91FCB" +opaque none result.png
I'm not familiar with Node's GraphicsMagick library, but there are a few methods to achieve this. Here's a few I can think of...
Extract alpha, and replace colors
convert 84xHk.png -alpha extract \
-negate -fill '#E91FCB' -fuzz 50% \
-opaque black output.png
Create solid color image, and copy alpha channel
convert 84xHk.png \
\( +clone -alpha off \
-fill '#E91FCB' \
-draw 'color 0,0 reset' \
\) +swap -compose CopyOpacity -composite output.png
Use FX expressions
convert 84xHk.png -fx 'p.a==1?#E91FCBFF:#E91FCB00' output.png

Using ImageMagick to remove all color except black in an image?

The situation is : I have many images of documents from scanning. I want to keep the document's main content - which is printed in color black (a small range of colors around #000000). But, you know, the documents are always full of colors : stamp, background, decorations, logos...etc.
I just want to keep the TEXTS ONLY which were printed in the color black.
I've tried with ImageMagick and this command so far:
convert X.png -matte (+clone -fuzz 20% -transparent "#000000") -compose DstOut -composite X1.png
But the result was not as expected , the text was very damaged that I cannot read.
Someone suggested me to increase the fuzz to 70%:
convert X.png -matte (+clone -fuzz 70% -transparent "#000000") -compose DstOut -composite X1.png
Then the text appeared to be more readable, but the other colors kept remaining too.
Can you please show me a better solution for my situation? Every suggestion would be highly appreciated!
To match all colors except black you can use +opaque "#000000".
In order to include a little range around #000000 you can try different percentages with the fuzz operator:
convert input.png -fill white -fuzz 10% +opaque "#000000" result.png
Tested with ImageMagick 6.6.0-1 on Windows
I stumbled upon this question while looking for an answer to this question. I found a couple imagemagick forum posts that helped. Here is what I came up with:
convert input.gif -matte \( +clone -fuzz 1 -transparent black \) -compose DstOut -composite ~/Desktop/output.png
Forum post: ImageMagick: Removing all but one color
You should try the level command:
convert test.png -level 25%,75% test_level.png

How to convert SVG image to 32-bit RGBA with ImageMagick?

How to convert SVG image to 32-bit RGBA with ImageMagick?
Background must be transparent.
Simply try this command:
convert in.svg -transparent white out.png
(Assuming the original SVG uses a transparent or white background. Otherwise, if SVG background is red or blue or '#235689': use -transparent red, -transparent blue or -transparent '#235689'....)
If your version of ImageMagick doesn't do it right, check if convert -list configure | grep svg shows rsvg output in the DELEGATES and CONFIGURE lines.
I found that setting background to none worked for me:
convert -background none in.svg out.png

Resources