Using imagemagick, I'm looking to add an expanded background color to an image. My source looks like this:
And this is the desired output (the background color will be white, but this is for clarity):
Step 1 seems to be filling the background with my desired color, but I can't find a way to remove the background outside the shape. This will also be a problem if the source image contains any of the desired background color already (because it will be made transparent).
Ideas?
It is not perfect but should do the job:
convert ( in.png -resize 200% -flatten -negate -morphology Dilate Disk:20 \
-fuzz 90% -fill none -draw "matte 0,0 floodfill" -fill green \
-colorize 100% -resize 50% ) in.png -composite out.png
Related
I'm trying to reduce the number of colors in the image below using -remap in imagemagick.
olympic-logo.png
colortable.png which consists of this two color A12E64, FF0000
Using the following code:
convert olympic-logo.png +dither -remap colortable.png olympic-logo-remap.png
Output:
olympic-logo-remap.png
Expected Output:
olympic-logo-expected.png
Is there a way to ignore transparent area so It won't get mapped to get the expected output?
Thanks and more power.
You can put a copy of the original image to one side before doing what you already did and then restore the alpha from that afterwards like this:
convert rings.png -write MPR:orig +dither -remap colortable.png MPR:orig -compose copyalpha -composite result.png
where MPR: is a "Memory Program Register", i.e. a named lump of RAM.
Your input image does not have a constant background color. It is mostly black with a large white border. You can see that if you turn alpha off:
convert olympic-logo.png -alpha off aoff.png
So you can modify Mark Setchell's command by adding -background black -alpha background to it.
convert xc:"#A12E64" xc:"#FF0000" +append colortable.png
convert olympic-logo.png -background black -alpha background -write MPR:orig +dither -remap colortable.png MPR:orig -compose copy_opacity -composite result.png
Does this now work for you? If not, try making the background all white.
I am using Imagemagick to work with images in php. I am no-wise in ImageMagick so could not do it. I have 2 Pictures, 1 is background and other one is above it. The one which is on top is of gray color png. While the background image can be any image. I want to set my top image's color to match the most color of background image.
For Example, this is a random background image which has beige/pink as its main color
and this is my top image
I want my above top image to change its color to match the most color of background image, as in the above image, it should be something like
Is it possible?
I don't feel like writing and debugging a load of PHP today, but can show you some techniques on the command line that you should be able to translate into PHP.
You can get the mean of the background image by resizing it to a single 1x1 pixel and then printing its value in RGB terms:
convert background.jpg -scale 1x1 -format "%[pixel:p{0,0}]" info:
srgb(219,199,164)
If I take that value and make a solid square out of it, you can see it is a beige colour like you suggest:
convert -size 100x100 xc:"srgb(219,199,164)" mean.png
You can probably use getImageChannelStatistics()
in PHP for that.
If I now take that colour, and make it the fill colour for tinting and apply a tint, I get this:
convert top.png -fill "srgb(219,199,164)" -tint 100% result.png
In PHP, you'd be looking at tintImage().
Something horrible has happened down the right side - I don't understand that, but if I extract the opacity from your top image and re-apply it to the result image, it goes away:
convert top.png -alpha extract alpha.pgm
convert top.png -fill "srgb(219,199,164)" -tint 100% alpha.pgm -compose copyopacity -composite result.png
I have solved it myself through these 2 lines of codes
exec("convert fabric.jpg -scale 1x1\! -format '%[pixel:u]' info:-", $a);
exec('convert arm-shadow.png -fuzz 10% -fill "'.$a[0].'" +opaque black -fill "'.$a[0].'" -opaque black foo.png');
I do have fowllowing article pics, here as an example a mirror. Of course there can be other geometric forms like bottles, chairs.
The pictures are all in JPEG-format. I want to convert these files to PNG-format. But I want to get rid of the outer white background.
Is there a way to do it by script in php oder ImageMagick?
You can do it like this:
convert frame.jpg -fuzz 10% -fill none -draw 'color 10,10 floodfill' result.png
I have drawn the effect in red here so you can see it.
You can use ImageMagick's -draw to isolate a color, and "floodfill" it to transparency.
convert input.jpg \
-fill transparent \
-fuzz 20% \
-draw 'color 15,15 floodfill' \
out.png
See Color Fill Primitives for other great examples.
For PHP's Imagick library, you would do something like...
$img = new Imagick('input.jpg');
$draw = new ImagickDraw();
$draw->setFillColor('transparent');
$draw->color(15,15, Imagick::PAINT_FLOODFILL);
$img->drawImage($draw);
$img->writeImage('out.png');
I really need some help to understand the image channel problem,
I used ImageMagick to gen a text image with jpg 300 dpi RGB.
I then merge this image on the top of a background image(jpg 300 dpi CMYK 8bits/Channel).
I find the text image is normal when open this by default/photoshop, however when I merge to the background image, it have become inverting the color tone, black and white.
can anybody tell me what's wrong with the image?
please help I have tried couple of hours however still no answer
I'm using ImageMagick 6.7.9-10
This is the command I ran:
convert -background white -fill black \
-font 'Brandon_reg.otf' \
-pointsize 9 -size 445x -density 300 -gravity center\
caption:'Why this message become invert the black and white?' \
msg.jpg"
convert background.jpg msg.jpg \
-gravity center -composite output.jpg
please see this image
I'm currently looking for a way to crop an image, but on an angle.
I don't think I can just rotate the image first as the script is supplied with specific x,y coordinates of each corner.
So if you can imaging this, image is uploaded, 1280x720.
Along with the image it's supplied with x,x coordinates for the crop zone.
However the top left and top right coordinates will not have the same y position.
Heres an examples
Before
After
Any ideas ?
You'll still need to use trigonometry methods to rotate the image, but you can mimic a crop-at-an-angle by mixing opacity copying and trimming.
First. Create an Image Mask
If all the points are giving to you, and the image size is defined, simply draw the area that needs to be extract
WIDTH=819
HEIGHT=616
TOP_LEFT=669,117
TOP_RIGHT=784,155
BOTTOM_LEFT=544,495
BOTTOM_RIGHT=659,534
convert -size $WIDTHx$HEIGHT xc:black -fill white -stroke white \
-draw "polyline $TOP_LEFT $TOP_RIGHT $BOTTOM_RIGHT $BOTTOM_LEFT" \
mask.png
Masking and Background Removal
This method of masking will turn off the alpha-channel and set the background to transparent. When we compose the two images, the resulting image will only display what's within the area we defined in the mask. (note: you may need to adjust the -background to white, or transparent.)
convert source.jpg mask.png -alpha Off -compose CopyOpacity \
-composite -background transparent copyOpacity.png
Calculate Degree to Rotate
If you have two points on a square angle, you should be able to follow the atan method. Most language will have an atan2 function. Other trigonometry questions "Rotating a rectangle" & "How to calculate the angle between two points relative to the horizontal axis?"
DELTA_Y=$(($HEIGHT-155-534))
DELTA_X=$((784-659))
DEGREE=`awk "BEGIN { pi=4.0*atan2(1.0,1.0)+90; print atan2($DELTA_Y,$DELTA_X)*180/pi; }"`
convert copyOpacity.png -rotate $DEGREE -trim final.png
Luckily, you can do everything in one step.
#!/bin/bash
WIDTH=819
HEIGHT=616
TOP_LEFT=669,117
TOP_RIGHT=784,155
BOTTOM_LEFT=544,495
BOTTOM_RIGHT=659,534
DELTA_Y=$(($HEIGHT-155-534))
DELTA_X=$((784-659))
DEGREE=`awk "BEGIN { pi=4.0*atan2(1.0,1.0)+90; print atan2($DELTA_Y,$DELTA_X)*180/pi; }"`
convert source.jpg \( -size $WIDTHx$HEIGHT xc:black -fill white -stroke white \
-draw "polyline $TOP_LEFT $TOP_RIGHT $BOTTOM_RIGHT $BOTTOM_LEFT" \) \
-alpha Off -compose CopyOpacity -composite \
-background transparent -rotate $DEGREE -trim \
final.png