How to place image directly to the center?

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?".
imanasya
Posts: 36
Joined: 2018-10-05T11:21:07-07:00
Authentication code: 1152

How to place image directly to the center?

Post by imanasya »

This imagemagic code crops 2 pixels then crops same color borders and then scales and places images to the center:

Code: Select all

@echo off
setlocal enabledelayedexpansion
 
for /f "tokens=1,2 delims=:" %%x in ('magick 0.jpg -crop +2+2+2+2 -fuzz 2%% -trim +repage -format %%w:%%h +write info: NULL:') do set /a "w1=%%x,h1=%%y"
for /f "tokens=1,2 delims=:" %%x in ('magick 1.jpg -crop +2+2+2+2 -fuzz 2%% -trim +repage -format %%w:%%h +write info: NULL:') do set /a "w2=%%x,h2=%%y"
 
if !w1! LSS !h1! (if !w2! LSS !h2! (call :#y) else (call :#n)) else (call :#n)
exit
 
:#y
 magick 0.jpg -crop +2+2+2+2 -fuzz 2%% -trim +repage -quality 100 combined.jpg
exit /b
 
:#n
 magick 0.jpg 1.jpg -crop +2+2+2+2 -fuzz 2%% -trim +repage -set option:dim "%%[fx:max(max(u.w,v.w),max(u.h,v.h))]" -filter Catrom -resize "%%[dim]x%%[dim]" -unsharp 0x1+0.5+0.05 -background white -gravity center -append -quality 100 combined.jpg
exit /b
But I found that it places the image a few pixels to the right from the center. How can I fix this?

Images:
https://yadi.sk/i/mNdVdM63IVjQ1A
https://yadi.sk/i/NosP3XXYcvSTaQ
https://yadi.sk/i/XS-VdrOSoZeojQ
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to place image directly to the center?

Post by snibgo »

"-crop +2+2+2+2" is not valid IM syntax, although IM does accept it. It doesn't do the same as "-crop 2x2+2+2". What do you want it to do?

I suggest you use valid syntax.
snibgo's IM pages: im.snibgo.com
imanasya
Posts: 36
Joined: 2018-10-05T11:21:07-07:00
Authentication code: 1152

Re: How to place image directly to the center?

Post by imanasya »

snibgo wrote: 2018-12-13T15:17:19-07:00 "-crop +2+2+2+2" is not valid IM syntax, although IM does accept it. It doesn't do the same as "-crop 2x2+2+2". What do you want it to do? I suggest you use valid syntax.
Thank you, snibgo. But I don't know the valid syntax. I have read the manual but didn't find how to cut off 2 pixels from each side. Do you know how to do that?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to place image directly to the center?

Post by snibgo »

To shave 2 pixels from all sides, use "-shave 2x2".

That page also gives the correct syntax for "-crop".
snibgo's IM pages: im.snibgo.com
imanasya
Posts: 36
Joined: 2018-10-05T11:21:07-07:00
Authentication code: 1152

Re: How to place image directly to the center?

Post by imanasya »

snibgo wrote: 2018-12-14T02:13:01-07:00 To shave 2 pixels from all sides, use "-shave 2x2".

That page also gives the correct syntax for "-crop".
Unfortunately the top image is still not in the center.

Code: Select all

@echo off
setlocal enabledelayedexpansion
 
for /f "tokens=1,2 delims=:" %%x in ('magick 0.jpg -shave 2x2 -fuzz 2%% -trim +repage -format %%w:%%h +write info: NULL:') do set /a "w1=%%x,h1=%%y"
for /f "tokens=1,2 delims=:" %%x in ('magick 1.jpg -shave 2x2 -fuzz 2%% -trim +repage -format %%w:%%h +write info: NULL:') do set /a "w2=%%x,h2=%%y"
 
if !w1! LSS !h1! (if !w2! LSS !h2! (call :#y) else (call :#n)) else (call :#n)
exit
 
:#y
 magick 0.jpg -shave 2x2 -fuzz 2%% -trim +repage -quality 100 combined.jpg
exit /b
 
:#n
 magick 0.jpg 1.jpg -shave 2x2 -fuzz 2%% -trim +repage -set option:dim "%%[fx:max(max(u.w,v.w),max(u.h,v.h))]" -filter Catrom -resize "%%[dim]x%%[dim]" -unsharp 0x1+0.5+0.05 -background white -gravity center -append -quality 100 combined.jpg
exit /b
https://yadi.sk/i/wpSemloN6BtOQQ
imanasya
Posts: 36
Joined: 2018-10-05T11:21:07-07:00
Authentication code: 1152

Re: How to place image directly to the center?

Post by imanasya »

imanasya wrote: 2018-12-14T02:29:56-07:00
snibgo wrote: 2018-12-14T02:13:01-07:00 To shave 2 pixels from all sides, use "-shave 2x2".
Unfortunately the top image is still not in the center.

Code: Select all

@echo off
setlocal enabledelayedexpansion
 
for /f "tokens=1,2 delims=:" %%x in ('magick 0.jpg -shave 2x2 -fuzz 2%% -trim +repage -format %%w:%%h +write info: NULL:') do set /a "w1=%%x,h1=%%y"
for /f "tokens=1,2 delims=:" %%x in ('magick 1.jpg -shave 2x2 -fuzz 2%% -trim +repage -format %%w:%%h +write info: NULL:') do set /a "w2=%%x,h2=%%y"
 
if !w1! LSS !h1! (if !w2! LSS !h2! (call :#y) else (call :#n)) else (call :#n)
exit
 
:#y
 magick 0.jpg -shave 2x2 -fuzz 2%% -trim +repage -quality 100 combined.jpg
exit /b
 
:#n
 magick 0.jpg 1.jpg -shave 2x2 -fuzz 2%% -trim +repage -set option:dim "%%[fx:max(max(u.w,v.w),max(u.h,v.h))]" -filter Catrom -resize "%%[dim]x%%[dim]" -unsharp 0x1+0.5+0.05 -background white -gravity center -append -quality 100 combined.jpg
exit /b
https://yadi.sk/i/wpSemloN6BtOQQ
imanasya
Posts: 36
Joined: 2018-10-05T11:21:07-07:00
Authentication code: 1152

Re: How to place image directly to the center?

Post by imanasya »

imanasya wrote: 2018-12-14T02:29:56-07:00
snibgo wrote: 2018-12-14T02:13:01-07:00 To shave 2 pixels from all sides, use "-shave 2x2".
Unfortunately the top image is still not in the center.

Code: Select all

@echo off
setlocal enabledelayedexpansion
 
for /f "tokens=1,2 delims=:" %%x in ('magick 0.jpg -shave 2x2 -fuzz 2%% -trim +repage -format %%w:%%h +write info: NULL:') do set /a "w1=%%x,h1=%%y"
for /f "tokens=1,2 delims=:" %%x in ('magick 1.jpg -shave 2x2 -fuzz 2%% -trim +repage -format %%w:%%h +write info: NULL:') do set /a "w2=%%x,h2=%%y"
 
if !w1! LSS !h1! (if !w2! LSS !h2! (call :#y) else (call :#n)) else (call :#n)
exit
 
:#y
 magick 0.jpg -shave 2x2 -fuzz 2%% -trim +repage -quality 100 combined.jpg
exit /b
 
:#n
 magick 0.jpg 1.jpg -shave 2x2 -fuzz 2%% -trim +repage -set option:dim "%%[fx:max(max(u.w,v.w),max(u.h,v.h))]" -filter Catrom -resize "%%[dim]x%%[dim]" -unsharp 0x1+0.5+0.05 -background white -gravity center -append -quality 100 combined.jpg
exit /b
https://yadi.sk/i/wpSemloN6BtOQQ
imanasya
Posts: 36
Joined: 2018-10-05T11:21:07-07:00
Authentication code: 1152

Re: How to place image directly to the center?

Post by imanasya »

Oops. I can't delete my messages.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to place image directly to the center?

Post by snibgo »

I can't download your images, so I can't diagnose your problem.
snibgo's IM pages: im.snibgo.com
imanasya
Posts: 36
Joined: 2018-10-05T11:21:07-07:00
Authentication code: 1152

Re: How to place image directly to the center?

Post by imanasya »

Image 0: Image
Image 1: Image
Combined Image: Image
imanasya
Posts: 36
Joined: 2018-10-05T11:21:07-07:00
Authentication code: 1152

Re: How to place image directly to the center?

Post by imanasya »

snibgo wrote: 2018-12-14T03:31:33-07:00 I can't download your images, so I can't diagnose your problem.
Red image is slightly moved to the right.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to place image directly to the center?

Post by snibgo »

It seems to work fine, but I don't know what you intend. Set a different background colour to understand what is happening. Perhaps you intend the two images to be the same size. If so, you need "!" in the resize. See http://www.imagemagick.org/script/comma ... p#geometry
snibgo's IM pages: im.snibgo.com
imanasya
Posts: 36
Joined: 2018-10-05T11:21:07-07:00
Authentication code: 1152

Re: How to place image directly to the center?

Post by imanasya »

snibgo wrote: 2018-12-14T04:16:52-07:00 It seems to work fine, but I don't know what you intend.
It's clearly visible here. Take a look at upper red image. It is not in the center, it moved a few pixels to the right from the center. Left white column is is a few pixels wider than right white column. I'd like to get both left and right white columns the same sizes:
Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to place image directly to the center?

Post by snibgo »

imanasya wrote:[The upper red image...] Left white column is is a few pixels wider than right white column.
Yes, because the left side starts off with a few columns of roughly white pixels. If you intend to trim them off, use a larger fuzz.

Incidentally, where did these images come from? JPG is a very bad format for graphics like this.
snibgo's IM pages: im.snibgo.com
imanasya
Posts: 36
Joined: 2018-10-05T11:21:07-07:00
Authentication code: 1152

Re: How to place image directly to the center?

Post by imanasya »

snibgo wrote: 2018-12-14T04:55:21-07:00 If you intend to trim them off, use a larger fuzz.
Why?
2% * 256 = 5
It means that trim should cut off pixels in the range 250-255. That is left column 5 pixels wide. Check the image 0 in this topic above, you won't find pixels darker than 250 in the left white column (5 pixels).

These images were made for test purposes only. This script is not for graphics, it's for photos.
Post Reply