Page 1 of 1

GIMP/ImageMagick distort SRT difference

Posted: 2013-11-25T07:20:56-07:00
by tsanders
Hi,

I have written a python script for Gimp to visually align two images together using layer rotation and translation. I want to use the values found in Gimp to batch-process a set of similar images in IM, using -distort SRT.
The SRT values for Gimp produce slightly different results in IM. If the rotation is 0, the translate is identical to the Gimp result, but when an image is rotated (I am using values between 0.05 and 0.5 degrees plus and minus) the horizontal and vertical scale starts to differ from the Gimp results. 0.05 degrees is still OK, but higher values produce higher differences.
(Scale factor for SRT is always 1.0 )

Does anyone have the same experience? Is there a solution for this?
TIA for any advice
Tom

Re: GIMP/ImageMagick distort SRT difference

Posted: 2013-11-25T08:30:28-07:00
by snibgo
Is your python script doing the operations in the same order as IM? Rotate then translate, not translate then rotate.

Are your units the same? IM uses degrees where other languages commonly use radians.

If that doesn't help, posting some clear examples might help. Use angles much larger than 0.5 degrees, eg 30 degrees, to make movements more clear.

Re: GIMP/ImageMagick distort SRT difference

Posted: 2013-11-25T13:15:39-07:00
by fmw42
I get virtually the same result using IM -distort SRT and GIMP for 20 degree rotation. There is a nominal 0.5 pixel offset unless you use image coordinates in IM. See http://www.imagemagick.org/Usage/distor ... oordinates That basically means you need to specify the center pixel for the rotation with 0.5 added as follows, using the 256x256 zelda image.

convert zelda1.jpg -virtual-pixel none -distort SRT "128.5,128.5 1 20 128.5,128.5" zelda1_srt20b.png

see http://www.imagemagick.org/Usage/distorts/#srt

This may be more an issue for very small rotatations.

Re: GIMP/ImageMagick distort SRT difference

Posted: 2013-11-25T14:06:19-07:00
by tsanders
Hi Snibgo,

Thanks for your input. The order of rotation and translation makes no difference. The units do not seem to be the problem: the rotation is correct, the problem is in the translation. I tried to increase the angle (great idea!) and that showed that I mistakenly thought that the scaling went wrong. This is NOT the case. The translation does something unexpected. The larger the rotation, the further the translation offsets from the center.
I have made a zip file containing 3 sample files, the IM commandlines and the resulting images:
- 001.jpg is turned into result1.jpg: 0 degrees rotation, just translation. This is the correct result
- 002.jpg is turned into result2.jpg: -0.15 degrees rotation and the same translation. The result has a small offset compared to result1.jpg
- 003.jpg is turned into result3.jpg: 4.85 degrees rotation and the same translation. The offset is huge!

The images are 2816x2112px.

If you use the same values in GIMP (layer-transform) the results are fine (all like result1.jpg).

I've put the zip file (7.5 Mb) on our server:
http://www.dgsvisueel.nl/srttest.zip

I am using Gimp 2.8.8 and IM 6.8.5-3 on Ubuntu 12.04 LTS 64Bit

Re: GIMP/ImageMagick distort SRT difference

Posted: 2013-11-25T14:18:56-07:00
by tsanders
Hi fmw42,
The zelda picture is probably too small to see the effect. I am using 2826x2112px. -virtual-pixel none does not make any difference.
fmw42 wrote:I get virtually the same result using IM -distort SRT and GIMP for 20 degree rotation. There is a nominal 0.5 pixel offset unless you use image coordinates in IM. See http://www.imagemagick.org/Usage/distor ... oordinates That basically means you need to specify the center pixel for the rotation with 0.5 added as follows, using the 256x256 zelda image.

convert zelda1.jpg -virtual-pixel none -distort SRT "128.5,128.5 1 20 128.5,128.5" zelda1_srt20b.png

see http://www.imagemagick.org/Usage/distorts/#srt

This may be more an issue for very small rotatations.

Re: GIMP/ImageMagick distort SRT difference

Posted: 2013-11-25T14:49:00-07:00
by snibgo
SRT with 6 arguments (X,Y Scale Angle NewX,NewY) first scales (but yours doesn't), then rotates about X,Y, then translates from X,Y to NewX,NewY.

So your rotate is about (0,0), the top-left corner. Perhaps you'd prefer to rotate about the centre, eg:

Code: Select all

convert "srttest\SRT test\001.jpg" -distort SRT "1408,1061 1.0 0 1421,1068" result1a.jpg

Re: GIMP/ImageMagick distort SRT difference

Posted: 2013-11-25T15:16:45-07:00
by tsanders
Actually, 0,0 is the center, any other value introduces an offset.
snibgo wrote:SRT with 6 arguments (X,Y Scale Angle NewX,NewY) first scales (but yours doesn't), then rotates about X,Y, then translates from X,Y to NewX,NewY.

So your rotate is about (0,0), the top-left corner. Perhaps you'd prefer to rotate about the centre, eg:

Code: Select all

convert "srttest\SRT test\001.jpg" -distort SRT "1408,1061 1.0 0 1421,1068" result1a.jpg

Re: GIMP/ImageMagick distort SRT difference

Posted: 2013-11-25T15:24:55-07:00
by fmw42
tsanders wrote:Hi fmw42,
The zelda picture is probably too small to see the effect. I am using 2826x2112px. -virtual-pixel none does not make any difference.
fmw42 wrote:I get virtually the same result using IM -distort SRT and GIMP for 20 degree rotation. There is a nominal 0.5 pixel offset unless you use image coordinates in IM. See http://www.imagemagick.org/Usage/distor ... oordinates That basically means you need to specify the center pixel for the rotation with 0.5 added as follows, using the 256x256 zelda image.

convert zelda1.jpg -virtual-pixel none -distort SRT "128.5,128.5 1 20 128.5,128.5" zelda1_srt20b.png

see http://www.imagemagick.org/Usage/distorts/#srt

This may be more an issue for very small rotatations.

I used -virtual-pixel none because my gimp result came out with transparent outside region. The important thing is the 0.5 pixel offset for getting the proper center in IM. That is why I used 128.5 and not 128. You need to use all the arguments, not just the rotation angle.

PS. check your result and script to be sure it works correctly vs GIMP rotate. Perhaps the issue is in your script or how GIMP does rotation and translation in the layers.

Re: GIMP/ImageMagick distort SRT difference

Posted: 2013-11-25T15:33:22-07:00
by snibgo
@tsanders: What version IM are you using? In v6.8.7-0 on Windows 7, your command rotates around the top-left corner (then applies a small translation), whereas mine rotates around the centre (and translates).

Your command:

Code: Select all

convert "srttest\srt test\003.jpg" -distort SRT "0,0 1.0 4.85 13,7" result3.jpg
My command:

Code: Select all

convert "srttest\srt test\003.jpg" -distort SRT "1408,1061 1.0 4.85 1421,1068" result3a.jpg
To get a simple rotate exactly at the centre with no translation, we can set X=NewX and Y=NewY:

Code: Select all

convert "srttest\srt test\003.jpg" -distort SRT "1408,1061 1.0 4.85 1408,1061" result3b.jpg

Re: GIMP/ImageMagick distort SRT difference

Posted: 2013-11-25T16:34:28-07:00
by fmw42
tsanders wrote:Actually, 0,0 is the center, any other value introduces an offset.
In IM if the first coordinate pair in -distort SRG are 0,0, then as snibgo says it will be around the top left corner. Is 0,0 in GIMP the center or top left corner?

What was your exact IM command?

In IM -distort SRT the offset is determined by the difference between the first X,Y pair and the last X,Y pair. To rotate about the center and have no offset, you can simply use -distort SRT rotationangle.

But if you want offsets, then you need to specify the x,y center of rotation in coordinates relative to the top left corner and the translated center point (not offset) after rotation relative to the top left corner, where the top left corner is 0,0.

Re: GIMP/ImageMagick distort SRT difference -- SOLVED --

Posted: 2013-11-26T01:31:58-07:00
by tsanders
Thanks snibgo/fmw42! I misunderstood the IM docs for SRT: "If no control point is given, the exact center of the input source image is used". No control point is indeed NOT similar to control point 0,0. If I use the coordinates of the center of the image for the rotation point and add that point to the translation values, everything works fine. Thanks again, great help!
Tom

Re: GIMP/ImageMagick distort SRT difference

Posted: 2013-11-26T02:39:59-07:00
by snibgo
Good stuff. I should mention that my comments above ignore the 0.5 pixel issue. You will need to figure out if Gimp and IM use the same convention.