I have a rails 4 app, where I allow users to upload images. I have validations in place for min dimension of 640X385 and thus a min aspect ratio of 1.6623376623376624. Now that means user can't upload smaller images either in terms of dimension or aspect ratio. And when they upload a bigger image, it gets resized by ImageMagick's resize_to_fit method. But it resizes to a proportional width and height and my dimension's validation fails. If I use resize_to_fill, even the smaller images get stretched and bypass aspect ratio's validation. So How do I get the resize method to resize an image perfectly to 640X385 dimension?
So I do not know exactly what result you expect for various sizes. I tend to use ImageMagick on the commandline so I produced a small script with examples that may or may not help you:
#!/bin/bash
# cleanup
rm *.jpg
# as expected
convert -size 640x385 xc:white img0.jpg
# bigger aspect ratio (2.0)
convert -size 800x400 xc:white img1.jpg
# lower aspect ratio (1.0)
convert -size 800x800 xc:white img2.jpg
# bigger aspect ratio (2.0) but to small
convert -size 400x200 xc:white img3.jpg
# lower aspect ratio (1.0) but to small
convert -size 200x200 xc:white img4.jpg
for img in img*; do
# resize images to fit in box but keep aspect ratio
convert ${img} -resize 640x385 r1.${img}
# resize images keeping the aspectratio to fit in box. But only shrink
# images, images that are to small is not enlarged.
convert ${img} -resize 640x385\> r2.${img}
# resize images ignoring the aspectratio to fit in box. But only shrink
# images, images that are to small is not enlarged.
convert ${img} -resize 640x385\>\! r3.${img}
done
identify -format "%f: %w %h\n" *.jpg
The output is as follows:
img0.jpg: 640 385
img1.jpg: 800 400
img2.jpg: 800 800
img3.jpg: 400 200
img4.jpg: 200 200
r1.img0.jpg: 640 385
r1.img1.jpg: 640 320
r1.img2.jpg: 385 385
r1.img3.jpg: 640 320
r1.img4.jpg: 385 385
r2.img0.jpg: 640 385
r2.img1.jpg: 640 320
r2.img2.jpg: 385 385
r2.img3.jpg: 400 200
r2.img4.jpg: 200 200
r3.img0.jpg: 640 385
r3.img1.jpg: 640 385
r3.img2.jpg: 640 385
r3.img3.jpg: 400 200
r3.img4.jpg: 200 200
My guess is that you are looking for the last variant where the output is named r3.<something>.
For more info and good examples of how to use resize: http://www.imagemagick.org/Usage/resize/
Also have a look at:
Resizing the image with padding using convert on ubuntu
I hope this helps.
Related
I'm using the following command to resize all the images in a folder:
convert folder\*.png" -format jpg -resize 1573 -quality 70 -strip -density 72 -interlace Plane -set filename:fname %t-1 +adjoin "C:\Users\%USERNAME%\Desktop\New folder\%[filename:fname].jpg"
It works, but instead of resizing to 1,573 pixels, I'd like to resize to 8.5 inches in width and 11 inches in height. How can I do that using ImageMagick Convert?
The "print size" of an image is the combination of its size in pixels and it print resolution, the matter being just metadata:
print size (inches) = size in pixels ÷ print resolution (in PPI)
So to change your image to 8.5×11 inches you can just change the resolution:
Necessary resolution = 1513 pixels ÷ 11 inches = 138PPI
The usual tool to edit metadata is ExifTool:
exiftool -xresolution=138 -yresolution=138 -v2 your_image.jpg
This of course assumes that your image is already in the 11 ÷ 8.5 aspect ratio. Otherwise you compute different X and Y print resolutions or first crop the image to achieve the proper aspect ratio.
Also, watch out for low resolution values. If you have text or sharp lines in the image (logo, CGI) 150PPI is on the low side...
I'm trying to reduce the resolution of a large number of images using ImageMagick. The photos' original size is 5312 x 2988 pixels. The new dimensions should be 2000 x 1125 px.
Here is the command I'm using:
mogrify -path "C:\Users\Joe\Desktop\img\new" -resize 2000x1125 -auto-orient -format jpg -quality 75 "C:\Users\Joe\Desktop\img\*.jpg"
It works great for images that are in landscape mode. The problem is for images that are in portrait mode, with a resolution of 2988 x 5312 px. When I run the above command, their resulting size is 633 x 1125 px instead of 1125 x 2000.
Which command can I run that will produce, if the original image is 5312 x 2988, a resulting image of 2000 x 1125 (landscape). Or, if the original image is 2988 x 5312 (portrait), a resulting image of 1125 x 2000 pixels? It should be a single command since I have large number of images in a folder in both landscape and portrait modes.
Basically, the largest dimension should be 2000. If the photo is in landscape, the width should be 2000 px and the height should scale down proportionately. If the photo is in portrait mode, the height instead should be 2000 px.
The answer, as Mark suggested, ended up being using the -resize flag with only one parameter, e.g.: -resize 2000. When only one parameter is specified, the longest side will be 2000 (the other dimension will be scaled automatically and proportionately).
The caveat was, if you're using the -auto-orient flag in the same command, make sure to place -auto-orient AFTER -resize. That way, ImageMagick disregards image orientation when resizing.
mogrify -path "C:\Users\Joe\Desktop\Folder\New" -resize 2000 -format jpg -quality 75 -auto-orient "C:\Users\Joe\Desktop\Folder\*.jpg"
The key being calling -resize BEFORE -auto-orient.
I have been working on an image from last 25 days with zero output. So I came here in search of an answer.
I have a jpg image of 7MB. I upload it in Photoshop and changed the width to 96 Inch, Resolution for 300 pixels/inch, checked resample option and selected Preserve Details 2.0 and reduce noise to 100%. It gave me a 1.5 GB image as output.
Now I tried the same steps in image magic
gm convert -scale 768 -units PixelsPerInch -density 300x300 -resample 300x300 -noise 100% image.jpg -resize 768 image.tiff
Above command gave output in KBs. I need help.
If you want a 96inch wide image at 300dpi, you will need a width of 28,800 pixels, so start with:
gm convert input.jpg -resize 28800x result.jpg
That will resize the image to the correct width and do whatever is required with the height to preserve the image's aspect ratio - i.e. without distorting it.
I am trying without success to scale an image to 250 px width keeping aspect ratio using ImageMagick and then crop it from top corner (x,y) with size 160 px width and 160 px height.
I have tried this with PHP, but it s not working:
exec($homePath.'/ImageMagick/bin/mogrify -resize 250x -crop 160x160'+'.$x.'+'.$y
.' test.jpg');
I have even tried 2 instructions like below:
//Resize is not working
exec($homePath.'/ImageMagick/bin/mogrify -resize 250x test.jpg');
//Crop is working
exec($homePath.'/ImageMagick/bin/mogrify -crop 160x160'.'+'.$x.'+'.$y.' test.jpg');
I m using ImageMagick 7.0.7-4
Finally with the Help of #fmw42, who focus my attention on the term magick before mogrify in ImageMagick 7:
exec($homePath.'/ImageMagick/bin/magick mogrify -scale 250x
-crop 160x160+'.$x.'+'.$y.' test.jpg');
Thanks.
I am using ImageMagick to reduce the image resolution, height and width.
I have noticed a few things. When I am changing resolution at "Image Size" through Photoshop (version 7) from 300dpi to 150dpi image height and width automatically change.
With ImageMagick however I am not getting such variations. For example, if image contains 878 width and 179 height at 300dpi, when changing it to 150 dpi, automatically the image width changing to 439 and height 89 respectively.
Can any one support me how to obtain such changes through ImageMagick.
The dpi setting is not really relevant in most imaging applications/areas, until the point at which you want to print an image.
Do you really need to set it? I mean, if you want to half the size of an image, just use ImageMagick and do:
convert input.jpg -resize 50x50% output.jpg
and ignore the dpi.
To resize the image keeping the rendered size the same, you can use the -resample
option, like so:
$ convert original.jpg -resample 150x150 new.jpg
Using your example, if the original is an 878x179 image at 300DPI,
the result is an 439x90 image at 150DPI:
$ file original.jpg
original.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 300x300,
segment length 16, baseline, precision 8, 878x179, frames 3
$ file new.jpg
new.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 150x150,
segment length 16, baseline, precision 8, 439x90, frames 3
You can alternatively use the
-density
option along with the
-resize
option to achieve the same effect:
$ convert original.jpg -density 150x150 -resize 50%x50% new.jpg
In summary:
-density
just sets the DPI metadata without changing the underlying image;
-resize
changes the image size without changing the DPI;
-resample
changes the DPI and resizes the image accordingly.
Uses
The DPI metadata is relevant when you need to print an image or convert it to a PDF.
Now you can convert both images to PDF and get files with essentially the same page size:
$ convert original.jpg original.pdf
$ convert new.jpg new.pdf
$ pdfinfo original.pdf | grep -a "Page size:"
Page size: 210.72 x 42.96 pts
$ pdfinfo new.pdf | grep -a "Page size:"
Page size: 210.72 x 43.2 pts