Have the following question for Imagemagick (or another solution as it works)
Case: with photoshop i crop images before uploading to my webshop. We sell for examples trousers. I automatically cut them, so the whitespace is removed, and I get a nice image of only the trouser. The issue is that the width of that image can be lower then 250px. And Google wants there images to have a minimal size of 250×250 for clothes on google shopping.
So I look an solutino for the following:
Search images in subdirectories with an Height or an Width lower then 250px on my linux webserver. Add canvas size to the height or the width (centered) so that the minimum width or height is 250px.
What is the best (and fastest) solution? I have ALOT of images.. 🙂
Thanks,
HRR
==
Edit: To clearify:
- Old size (example 1): 180×900 – > New Size 250 x 900.
- Old size (example 2): 900×240 – > New Size: 900 x 250.
So I dont want an square 250×250 image. The lowest point needs to be a minimum width or height as 250px
And I work with images with .jpg
And the images should be overwritten.
Thanks!
4
Answers
In ImageMagick, you can use -extent to pad the image. Assuming you want the pad all around (centered image), then
convert image -background somecolor -gravity center -extent 250x250 result
where somecolor can be any color including “transparent”. If you want ImageMagick to do the trimming as well, then
convert image -fuzz XX% -trim +repage -background somecolor -gravity center -extent 250x250 result
where XX is some value between 0 and 100, typically near zero, that allows the background to vary some from a uniform color. It allows some percent variation in background color to remove.
If you have larger images, you can have imagemagick resize them also.
convert image -fuzz XX% -trim +repage -resize 250x250 -background somecolor -gravity center -extent 250x250 result
The best solution is:
image of the size you specified and then either delivers from the
cache or resizes->stores in the cache->delivers image.
What webstore are you using that doesn’t already do this?
ImageMagick typically includes the “mogrify” program which can resize, center, and pad the images in bulk. A command like this will resize every *.png image in the current directory to fit inside a maximum of 250 pixels width or height, pad them to 250×250 with white to fill the padding, and write them to the original file names.
That will overwrite the original images, so consider running it on a copy, or use mogrify’s “-path somewhere” to specify another directory for the output files.
This command using ImageMagick’s “mogrify” will run through all the *.jpg images in the current directory, and center each image in white padding as needed to make the minimum dimensions 250 pixels wide and high. Dimensions more than 250 pixels are left unchanged.
That calculates the necessary dimensions of the viewport to be at least 250 pixels in both directions. Then, only if the image is smaller than that viewport one way or another, it slides the image over or down, or both, to center it in the now enlarged viewport. The virtual-pixel setting assures the padding will be white.
It will overwrite the input images, so use due caution.
You might want to include “-quality 100” to minimize the quality loss that happens when saving in the JPG format.
I worked up this command with IM7 in Windows, but this command is in *nix syntax. I tested it with IMv6.8.9 in bash, and it seems to work properly.