Page 1 of 1

Creating a Square Product Photo

Posted: 2019-03-31T05:06:48-07:00
by hlavac_michal
Please, give me an advice.
I need to create a square product photo automatically from the photos.
I manage to automatically crop the gray background to the product, but I can't make a square output.
I attach the original and the output I need.
Does anyone know how to do this?
Thanks!

Original (2992x2000 px):
Image


I need (square):
Image

Re: Creating a Square Product Photo

Posted: 2019-03-31T07:55:04-07:00
by snibgo
What version of IM? On what platform?

You want to find the width, height and offsets of the trim, and increase either the height or width so they are equal, with a corresponding reduction in one offset, so you can do the crop. Is that correct?

Code: Select all

magick dress.jpg -fuzz 10% -format %@ info:
1574x1648+864+190
This is the width, height and offsets of what a trim would give. You need to extract those numbers and do some arithmetic. The code for this depends on your script language.

With IM v7, the job can probably be done in a single command.

Re: Creating a Square Product Photo

Posted: 2019-03-31T09:22:24-07:00
by hlavac_michal
snibgo wrote: 2019-03-31T07:55:04-07:00 What version of IM? On what platform?

You want to find the width, height and offsets of the trim, and increase either the height or width so they are equal, with a corresponding reduction in one offset, so you can do the crop. Is that correct?

Code: Select all

magick dress.jpg -fuzz 10% -format %@ info:
1574x1648+864+190
This is the width, height and offsets of what a trim would give. You need to extract those numbers and do some arithmetic. The code for this depends on your script language.

With IM v7, the job can probably be done in a single command.
Hello platform is Windows 10, version ImageMagick 7.0.8-35 Q16 x64 2019-03-25

The product always has a different dimension. I need to find his longer side and make a square cutout.

I found "convert" * .jpg "-bordercolor # e4e6e3 -border 10x10 -fuzz 25% -trim -set filename: f% t trimmed /% [filename: f] .jpg"
This works well to accurately cut the product. If it were possible to set the cutout sides to be the same, it would be great.

Thank you very much for your help!

Re: Creating a Square Product Photo

Posted: 2019-03-31T10:54:41-07:00
by fmw42
Try this

Code: Select all

magick.exe original.jpg -fuzz 15% -trim +repage -set option:bgcolor "%[pixel:u.p{0,0}]" -set option:maxwh "%[fx:max(w,h)]" -background "%[bgcolor]" -gravity center -extent "%[maxwh]x%[maxwh]" result.jpg
If in a .bat file, then I believe % need to be doubled to %%

The above will pad to square using the color found in the top left corner of the cropped image.

Re: Creating a Square Product Photo

Posted: 2019-03-31T12:59:59-07:00
by hlavac_michal
fmw42 wrote: 2019-03-31T10:54:41-07:00 Try this

Code: Select all

magick.exe original.jpg -fuzz 15% -trim +repage -set option:bgcolor "%[pixel:u.p{0,0}]" -set option:maxwh "%[fx:max(w,h)]" -background "%[bgcolor]" -gravity center -extent "%[maxwh]x%[maxwh]" result.jpg
If in a .bat file, then I believe % need to be doubled to %%

The above will pad to square using the color found in the top left corner of the cropped image.
Thank you very much for your help!

Thank you for the advice. But I don't need this. The background is not exactly the same everywhere, so when it loads the color of one pixel and in that color fills the edges into a square, it is visible.

I really need to get a square cutout from the original.

For every advice I will be grateful.

Re: Creating a Square Product Photo

Posted: 2019-03-31T13:04:21-07:00
by fmw42
Try this (change max to min)

Code: Select all

magick.exe original.jpg -fuzz 15% -trim +repage -set option:minwh "%[fx:min(w,h)]" -gravity center -extent "%[minwh]x%[minwh]" result.jpg

Re: Creating a Square Product Photo

Posted: 2019-03-31T13:42:32-07:00
by hlavac_michal
fmw42 wrote: 2019-03-31T13:04:21-07:00 Try this (change max to min)

Code: Select all

magick.exe original.jpg -fuzz 15% -trim +repage -set option:minwh "%[fx:min(w,h)]" -gravity center -extent "%[minwh]x%[minwh]" result.jpg
Thank you very much for your effort, I appreciate it very much.
This is the output (that's not exactly right):
Image
Image
Image
Image
Image

It is necessary to choose a longer side, increase by about 50 px and then use for horizontal and vertical output image.

Any idea yet?

Thank you very much!

Re: Creating a Square Product Photo

Posted: 2019-03-31T14:16:20-07:00
by fmw42
What is that you actually want. Show me an example from the above images and post the input images as well.

try

Code: Select all

magick.exe original.jpg -fuzz 15% -trim +repage -set option:minwh "%[fx:min(w,h)]" -gravity center -crop "%[minwh]x%[minwh]+0+0" +repage result.jpg
If you want the image distorted to fit a square, then try (note the !)

Code: Select all

magick.exe original.jpg -fuzz 15% -trim +repage -set option:maxwh "%[fx:max(w,h)]" -resize "%[maxwh]x%[maxwh]!" result.jpg
If your input images have more than one color on the borders ( one inside the other ) then you will need to trim multiple times.

So please show the above input images.

Re: Creating a Square Product Photo

Posted: 2019-03-31T17:35:55-07:00
by snibgo
Based on my first reply, I think this is what is needed. Windows BAT script:

Code: Select all

set SRC=dress.jpg

for /F "usebackq tokens=1-4 delims=x+" %%A in (`%IMG7%magick ^
  %SRC% ^
  -fuzz 10%% ^
  -format %%@ info:`) do (
  set W1=%%A
  set H1=%%B
  set X1=%%C
  set Y1=%%D
)

echo %W1% %H1% %X1% %Y1%

if %W1% GTR %H1% (
  set /A Y1=Y1-^(W1-H1^)/2
  set H1=%W1%
) else (
  set /A X1=X1-^(H1-W1^)/2
  set W1=%H1%
)

echo %W1% %H1% %X1% %Y1%

if %X1% LSS 0 (
  echo Can't make square: X1 is %X1%
  exit /B 1
)

if %Y1% LSS 0 (
  echo Can't make square: Y1 is %Y1%
  exit /B 1
)

%IMG7%magick ^
  %SRC% ^
  -crop %W1%x%H1%+%X1%+%Y1% +repage ^
  out.png

Re: Creating a Square Product Photo

Posted: 2019-03-31T18:55:37-07:00
by fmw42
From the images you show, it looks to me like your -trim is not effective, possibly due to noise or multiple surrounding layers of colors. Check the results from simply -trim and see if that is the case.