Imagemagick scale and image quality - image

I'm scaling an image down to 50% of its ratio with the convert command and -scale parameter. The generated image quality is pretty bad. Is there any extra option i can use to get a better result ?

-scale is quick but I belive -resize or -thumbnail is better and you can use any filters you like.

Using -sharpen 0x1.2 with -resize x% with -quality 95 produces good results for me.

I was trying to create thumbnails of PDFs and was getting poor results until I added -density 400, which is a suggestion I found here. Below is an example of the difference it can make. I got the PDF from here. Look at the bear and also the lines behind the bird.
Without -density 400
Full command: convert -resize 500x500 catalog.pdf[0] catalog-page1-resized.png
File size: 180 KB
With -density 400
Full command: convert -density 400 -resize 500x500 catalog.pdf[0] catalog-page1-resized.png
File size: 185 KB

Using -quality 80 -adaptive-resize is better for larger photos.
If you need to blur the output, use -interpolative-resize instead of -adaptive-resize

Related

ImageMagick: resize image and reduce file size

Using ImageMagick, I'm trying to resize a JPEG's dimensions and reduce the file size.
The issue is that I don't want to worsen the image quality.
I've tried the following commands:
convert -resize 170x80 -resample 100x100 image1.jpg image2.jpg
=> A resized picture but with bad quality.
convert -resize 170x80 -quality JPEG image1.jpg image2.jpg
=> A resized image and with good quality, but the same file size.
convert -density 600 -resize 170x80 image1.jpg image2.jpg
=> A resized image but very bad quality.
I don't know what option I should use.
quality parameter has a numeric value. From -quality doc :
For the JPEG and MPEG image formats, quality is 1 (lowest image quality and highest compression) to 100 (best quality but least effective compression). The default is to use the estimated quality of your input image if it can be determined, otherwise 92.
You may use quality lower than the default 92 to reduce the size, e.g. 70 as:
convert -resize 170x80 -quality 70 image1.jpg image2.jpg
I've managed to solve this issue using convert and mogrify :
convert -flatten -colorspace RGB myImage.jpg myImage.jpg
&&
mogrify -quality JPEG -geometry 170x80 myImage.jpg

How to crop empty white space in PDF file using Preview on Mac?

There is a comic book PDF file which has a lot of white space at the bottom.
The content is almost half the length of the page size.
How to crop all pages in the PDF file?
I have tried imagemagick but the quality is poor.
convert -verbose -density 300 -interlace none -quality 100 output.pdf
In ImageMagick, try using a larger density and then resize by the inverse amount in percent. Here, I use density = 4*72 = 288 and then resize by 25% (1/4).
convert -density 288 image.pdf -resize 25% -fuzz 15% -trim +repage result.pdf

Compressing an image to make it less than 4KB

I have an image of a person and I want to compress it to make it less than 4KB. I need to compress it and still have the face of the person recognizable even if the image will shrink.
Here is Theresa May at 142kB:
and resized to 72x72 and converted to greyscale and reduced to 2kB with ImageMagick at the command line:
convert original.jpg -resize 72x72 -colorspace gray -define jpeg:extent=2kb result.jpg
I can still recognise her.
Here is some other guy reduced to 1kB and I can still recognise him too:
ImageMagick is installed on most Linux distros and is available for macOS and Windows. Bindings are available for Python, PHP, Ruby, Javascript, Perl etc.
If you had further knowledge about your images, or your recognition algorithm, you may be able to do better. For example, if you knew that the centre of the image was more important than the edges, you could slightly blur, or reduce contrast in relatively unimportant areas and use the available space for more details in the important areas.
Mark Setchell has the right idea. But I might suggest one potential minor improvement. Remove any meta data including profiles, EXIF data etc. You can do that by either adding -strip
convert input.jpg -strip -resize 72x72 -colorspace gray -define jpeg:extent=2kb result.jpg
or by using -thumbnail rather than -resize. The former automatically does the strip.
convert input.jpg -thumbnail 72x72 -colorspace gray -define jpeg:extent=2kb result.jpg

Which Imagemagick convert parameters should be used to create a GPG public key photo?

Per GPG guidelines, photo IDs should be about 240x288 pixels and 4k-6k in size. I used: convert -resize 240x240 -quality 75 original.jpg gpguid.jpg. However, the resulting file is still 17k. Specifically, I am curious what options I have before decreasing the quality level.
This is the command I settled on: convert -resize 240x240 -quality 55 -strip -sampling-factor 4:2:0 original.jpg gpguid.jpg.
The original command yielded a file that was 17,169 bytes. The -strip argument brought the size down to 10,226 bytes by removing profiles and comments. The -sampling-factor argument reduced the size to 8,315 bytes by cutting the chroma channel's resolution in half without affecting the luminance resolution. Finally, turning down the quality to 55 brought me within the recommended 6K.
ImageMagick has a feature that allows you to set a maximum output file size, and you may find you get on better letting it do that rather than arbitrarily reducing quality to the point that all photos are within the specified size - that way you should hopefully retain as much quality as possible:
convert input.jpg -strip -resize 240x240 -define jpeg:extent=6kb result.jpg
The above command will result in a file just under 6kB.
If you want a way to do something similar with Python, I wrote an answer that works pretty well here. It does a binary search for a JPEG quality that satisfies a maximum size requirement.

ImageMagick - get optimal compression

I compared ImageMagick with other tools. In my example I just want to resize and compress images. The goal is an acceptable file size with good quality.
Original file size: 127 Kb
Comparison between ImageMagick and Caesium
Unscaled, quality set to 80%
ImageMagick: convert image.jpg -strip -quality 80 optImage.jpg
=> 123 Kb
Casesium: 101 Kb
Scaled to 640x359, quality set to 80%
ImageMagick: convert image.jpg -strip -resize 640x359! -quality 80 optImage.jpg
=> 48 Kb
Caesium: 33.6 Kb
So what is wrong with that? Is there any ImageMagick-option I should include? Or is the quality parameter different between these tools?
EDIT: is there any Linux shell tool which is able to resize (maybe crop) and compress as good as Caesium?
I found out that these quality-parameters are not the same; 80% quality on ImageMagick corresponds to 87% (not exact) in Caesium; to get a acceptable file size the ImageMagick quality-parameter should set to 80 (not losless). But I think it's not bad to use an extra losless compression for ImageMagick-resized images like jpegtran for JPGs and optiPNG for PNGs. They can reduce the file size a bit more.
If you have an acceptable file size in mind, you can tell ImageMagick what it is and it will do its best to honour it.
Like this:
convert image.jpg -strip -define jpeg:extent=88kb optImage.jpg
If you want a way to do something similar with Python, I wrote an answer that works pretty well here. It does a binary search for a JPEG quality that satisfies a maximum size requirement.

Resources