After I rotate or deskew an image with Imagemagick there's a white background in the corners, where the rotation took place. Example:
convert image.png -rotate 10 out.png
Output: https://i.sstatic.net/pMRhl.jpg
Is there a way I could somehow fill up those white corners with some texture or at least a color that blends in with the image? Cropping is not an option.
I've found this great solution for simple rotation:
convert image.png -virtual-pixel Edge +distort SRT 10 out.png
Output: https://i.sstatic.net/zUIr8.jpg
But unfortunately it doesn't work with the -deskew
command...
So, does anyone know how to fill up those corners in a similar way for the -deskew
(and -rotate
) command? The point is to mask the fact the image was rotated as best as possible.
I'll go ahead and answer my own question, but I'm still hoping for a better solution.
You can set the -background
color, which works with rotate and deskew.
Here's a solution that uses the average image color:
convert image.png -background `convert image.png -resize 1x1 txt:- | tail -1 | cut -b 30-50` -rotate 10 out.png
Output: https://i.sstatic.net/9u6CH.jpg
And a better solution that takes some border pixels:
convert image.png -background `convert image.png -resize 100x1! \( +clone -crop 1x1+0+0 \) +append -crop 2x1+99+0 -resize 1x1 txt:- | tail -1 | cut -b 30-50` -rotate 10 out.png
Output: https://i.sstatic.net/vwki6.jpg
There's some bash scripting (inside the backticks), so these solutions are unix only.
Try this:
convert image.png -background transparent -rotate 10 out.png
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