How to change picture background color using ImageMagick? - linux

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

Related

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

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

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

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

ImageMagick best fit text within rectangle?

I have an image like this, with a rectangle at specific coordinates:
(for illustratory purposes I put the coordinates of the rectangle and its size and center in there)
Now I want to render some text with ImageMagick, so that it fits exactly within the rectangle.
If it's a very short (narrow) string, the rectangle's height will be the limiting factor:
On the other hand with a long (wide) string, the rectangle's width will determine the size:
In either case, independent of how short or long the text is, I would like to print it in one line (i.e. no word wrapping or multi line), and have it fit exactly in the rectangle, and make sure it's centered (the center of the text is in the center of the rectangle).
My questons:
How to do this 'best fit' feature with ImageMagick (I don't know how to dynamically determine the required -pointsize for this)
How to get the text centered, when I use -gravity center it seems to apply to the position of the text within the entire image, i.e. text coordinates become relative to the entire image's center. But I want to specify exact (absolute) coordinates, and that should be the center of the text.
For example, if I do this:
convert test.jpg -font Arial -fill yellow \
-pointsize 65 -draw "text 398,90 'Hello'" test2.jpg
I'm getting:
Note how the coordinates I specify (the rectangle's center) become the bottom left anchor point for the text! (this surprised me)
And if I do:
convert test.jpg -font Arial -fill yellow \
-pointsize 65 -gravity center -draw "text 148,-94 'Hello'" test3.jpg
I get:
Which is kinda OK, but note the weird text coordinates I have to use to get that. And besides I wouldn't know how to automatically calculate the pointsize (did the above by trial and error).
Updated Answer
With what I have now gathered, I think this may be your best option.
convert sea.jpg \( -size 173x50 -background none label:"A" -trim -gravity center -extent 173x50 \) -gravity northwest -geometry +312+66 -composite result.png
And this:
convert sea.jpg \( -size 173x50 -background none label:"A very much longer label" -trim -gravity center -extent 173x50 \) -gravity northwest -geometry +312+66 -composite result.png
Basically, I am using some "aside processing" in parentheses to generate the text and then compositing it onto the page afterwards. I generate the text with label: to the best automatic size, then trim off any excess space around the text. I then centre the trimmed text, using -gravity center and expand the background out (using -extent) so that the text box is always the same size, then I can position it (with -geometry) when compositing it relative to the top-left corner as I reset -gravity to NorthWest.
Original Answer
If you want ImageMagick to do its best to fit your text in a given box, you should use caption rather than annotate, label or -draw "text".
So you want to load your sea image, set the size for the caption, draw the caption and composite that onto the image at the correct spot using -geometry:
convert sea.jpg -size 173x50! caption:"Your text" -geometry +312+66 -composite result.png
Or, with longer text:
convert sea.jpg -size 173x50! caption:"A considerably longer text that will result in a smaller font being chosen" -geometry +312+66 -composite result.png
If you want a blank background, use -background none before caption:
convert sea.jpg -size 173x50! -background none caption:"Your text" -geometry +312+66 -composite result.png
If you want to centre your text, you can do that with PANGO, like this, but I believe you then give up the auto-sizing feature:
convert sea.jpg -size 173x50! -background none -gravity center pango:'<span foreground="yellow">ABC</span>' -gravity northwest -geometry +312+66 -composite result.png

imagemagick text as a hole on solid background

I am simply trying to create some text as image with IM.
I want the size of the image to be automatically adjusted to text length.
Also I need the background color to be a solid specified color (rgb 166,127,000 in this example), and text color to be "transparent". I mean text "as hole" on the background.
I could obtain it with the following command:
/opt/local/bin/convert \( -font /fonts/myFont.ttf -pointsize 25 -background none -fill 'rgb(166,127,000)' -gravity center +antialias label:'TEST' \) -channel a -separate +channel -negate -fill -alpha set -channel RGBA -fill none -opaque black -fill 'rgb(166,127,000)' -opaque white png:-
I am looking for a simpler way to do it. Also I am having problems with antialias and the text looks too bulky.
Thank You.

Resources