Remove or avoid offset after some conversions

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Picklepaws
Posts: 10
Joined: 2014-07-04T11:42:38-07:00
Authentication code: 6789
Location: Canada

Remove or avoid offset after some conversions

Post by Picklepaws »

I'm doing some processing for an image and it's entirely necessary that I preserve the original center of the image.

I start with a 580x480 source image. There are 100 pixels on the right that I need to get rid of, and also, I only need a 478x478 circle of the image. So, I created a 480x480 mask file that has a 478x478 circle. I crop off the 100 pixels that I don't want, and I composite it with the mask to only have the 478x478 pixels of the circle that I need for production.

My issue is that I'm getting a 1,1 offset. I can tell that the center is off, because there's a cross in the center of the image. When I overlay a "test" layer that should give me the true center, the crosses don't quite line up. I know that the test layer is correct.

These are my commands

Masking and removing the rightmost 100 pixels with the composite and trim.
Note that this takes a 580x480 pixel image and masks it with a 480x480 image and makes it a 478x478 circular image

Code: Select all

	convert	source.png	mask.png
	-alpha 	Off
	-compose	CopyOpacity
	-composite
	-trim
	+repage
	output1.png
Removing the colours that I don't want from the image
478x478 is still 478x478

Code: Select all

	-convert	output1.png
	-channel	rgba
	-alpha	set
	-fill		none
	-opaque	"#999960"
	-opaque	"#333360"
	-opaque	"#000000"
	output2.png
Overlaying the test layer
478x478 is still 478x478

Code: Select all

	-convert	output2.png	test.png
	-composite
	final.png
So my guess is that something goes wrong in the first part of the code, but I don't know how to resolve it. When I try to open any of the images in GIMP, I am notified that there is an offset of 1,1.
Any suggestions for different or additional commands to resolve this?
Thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove or avoid offset after some conversions

Post by fmw42 »

try adding +repage after reading your input images (they may already have a virtual canvas).

convert source.png mask.png +repage .....

Also an even dimensioned circle may not be centered. That is most likely the source of the offset by 1 pixel. What commands did you use to create the circle mask?
Picklepaws
Posts: 10
Joined: 2014-07-04T11:42:38-07:00
Authentication code: 6789
Location: Canada

Re: Remove or avoid offset after some conversions

Post by Picklepaws »

I didn't use any commands to create the circle mask, I did it in gimp. That test layer that I'm using in the last command is composed of a bunch of circles, and so I just filled white in to the outermost circle and filled the exterior with black.

Image

So basically I just used the paint bucket tool to fill in the center up to the last white circle.

I will try the repage first
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove or avoid offset after some conversions

Post by fmw42 »

Your cross marker is at 240x240, but the actual image center is at 240.5,240.5 (on half pixel off for even dimensions), which probably gets rounded to 241,241 when being used or measured
Picklepaws
Posts: 10
Joined: 2014-07-04T11:42:38-07:00
Authentication code: 6789
Location: Canada

Re: Remove or avoid offset after some conversions

Post by Picklepaws »

If you notice on that image that I attached in my previous message, on the right and bottom edges the white pixels cut out. If I create the mask by going to the outermost circle (the red one) then I don't get my offset issue. But if I fill in those edges with white pixels, then it creates the offset.

EDIT: I need those "missing" pixels as the white part of the mask otherwise I get red lines in my final image. However, I can't have the offset. :(
Last edited by Picklepaws on 2015-06-29T12:58:37-07:00, edited 1 time in total.
Picklepaws
Posts: 10
Joined: 2014-07-04T11:42:38-07:00
Authentication code: 6789
Location: Canada

Re: Remove or avoid offset after some conversions

Post by Picklepaws »

Is there any way for me to get around the rounding?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove or avoid offset after some conversions

Post by snibgo »

I suggest you test without a trim. Then find out exactly how many pixels need offsetting to get the correct result. If this is an integer number, then "-geometry" will do the job. If it is fractional, you can use "-distort SRT".
snibgo's IM pages: im.snibgo.com
Picklepaws
Posts: 10
Joined: 2014-07-04T11:42:38-07:00
Authentication code: 6789
Location: Canada

Re: Remove or avoid offset after some conversions

Post by Picklepaws »

Thank you all very much for your help!

I decided to add another mask, that only includes the 480x480 square that I want. For the remaining 102 pixels, I used a crop argument. This allowed me to remove my trim argument.

Final code:

Code: Select all

	convert	source.png	squaremask.png
	-alpha	Off
	-compose	CopyOpacity
	-composite
	output1.png

Code: Select all

	convert	output1.png
	-crop		478x478+0+0
	output2.png

Code: Select all

	convert	output2.png	circlemask.png
	-alpha	Off
	-compose	CopyOpacity
	-composite
	output3.png

Code: Select all

   -convert   output3.png
   -channel   rgba
   -alpha   set
   -fill      none
   -opaque   "#999960"
   -opaque   "#333360"
   -opaque   "#000000"
   final.png

Code: Select all

   -convert   final.png   testlayer.png
   -composite
   test.png
Again thank you all very much for your help. Talking about it and seeing what others have for solutions is a big help! Have a great day.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove or avoid offset after some conversions

Post by fmw42 »

My mistake above. The center is at 239.5,239.5 and is probably truncated to 239,239. (Pixel numbers start at 0).
Post Reply