Page 1 of 1
Crop and Straighten Function in Imagemagick? (trim fuzz)
Posted: 2008-08-25T11:07:53-07:00
by simagick
Hi,
Does anyone know if its possible to use Imagemagick to perform an action similar to Photoshops 'crop and straighten' plugin? By this I mean cutting an unwanted border off an image, and rotating the remaining image to a straight position (think of a picture laid down slightly rotated on a scanner)
I've tried many things with -trim and -fuzz -trim, but i cant get it quite accurate and stable, and cant find a fuzz factor that leaves enough and yet crops enough on varying images.
I also read about MagickTrimImage(), but I haven't managed to install image magick php on mac osx 10.5 leopard, so I can't test it. Does anyone know if MagickTrimImage works better than -trim and if MagickTrimImage is possible to use straight from the command line?
Many thanks,
Sim
Re: Crop and Straighten Function in Imagemagick? (trim fuzz)
Posted: 2008-08-25T11:16:45-07:00
by fmw42
see IM function -deskew
also I have a bash script called unrotate that will do that also
http://www.fmwconcepts.com/imagemagick/index.html
Re: Crop and Straighten Function in Imagemagick? (trim fuzz)
Posted: 2008-08-26T02:51:08-07:00
by simagick
Hi,
Many thanks! I've tried some things with -unrotate of your site, but am getting weird results when the image is not on a white but a colored background. The subject gets rotated properly, but the trim does not work - it gives the outcoming image an additional white border, in which the colored background area (intended to be cropped off) is rotated along with the subject.
To explain if you start with Miss Zelda on a 300x300 brown background at 45degrees, and use 'unrotate -f 20', then the script makes a first rectangular crop to the bounding box of the (still rotated) Miss Zelda. Then, it corrects the rotation of that whole image (so including the brown triangular leftovers on each side of Miss Zelda), and puts that on a white background. So the outcome is a white rectangle, in which a rotated brown rectangle, in which a straight Miss Zelda.
Do you think the script could work on colored background images?
Thanks,
Sim
Re: Crop and Straighten Function in Imagemagick? (trim fuzz)
Posted: 2008-08-26T10:14:26-07:00
by fmw42
Hello Sim,
Thanks for the note about the problem. I see that it does not work for non-white backgrounds. I guess I did not test anything but white.
It should now be fixed. Download it again and try it and let me know.
Also you should try the IM function -deskew. It might work properly on a colored background.
Fred
Re: Crop and Straighten Function in Imagemagick? (trim fuzz)
Posted: 2008-08-26T23:14:26-07:00
by anthony
Warning while Fred's script is designed with very large rotations in mind, -deskew is designed with very small rotation corrections in mind, thats is rotations from scanned documents.
Basically -deskew does work, but with some limits as to how much it will de-rotate an image.
Both however have limitations in that the document has a rectangular border for it to work with. It will not for example de-rotate an image of slanted text (although the algorithm -deskew uses should be able to do this).
I have not generated examples for this operator due to a lack of understanding of the operator on my part.
Re: Crop and Straighten Function in Imagemagick? (trim fuzz)
Posted: 2008-08-27T04:10:48-07:00
by simagick
Fred, Thanks a lot! That seems to work very well. I will test on and let you know my findings.
One first question would be, there seems to be a considerable drop of image quality and sharpness in the outcoming image, is that something that can be adjusted in the process? (im using -quality 90)
In fact it is the same quality loss when using just 'convert -rotate -45'
Anthony, I've indeed searched after -deskew, but I can't find the proper syntax. From the documentation it seems something with -set? I've tried many things but can't get it to work. Do you know the syntax?
Thanks a lot,
Sim
Re: Crop and Straighten Function in Imagemagick? (trim fuzz)
Posted: 2008-08-27T05:37:39-07:00
by simagick
Fred,
Trying on a few things with -unrotate, I wondered if you image it possible to work a picture containing 2 or more slightly rotated Zelda's to separate files?
I mean like the PS 'crop and straighten' plugin, the part referred to as 'gang scan' - several pictures on a scanner all slightly rotated.
Thanks,
Re: Crop and Straighten Function in Imagemagick? (trim fuzz)
Posted: 2008-08-27T13:14:26-07:00
by fmw42
simagick wrote:Fred, Thanks a lot! That seems to work very well. I will test on and let you know my findings.
One first question would be, there seems to be a considerable drop of image quality and sharpness in the outcoming image, is that something that can be adjusted in the process? (im using -quality 90)
In fact it is the same quality loss when using just 'convert -rotate -45'
Anthony, I've indeed searched after -deskew, but I can't find the proper syntax. From the documentation it seems something with -set? I've tried many things but can't get it to work. Do you know the syntax?
Thanks a lot,
Sim
The jpeg quality is not something that is controlled in the script. My script converts to the output image without regard to its type and so uses whatever is the IM defaults for that image type. IM has a -quality setting. So I would suggest that you first use -unrotate to generate a png image and then convert the png image to jpg via a normal IM convert using the -quality setting to get what you want. The default IM quality is 85.
My script will ONLY handle one image in a background. I have no plan to do more than one. However, Anthony has developed something that may do that. You can search the archives as there was some discussion about that a while ago. He has some script that he developed. But I will let him comment about it.
The syntax for -deskew is
convert <input> -deskew XX% <output>
The XX% is the fuzz value in matching the background color. I am not sure how -deskew gets the background color. The SET option allows you to notify -deskew of the size of the border area of background color around the image.
Unfortunately, I have not had too much success with it. I can rotate zelda by 5 degrees and then deskew it with -deskew 40%. But if the background is not white, it does not deskew. Also I cannot get it to deskew for large angles. I tried 45 degrees and white background, but got no deskew. I think it was designed for small angles and white background. But perhaps Anthony or Magick can comment here.
Re: Crop and Straighten Function in Imagemagick? (trim fuzz)
Posted: 2008-08-27T16:59:10-07:00
by anthony
simagick wrote:Anthony, I've indeed searched after -deskew, but I can't find the proper syntax. From the documentation it seems something with -set? I've tried many things but can't get it to work. Do you know the syntax?
It takes a value that appears to be related to -fuzz, but it is unclear.
I would just use
+deskew which simply does a
-deskew 40% whatever that means exactly!
Neither method handles a 'gang scan' as you put it.
Re: Crop and Straighten Function in Imagemagick? (trim fuzz)
Posted: 2008-08-27T17:02:48-07:00
by anthony
The fuzzyness is a problem with the default -rotate of IM.
The -distort rotate should be better but will also be a little fuzzy.
the best way to improve rotate quality is to DIY it and use a super sampled method.
that is enlarge the origina image, deskew or unrotate then resize the image back to its original size.