Creating a Square Product Photo
-
- Posts: 4
- Joined: 2019-03-31T04:52:05-07:00
- Authentication code: 1152
Creating a Square Product Photo
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):
I need (square):
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):
I need (square):
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Creating a Square Product Photo
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?
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.
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
With IM v7, the job can probably be done in a single command.
snibgo's IM pages: im.snibgo.com
-
- Posts: 4
- Joined: 2019-03-31T04:52:05-07:00
- Authentication code: 1152
Re: Creating a Square Product Photo
Hello platform is Windows 10, version ImageMagick 7.0.8-35 Q16 x64 2019-03-25snibgo 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?
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.Code: Select all
magick dress.jpg -fuzz 10% -format %@ info: 1574x1648+864+190
With IM v7, the job can probably be done in a single command.
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!
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Creating a Square Product Photo
Try this
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.
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
The above will pad to square using the color found in the top left corner of the cropped image.
-
- Posts: 4
- Joined: 2019-03-31T04:52:05-07:00
- Authentication code: 1152
Re: Creating a Square Product Photo
Thank you very much for your help!fmw42 wrote: ↑2019-03-31T10:54:41-07:00 Try this
If in a .bat file, then I believe % need to be doubled to %%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
The above will pad to square using the color found in the top left corner of the cropped image.
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Creating a Square Product Photo
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
-
- Posts: 4
- Joined: 2019-03-31T04:52:05-07:00
- Authentication code: 1152
Re: Creating a Square Product Photo
Thank you very much for your effort, I appreciate it very much.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
This is the output (that's not exactly right):
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!
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Creating a Square Product Photo
What is that you actually want. Show me an example from the above images and post the input images as well.
try
If you want the image distorted to fit a square, then try (note the !)
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.
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
Code: Select all
magick.exe original.jpg -fuzz 15% -trim +repage -set option:maxwh "%[fx:max(w,h)]" -resize "%[maxwh]x%[maxwh]!" result.jpg
So please show the above input images.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Creating a Square Product Photo
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
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Creating a Square Product Photo
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.