I want to resize a 100x200 image to a new 400x400 image with ImageMagick.
So far I have the following command:
convert in.png -resize^ 400x400 -compose Copy -gravity center -extent 400x400 out.png
Now I want to read the color from the top, left pixel of the in.png and set it as background color of the out.png.
Does anybody know how to do it?
You can get the colour of the pixel ar top-left corner (coordinates: 0,0) like this:
convert in.png -colorspace rgb -format "%[pixel:p{0,0}]" info:
Output:
rgb(201,200,206)
If you are on OSX/Linux/Unix you can capture that in a variable and use it to set the background like this:
c=$(convert in.png -colorspace rgb -format "%[pixel:p{0,0}]" info:)
convert in.png -background "$c" ...
So, if we start with this image:
and do this:
c=$(convert in.png -colorspace rgb -format "%[pixel:p{0,0}]" info:)
echo $c # Check that puppy's colour
rgb(255,0,0) # Yep, it's red
convert -background "$c" in.png -resize 400x400 -gravity center -extent 400x400 out.png
we will get this:
If you are unfortunate enough to be on Windows, you will have to do some unintelligible stuff with FOR /F
that you will need to work out for yourself from here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With