Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adaptive background after rotation/deskew in Imagemagick

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.

like image 245
HairyFotr Avatar asked Oct 15 '25 10:10

HairyFotr


2 Answers

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.

like image 75
HairyFotr Avatar answered Oct 17 '25 03:10

HairyFotr


Try this:

convert image.png -background transparent -rotate 10 out.png
like image 29
Bonzo Avatar answered Oct 17 '25 04:10

Bonzo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!