Page 1 of 1

Batch resizing product images

Posted: 2016-10-24T19:58:06-07:00
by ebr4him
I have a couple thousand product images that I need to convert for ecommerce website:

1. Grab the border -fuzz 5% (and save as variable?)
2. Trim -trim (trim before resize to preserve detail?)
3. Resize -resize 980x980
4. Pad with same color border -bordercolor white -border 44x44
5. Center the image -gravity center
6. Limit the image dimensions -extent 980x980
7. Transparent BG to White -background white -alpha remove -flatten
8. Convert all images to JPG image_converted.jpg

Putting it all together:

Code: Select all

    convert image.png \
      -fuzz 5% \
      -trim \
      -resize 980x980 \
      -bordercolor white \
      -border 44x44 \
      -gravity center \
      -extent 980x980 \
      -background white \
      -alpha remove \
      -flatten \
      image_converted.jpg
Although I have the bits and pieces but the overall result is not as desired, because:

- Since I'm not sure how to save the fuzz value, the border color is currently white.
- Converted image is not exactly 1024x1024 (980+44).
- Smaller images are enlarged which is not ideal. After trim, if the image is smaller than 980x980, it should just be centered.

Thanks!

Re: Batch resizing product images

Posted: 2016-10-24T20:37:07-07:00
by snibgo
ebr4him wrote:- Converted image is not exactly 1024x1024 (980+44).
"-extent" gives you the size you specify, in this case 980x980. If you want 1024x1024, give those numbers.

You don't need both "-alpha remove" and "-flatten". For what you want, ether of these does the job.

For "-resize if" options, see http://www.imagemagick.org/script/comma ... p#geometry

Re: Batch resizing product images

Posted: 2016-10-24T21:08:37-07:00
by ebr4him
Tried this, but still no luck. Just can't figure the resize command

Code: Select all

  -fuzz 5% \
  -trim \
  -unsharp 2x0.5+0.7+0 \
  -resize 980x980\> \
  -bordercolor white \
  -border 44x44 \
  -gravity center \
  -extent 1024x1024 \
  -background white \
  -alpha remove \
  -dither none \
  -interlace none \
  -strip \
  -quality 82 \

Re: Batch resizing product images

Posted: 2016-10-24T21:44:23-07:00
by snibgo
I suggest you add lines like "+write x0.png" between each operation. Then you can trace whether the operations are doing what you expect.

Re: Batch resizing product images

Posted: 2016-10-24T21:47:58-07:00
by GeeMack
ebr4him wrote:Tried this, but still no luck. Just can't figure the resize command
Your "-resize 980x980\>" operation should take any image that extends beyond a 980x980 boundary and make it as large as possible while still fitting entirely inside that box. Any images that already fit inside those dimensions shouldn't be resized at all.

You can test your command by putting "-write testimage.png" immediately after your "-resize" operation. That will create an image you can look at to see how your command is working up to that point. Try your command with some input images less than 980x980 and some larger than 980x980. If it's all good to that point then your sizing issues may be with your "-border ..." or "-extent ..." operations.

Also, it can be helpful to follow a "-trim" operation with a "+repage" to clear any page offset which might affect the behavior of operations further along the command.

Re: Batch resizing product images

Posted: 2016-10-24T21:53:03-07:00
by ebr4him
@GeeMack Great, that helped the resize issue. It was the lack of `+repage` causing issues.

Now how do I set the fuzz as variable so I can use it for bordercolor?

Thanks!

Re: Batch resizing product images

Posted: 2016-10-24T22:10:29-07:00
by ebr4him
So basically I want:

Code: Select all

convert image.png \
-alpha remove \
  -flatten \
  -trim \
  +repage \
  -fuzz 3% \ = $value
  -trim \
  +repage \
  -background $value \
  -unsharp 2x0.5+0.7+0 \
  -resize 980x980\> \
  -bordercolor $value \
  -border 44x44 \
  -gravity center \
  -extent 1024x1024 \
  -dither none \
  -interlace none \
  -strip \
  -quality 82 \
  image.png_converted.jpg

Re: Batch resizing product images

Posted: 2016-10-24T22:33:39-07:00
by fmw42
You should do the border calculation in one step, save the result as a variable and then use that variable in a second step to do all the rest.

I am not sure I understand exactly what you want, but try this. At least it will give you an idea how to get the fuzz color.

The parenthesis processing in the first command, makes a b/w mask where white is the border after the fuzz and black is the center. This is put into the alpha channel and the image is scaled to 1 px. Transparent pixels are ignored in the scale. Then alpha is turned off to get the transparent border color.

Code: Select all

color=$(convert \
	zelda3b20g.png \
	\( \
	+clone \
	-fuzz 5% \
	-fill white \
	-draw "color 0,0 floodfill" \
	-alpha off \
	-fill black +opaque white \
	\) \
	-alpha off \
	-compose copy_opacity \
	-composite \
	-scale 1x1 \
	-alpha off \
	-format "%[pixel:u.p{0,0}]" \
	info:)

echo "color=$color"
gray(127,127,127)

convert zelda3b20g.png \
	-fuzz 5% \
	-trim \
	+repage \
	-unsharp 2x0.5+0.7+0 \
	-resize 980x980\> \
	-bordercolor $color \
	-border 44x44 \
	-gravity center \
	-background $color \
	-extent 1024x1024 \
	-alpha remove \
	-dither none \
	-interlace none \
	-strip \
	-quality 82 \
	zelda3b20g_border.jpg
Input:
Image

Output:
Image

Re: Batch resizing product images

Posted: 2016-10-24T22:42:54-07:00
by ebr4him

Code: Select all

Version: ImageMagick 6.9.6-2 Q16 x86_64 2016-10-11 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules 
Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib
This image for example (has transparent edges, solid color background):

Image

After converting with:

Code: Select all

convert image.png \
  -alpha remove \
  -flatten \
  -trim \
  +repage \
  -fuzz 3% \
  -trim \
  +repage \
  -background white \
  -unsharp 2x0.5+0.7+0 \
  -resize 980x980\> \
  -bordercolor white \
  -border 44x44 \
  -gravity center \
  -extent 1024x1024 \
  -dither none \
  -interlace none \
  -strip \
  -quality 82 \
  image_converted.jpg
The result is:
Image

However, the desired result is:
Image

Thanks!

Re: Batch resizing product images

Posted: 2016-10-24T22:55:34-07:00
by fmw42
If you have a tranparent border outside the color you want, then you need to remove the transparency with another trim. So try this:

Code: Select all

color=$(convert \
h1fmkvO.png \
-fuzz 5% \
-trim \
+repage \
\( \
+clone \
-fuzz 5% \
-fill white \
-draw "color 0,0 floodfill" \
-alpha off \
-fill black +opaque white \
\) \
-alpha off \
-compose copy_opacity \
-composite \
-scale 1x1 \
-alpha off \
-format "%[pixel:u.p{0,0}]" \
info:)

echo "color=$color"
#color=srgb(132,66,254)

convert h1fmkvO.png \
-fuzz 5% \
-trim \
+repage \
-unsharp 2x0.5+0.7+0 \
-resize "980x980>" \
+write tmp.png \
-bordercolor $color \
-border 44x44 \
+write tmp1.png \
-gravity center \
-background $color \
-extent 1024x1024 \
+write tmp2.png \
-alpha remove \
-dither none \
-interlace none \
-strip \
-quality 82 \
h1fmkvO_border.jpg

Re: Batch resizing product images

Posted: 2016-10-26T18:33:25-07:00
by ebr4him
Thanks Fred, I tinkered a lot but couldn't find the right combination so ended up just trimming them for now. Thanks anyway!