batch resize + crop product images to ratio for ecommerce

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?".
Post Reply
johnny538
Posts: 5
Joined: 2016-11-28T15:24:43-07:00
Authentication code: 1151

batch resize + crop product images to ratio for ecommerce

Post by johnny538 »

I have taken 2900 pictures of shoes and bags for my ecommerce site (not live yet). I'm new at this and used my phone (Nexus 5x) and a lightbox with proper lighting. The quality of the images are fine. But I haven't been able to always keep the tripod, not the object in exactly the same place each time. So sometimes the object is a little but further in the distance or closer to the camera. Sometimes it's more to the left or right, or the tripod was facing a bit more down or up.

Usually it's hardly noticeable but I'm mentioning it in case it is of importance.

The container of the image on the site is 247x300 (catalog) and 510x600 (product page) (defined by theme). I made sure woocommerce uses double those values for people who have retina screens. This doesn't affect the image in any way. The image I will upload must be at least the twice the largest size, so 1020x1200

Now comes the problem. I have a lot of white space around the objects in the images. Those need to go away. I want the object (shoe, boot, bag, wallet, etc) to be centered in the canvas with no white space on the left and right side. The white space on the top and bottom can be variable depending on the height of the object.

Example: http://imgur.com/a/r9L8j

Here you can see that regardless the height and width of the object, all images fit in the canvas. The dimension of the image are the same for all.

Original dimensions: 4032x3024
Required dimension: 1020*1200 with object resized and centered vertically and no whitespace on left/right sides.

If anyone can help me out I'd greatly appreciate it. I've been smashing my head against the wall for a month now and it's the only thing keeping me from finishing this project.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: batch resize + crop product images to ratio for ecommerce

Post by fmw42 »

What is your IM version and platform? Always provide that, since syntax may differ.

Your image examples are only 762x1100. Once the white is trimmed to the bound box, what size do you want the final images? Just that size or scaled? If scale, to what size?

try

Code: Select all

convert inputimage -trim +repage resultimage
If you want it then padded to some size, use

Code: Select all

convert inputimage -trim +repage -background color white -gravity center -extent WxH resultimage
Where WxH is your final padded image size.

If you want it scale to fill the space, then us

Code: Select all

convert inputimage -trim +repage -resize WxH resultimage
That will preserve aspect and so may not be your exact size. So you can do (unix syntax)

Code: Select all

convert inputimage -trim +repage -resize WxH \
-background color white -gravity center -extent WxH resultimageresultimage
If on windows, replace \ with ^
johnny538
Posts: 5
Joined: 2016-11-28T15:24:43-07:00
Authentication code: 1151

Re: batch resize + crop product images to ratio for ecommerce

Post by johnny538 »

Thanks for the response. I'm gonna test it when I get home. I'm running version 6.8.9-9 q16 on Debian 8. The link just contained examples I saw at another site.
johnny538
Posts: 5
Joined: 2016-11-28T15:24:43-07:00
Authentication code: 1151

Re: batch resize + crop product images to ratio for ecommerce

Post by johnny538 »

When I trim the images, I end up with different sizes cause the camera distance slightly varies. And some images are from the backside of a shoe for example.

Basically I need the trimmed images to fit as best it can on a canvas of 1020x1200. With as little padding as possible on the sides depending on the type of shoe. A regular shoe shouldn't have padding on the left and right sides, but there should be some on the top and bottom like you see in the example. But a long boot should be the other way around cause its a tall object.

Sorry if I'm not clear. It's tough to explain for me.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: batch resize + crop product images to ratio for ecommerce

Post by fmw42 »

You are going to have to create an example of what you want to happen with those shoe images you posted by processing them the way you want in some GUI based tool such as GIMP or Photoshop, so we can understand what it is you want.

If the shoe is small, it can be cropped and padded, but will end up looking the same if you do not resize it. Did my last set of code not work by resizing as well as padding? If not, what was wrong with it.
johnny538
Posts: 5
Joined: 2016-11-28T15:24:43-07:00
Authentication code: 1151

Re: batch resize + crop product images to ratio for ecommerce

Post by johnny538 »

oh snap, I think that last command did the trick ! Going to test some more images to see if the result is consistent !
johnny538
Posts: 5
Joined: 2016-11-28T15:24:43-07:00
Authentication code: 1151

Re: batch resize + crop product images to ratio for ecommerce

Post by johnny538 »

It seems some images don't process correct. The canvas is the right size but the object is small and oriented in a certain spot like upper right side. I think it's because the trim command meets a 'lost' pixel somewhere and cuts off effectively leaving 3 sides untrimmed. Even with -fuzz 30% it does this.

Is it safe to go higher ? Or is there another way to do this ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: batch resize + crop product images to ratio for ecommerce

Post by fmw42 »

It is safe to go higher until you reach a fuzz value that loses some of your object or its gone all together. Perhaps you need to use some kind of filter to remove noise spots. See -morphology close or my script isonoise. Or -enhance or -despeckle.

Or perhaps better, threshold your image, use -connected-components on the binary image to remove noise. Then use that result as a mask to remove the noise from your original image. See http://magick.imagemagick.org/script/co ... onents.php
Post Reply