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 »

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: 25562
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 »

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 »

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: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Center align object in a image

Post by Bonzo »

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 »

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: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Center align object in a image

Post 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%
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
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 »

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 »

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: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Center align object in a image

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