Image Magick - How to compress for Google page speed? - imagemagick

I'm trying to find the right command for compressing png and jpg files good enough for Google Page speed.
I'm using the following from my OSX termial:
convert -strip -quality 85 imagesource.png imagesource_optimized.png
The result is no difference in file size between the original and "optimized version. Am Is there a different command I can use?

Compression is fundamentally different between PNG and JPEG because PNG is lossless and JPEG is lossy.
With a JPEG the -quality parameter specifies how much quality should be retained.
With a PNG file, the -quality parameter specifies the strategy and the amount of time/effort zlib can spend to optimise your image - think gzip --best versus gzip --fast.
See https://www.imagemagick.org/script/command-line-options.php#quality
For PNG, I suggest pngcrush which you can install with homebrew using:
brew install pngcrush
For JPEG, either use -quality or specify a maximum size:
# Specify by quality
convert input.jpg -strip -quality 75% output.jpg
# Specify maximum size
convert input.jpg -strip -define jpeg:extent=300k output.jpg

Compression is fundamentally different between PNG and JPEG because PNG is lossless and JPEG is lossy.

Related

How to restrict tiff file size using ImageMagick and python?

I am converting a pdf to .tiff file using ImageMagick and calling it from python using subprocess,run() but, I want the output file size to be limited to a maximum value say 40MB. -define extent max_value is not working for tiff like it works for jpeg images.
This is my code:
subprocess.run('magick convert -density 150 example.pdf -trim -thumbnail 500 result%04d.tif')

Why does imagemagick convert increases file-size

I am using ImageMagick to convert png files to tiff. But after conversion the file size seems to increase by a huge amount? Any suggestion on what am i doing wrong here?
Command that I am using for conversion is : convert tiftest1.png tiftest2.png output_file.tif
PNG files are analysed (filtered) on a line-by-line basis to see how best each line can be compressed relative to the previous one, then compressed. TIFF files, in general, are not.
You need to consider whether you can accept lossless or lossy compression and then tell the TIFF encoder.
If only lossless compression is acceptable:
magick input1.png input2.png -compress LZW result.tif
If lossy compression is acceptable (and compatible with downstream needs):
magick input1.png input2.png -compress JPEG result.tif
Check actual compression used in a file with:
magick identify -verbose image.tif
Check available types of compression with:
magick identify -list compress
When converting to tiff, ImageMagick produces a non-compressed file by default. You can tell it to use LZW or ZIP or JPEG or other compressions. PNG is always compressed.

how to convert scanned jpg files to pbm format losslessly?

Using ImageMagick's convert utility to convert some scanned jpg files to pbm files.
However, even if the option -quality 100 is used, the pbm's resolution still looks worse than the original scanned jpg file.
Worse, the scanned jpg file is a colored one, while the converted pbm is black and white.
Info of original jpg:
image size: 2256 × 1568 pixels
dpi: 300 pixels/inch
color model: RGB
info of the converted pbm:
image size: 2256 × 1568 pixels
dpi: 72 pixels/inch
color model: Gray
Currently, here is what I did to convert the format:
qiang#bonjour:~/scan$ convert scan000.jpg scan000.pbm
Am I missing any option to use with convert? As I mentioned earlier, -quality 100 had been tried, but to no avail.
Using ImageMagick, I think you want to output to PPM not PBM. Try
convert image.jpg image.ppm
or try the ascii version by using
convert image.jpg -compress none image.ppm
PBM is binary (black/white) and PGM is grayscale. If you want to keep color, then you need to use PPM.
Unfortunately, I believe that ImageMagick can only read DJVU format images. So you cannot write to it directly from ImageMagick.

Lossless YCbCr Tiff?

My goal is to create a Tiff image that natively holds uncompressed (or with lossless compression) YCbCr data inside (since the original image is YUV420 and IMO it would be a poor choice to upsample and convert to RGB).
I’m able to create (using imagemagick for instance) a valid YCbCr Tiff with a JPEG compressed data. When I try uncompressed / deflate / lzw I get a broken image (neither windows image viewer nor photoshop can open it). On page 94 of the TIFF 6.0 spec (under “Minimum Requirements for YCbCr Images”):
Compression = none (1), LZW (5) or JPEG (6). SHORT.
What’s wrong? Is there a Tiff extension which says that YCbCr color space is supported only when using jpeg compression?
Thanks,
Mark.
Command that works:
convert infileRGB.tif -colorspace ycbcr -compress jpeg outfileYCbCr.tif
Commands that don't work (broken tiff):
convert infileRGB.tif -colorspace ycbcr -compress none outfileYCbCr.tif
convert infileRGB.tif -colorspace ycbcr -compress lzw outfileYCbCr.tif

how to reduce the size with imagemagick when the original file is too big

I converted a 2.9M jpg to a 20x20 using 8 as the quality. but that file's size is still 48k.
here is my command
convert 238832c58dc3bc0b_29M.jpg -quality 8 -resize '20x20>' +repage 238832c58dc3bc0b_20x20.jpg
and after conveted, 238832c58dc3bc0b_20x20.jpg is 48k. I tried smalled size and quality, still 48k. it shouldn't be so big. it should be less than 10k. anybody know how to enhance it? thanks
Use -thumbnail instead of -resize or add -strip to your command.
Thumbnail removes the EXIF information apart from the color profile and strip removes the color profile as well.

Resources