Page 1 of 2

How to place image directly to the center?

Posted: 2018-12-13T13:22:24-07:00
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

Re: How to place image directly to the center?

Posted: 2018-12-13T15:17:19-07:00
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.

Re: How to place image directly to the center?

Posted: 2018-12-14T01:12:37-07:00
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?

Re: How to place image directly to the center?

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

That page also gives the correct syntax for "-crop".

Re: How to place image directly to the center?

Posted: 2018-12-14T02:29:56-07:00
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

Re: How to place image directly to the center?

Posted: 2018-12-14T02:33:24-07:00
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

Re: How to place image directly to the center?

Posted: 2018-12-14T02:35:15-07:00
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

Re: How to place image directly to the center?

Posted: 2018-12-14T02:38:55-07:00
by imanasya
Oops. I can't delete my messages.

Re: How to place image directly to the center?

Posted: 2018-12-14T03:31:33-07:00
by snibgo
I can't download your images, so I can't diagnose your problem.

Re: How to place image directly to the center?

Posted: 2018-12-14T03:41:39-07:00
by imanasya
Image 0: Image
Image 1: Image
Combined Image: Image

Re: How to place image directly to the center?

Posted: 2018-12-14T03:43:12-07:00
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.

Re: How to place image directly to the center?

Posted: 2018-12-14T04:16:52-07:00
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

Re: How to place image directly to the center?

Posted: 2018-12-14T04:30:41-07:00
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

Re: How to place image directly to the center?

Posted: 2018-12-14T04:55:21-07:00
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.

Re: How to place image directly to the center?

Posted: 2018-12-14T06:21:46-07:00
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.