Page 1 of 2
Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-27T04:09:01-07:00
by tackd
System; Linux Mint 18.3
ImageMagick 6.8.9-9 Q16 x86_64 2018-09-28
Source: Scanned Typed Document, [US Letter 8.5x11 inches], (2550 x 3300 x 300 dpi)
Objective: deskew the above typed letter, and extend borders for padded fit, centered 'without' changing the size of the typed content.
Code: Select all
convert <image> -filter catrom -background white -deskew 80% +repage -gravity center -extent 2550x3300 <result>
Since the background might not be pure 'white'...
-How can I extend the canvas with the existing color of the page border
-How can I make sure the original type size will remain the same, only deskewed
-Will "-background none" use the existing page edge color for the canvas ?
Thank you.
Re: Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-27T08:58:15-07:00
by snibgo
tackd wrote:-How can I extend the canvas with the existing color of the page border
Use an operation that uses virtual pixels, for which the default is "edge". For example:
Code: Select all
convert rose: -define distort:viewport=200x200-20-30 -distort SRT 1,0 out.png
I don't understand your other questions. If you don't resize the image, then type size will remain the same. "-background None" and "-virtual-pixel Edge" do different things.
Re: Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-27T09:19:57-07:00
by tackd
Thank you for your review snibgo.
I don't know how to incorporate your example into my existing Code to
extend the edge color to pad the canvas to a specific size, and maintain the original content size.
Regards
Re: Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-27T09:38:59-07:00
by snibgo
Did you try my command? Then you will see that it extended the built-in "rose:" to 200x200 pixels, with a 20 pixel border at the left, and 30 pixel border at the top.
Use whatever numbers are suitable for you.
Re: Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-27T09:50:05-07:00
by tackd
snibgo,
Every scanned document has a varying size since they were scanned with an "auto crop" feature.
I therefore can't enter pixel dimensions individually as in your advanced example.
The only constant I know is the resulting size US Letter @ 300 dpi , 2550x3300
I don't know if you wanted me to drop your code into my exising working code, or if it uses the color of the existing edge to extend the canvas
so I don't have to include "-background white", and isn't "-distort" for resizing ? I have to keep the existing document type size after the deskew.
Regards.
Re: Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-27T10:14:16-07:00
by snibgo
You want the same output size, 2550x3300, for all inputs? Then use that as the first part of the viewport.
I used "-distort SRT 1,0", which scales by a factor of one and rotates by zero degrees. So it doesn't change the type size.
"-background White" is useful for deskew.
If you post a sample input, I can give more specific advice.
Re: Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-27T10:57:38-07:00
by tackd
I am trying to achieve whatever output size the original is.
This is an example of skewed document text;
https://www.codeproject.com/KB/graphics/1045825/ex3.png
The simplest use;
Code: Select all
convert <image> -filter catrom -deskew 80% +repage <result>
It works, deskews... but the resulting size is changed to accomodate the deskew.
If I add "-background white", the color will not match, I'm wondering if IM can extend the canvas using
the existing edge color. Does that help explain what I'm trying to acomplish ?.
Thank you.
Re: Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-27T11:32:08-07:00
by fmw42
You can test the input image for its most frequent color and input width and height and save those in variables. Then use -background storedcolor -extent StoredwidhtxStoredheight. In ImageMagick you can do that all inline. If not using IM 7, then the syntax to store and use variables depends upon our OS.
If you need to extend the output and keep your lines showing, then user snibgo is correct in needing to use distort SRT to rotate the image with a virtual canvas and virtual-pixel edge to achieve that.
Since you are on Linux, you could try my script, textdeskew, at my link below. If needed, I can add arguments to do the extent. Also see my script, unperspective.
Re: Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-27T12:03:46-07:00
by snibgo
tackd wrote:This is an example of skewed document text;
Is that the actual input image? It seems to be a screenshot of the input and output side by side.
Please post the actual input image.
Re: Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-27T14:02:26-07:00
by tackd
OK,
1. Here is an original: Document-Skewed_Before_IM.png
2519x3245 pixels @ 300 dpi
https://imageshack.com/a/img924/9350/gwXqy6.png
2. After deskew IM: Document-Skewed_After_IM.png
https://imageshack.com/a/img921/4414/RDdmSi.png
2659x3353 @ 300 dpi
Code: Select all
convert Document-Skewed_Before_IM.png -deskew 80% +repage Document-Skewed_After_IM.png
So, I wish to maintain #1 pixel dimensions, but deskew with the color of the original filled in the pad, not the default white,
every image will have a different color, probably not pure white for the paper color.
If I understand correctly, "-gravity center -extent ????x????" will center over white without changing the size of the original.
Re: Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-27T14:22:59-07:00
by fmw42
-extent will either crop or pad with whatever you have defined as the background color. So if your arguments are larger than the original, it will fill with background color.
Re: Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-27T22:21:52-07:00
by tackd
Thank you for your attention snigbo and fmw42.
In the #2 example I uploaded:
https://imageshack.com/a/img921/4414/RDdmSi.png
can IM automatically fill the resulting white areas after the deskew, with the same color as the original edge ?
Re: Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-27T23:01:34-07:00
by fmw42
If I understand what you want, I think to do that one would have to get the deskew angle and then use distort to do the deskewing with -virtual-pixel edge.
See
https://imagemagick.org/script/escape.php for accessing the deskew angle without having to do -deskew
Does this do what you want?
1) This will expand the image as much as needed for the deskew
Code: Select all
angle=`convert image.png -deskew 40% -format "%[deskew:angle]" info:`
convert image.png -virtual-pixel edge +distort SRT "1 -$angle" +repage image_deskew.png
2) This will keep the output the same as the input in size.
Code: Select all
angle=`convert image.png -deskew 40% -format "%[deskew:angle]" info:`
convert image.png -virtual-pixel edge -distort SRT "1 -$angle" +repage image_deskew2.png
Re: Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-28T05:07:33-07:00
by tackd
Wow, that's really great Fred !
Thank you so much.
To confirm...
1. Both examples are keeping the original size of the typed words ?
2. In #2, what is your recomended method to auto crop to the original size: 2659x3353 -> 2519x3245,
keeping the resulting deskew content centered ?
3. If I wished to customize where the deskew was positioned on the result, should I use the -gravity setting ?
Best regards.
Re: Extend Canvas with unknown color, maintain content size...
Posted: 2018-10-28T12:30:54-07:00
by fmw42
1) Yes, that should be true since it is only a rotation
2) The second method should already be the same size, but if you need to crop and center, then try
Code: Select all
ww=`convert -ping image.png -format "%w" info:`
hh=`convert -ping image.png -format "%h" info:`
echo "${ww}x${hh}"
angle=`convert image.png -deskew 40% -format "%[deskew:angle]" info:`
convert image.png -virtual-pixel edge -distort SRT "1 -$angle" +repage \
-fuzz 5% -trim +repage \
-set option:distort:viewport "${ww}x${hh}+%[fx:(w-$ww)/2]+%[fx:(h-$hh)/2]" \
-distort SRT "1 0" image_deskew3.png
3) Just add your offsets to the viewport fx terms for offsets in x and y respectively