Page 1 of 1

Center align object in a image

Posted: 2016-03-31T23:17:44-07:00
by joshuafinny
Platform: Windows
Imagemagick version: 6.9.3
Sample Image: http://imgur.com/81v7P6p

I have as set of images which have a object in it and the images are of different dimensions/resolutions. I want o batch process these to center align the objects. How do I do this?

Re: Center align object in a image

Posted: 2016-03-31T23:25:14-07:00
by fmw42
The image is centered horizontally (from appearance). the only issue is that there is no white at the bottom

By center do you mean equal white all around. If so, then trim and pad. So try

Code: Select all

convert 81v7P6p.jpg -fuzz 10% -trim +repage -bordercolor white -border 20 result.jpg

Re: Center align object in a image

Posted: 2016-03-31T23:48:27-07:00
by joshuafinny
fmw42 wrote:The image is centered horizontally (from appearance). the only issue is that there is no white at the bottom

By center do you mean equal white all around. If so, then trim and pad. So try

Code: Select all

convert 81v7P6p.jpg -fuzz 10% -trim +repage -bordercolor white -border 20 result.jpg
I shared the wrong sample. Here's the correct one http://imgur.com/Kw6mXbh

Re: Center align object in a image

Posted: 2016-04-01T02:36:30-07:00
by Bonzo
Did you try fmw42's example code? If so what was wrong with the result?

Re: Center align object in a image

Posted: 2016-04-01T03:47:30-07:00
by joshuafinny
Bonzo wrote:Did you try fmw42's example code? If so what was wrong with the result?
That code close crops the images and fills the border with white colour. What I want is to center align the object in the same canvas. The hex code will be the one shown in the second sample.

Re: Center align object in a image

Posted: 2016-04-01T04:07:32-07:00
by snibgo
Perhaps you want to effectively trim to the shoe but then back off the trim by a certain amount, say 10 pixels. Windows BAT script:

Code: Select all

rem Trim, but back off the trim.

set SRC=shoe.jpg
set OUT=s.jpg

set delta=10

for /F "usebackq tokens=1-4 delims=x+" %%A in (`%IM%convert ^
  %SRC% -fuzz 10%% -format %%@ info:`) do (
  set /A W=%%A+2*delta
  set /A H=%%B+2*delta
  set /A X=%%C-delta
  set /A Y=%%D-delta
)

echo %W%x%H%+%X%+%Y%

%IM%convert %SRC% -crop %W%x%H%+%X%+%Y% +repage %OUT%

Re: Center align object in a image

Posted: 2016-04-01T10:44:13-07:00
by fmw42
try this:

Unix syntax:

Code: Select all

convert Kw6mXbh.jpg -fuzz 1% -trim +repage \
-virtual-pixel edge -set option:distort:viewport \
"%[fx:max(w,h)+10]x%[fx:max(w,h)+10]+%[fx:(w-max(w,h)-10)/2]+%[fx:(h-max(w,h)-10)/2]" \
-filter point +distort SRT 0 Kw6mXbh_center.jpg
I think Windows syntax:

Code: Select all

convert Kw6mXbh.jpg -fuzz 1% -trim +repage ^
-virtual-pixel edge -set option:distort:viewport ^
"%[fx:max(w,h)+10]x%[fx:max(w,h)+10]+%[fx:(w-max(w,h)-10)/2]+%[fx:(h-max(w,h)-10)/2]" ^
-filter point +distort SRT 0 Kw6mXbh_center.jpg

Re: Center align object in a image

Posted: 2017-12-21T10:54:20-07:00
by cajun20/20
snibgo wrote: 2016-04-01T04:07:32-07:00 Perhaps you want to effectively trim to the shoe but then back off the trim by a certain amount, say 10 pixels. Windows BAT script:

Code: Select all

rem Trim, but back off the trim.

set SRC=shoe.jpg
set OUT=s.jpg

set delta=10

for /F "usebackq tokens=1-4 delims=x+" %%A in (`%IM%convert ^
  %SRC% -fuzz 10%% -format %%@ info:`) do (
  set /A W=%%A+2*delta
  set /A H=%%B+2*delta
  set /A X=%%C-delta
  set /A Y=%%D-delta
)

echo %W%x%H%+%X%+%Y%

%IM%convert %SRC% -crop %W%x%H%+%X%+%Y% +repage %OUT%
Trying to use this code, but getting a bug on this line: `for /F "usebackq tokens=1-4 delims=x+" %%A in (`%IM%convert ^`

Not sure why the /F is there or if that is causing the issue. The bug says there is a syntax error here. Please let me know what additional information I should include.

Thanks

Re: Center align object in a image

Posted: 2017-12-21T19:52:16-07:00
by snibgo
What version of IM are you running? If v7, you should change "convert" to "magick". On what platform? (I assume Windows, but perhaps not.)
cajun20/20 wrote:The bug says there is a syntax error here.
Please paste the exact error message.

I use %IM% to point to the directory that contains convert.exe. If you don't, then you can remove %IM%.

It might help if you paste the complete contents of your BAT file.