Center align object in a image

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
joshuafinny
Posts: 16
Joined: 2016-01-11T05:12:11-07:00
Authentication code: 1151

Center align object in a image

Post by joshuafinny » 2016-03-31T23:17:44-07:00

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?

User avatar
fmw42
Posts: 23658
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Center align object in a image

Post by fmw42 » 2016-03-31T23:25:14-07:00

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

joshuafinny
Posts: 16
Joined: 2016-01-11T05:12:11-07:00
Authentication code: 1151

Re: Center align object in a image

Post by joshuafinny » 2016-03-31T23:48:27-07:00

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

Bonzo
Posts: 2962
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Center align object in a image

Post by Bonzo » 2016-04-01T02:36:30-07:00

Did you try fmw42's example code? If so what was wrong with the result?

joshuafinny
Posts: 16
Joined: 2016-01-11T05:12:11-07:00
Authentication code: 1151

Re: Center align object in a image

Post by joshuafinny » 2016-04-01T03:47:30-07:00

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.

snibgo
Posts: 10554
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Center align object in a image

Post by snibgo » 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%
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 23658
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Center align object in a image

Post by fmw42 » 2016-04-01T10:44:13-07:00

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

cajun20/20
Posts: 1
Joined: 2017-12-21T10:52:10-07:00
Authentication code: 1152

Re: Center align object in a image

Post by cajun20/20 » 2017-12-21T10:54:20-07:00

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

snibgo
Posts: 10554
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Center align object in a image

Post by snibgo » 2017-12-21T19:52:16-07:00

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.
snibgo's IM pages: im.snibgo.com

Post Reply