0

I have a lot of tif images I would like to convert to a web friendly format. Ive been playing a little bit with imagemagick (and a couple other libraries) to try to convert them, but the resulting image is a lot larger than the original, uncompressed image.

For example:

1.tif - 348.2kB
1.png - 781.7 kB
1.jpg - 429.1 kb

2.tif - 49.8 kB
2.png - 76.2 kB
2.jpg 900.4 kB

3.tif 7.7 kB
3.png 21.4 kB
3.jpg 191.3 kB

The command im using:

convert *.tif -set filename: "%t" %[filename:].jpg

Im not an expert, but I dont understand how passing from an uncompressed source image to a compressed one makes the size explode. Any idea of what is happening?

After running the proposed command

identify -format "%f: size: %B, compression: %C\n" *png *tif *jpg

I get the following output

00000001.png: size: 104522, compression: Zip
00000002.png: size: 23565, compression: Zip
00000003.png: size: 58936, compression: Zip
00000001.tif: size: 74122, compression: Group4
00000002.tif: size: 10946, compression: Group4
00000003.tif: size: 29702, compression: Group4
00000001.jpg: size: 1011535, compression: JPEG
00000002.jpg: size: 226068, compression: JPEG
00000003.jpg: size: 457045, compression: JPEG
gmm
  • 943
  • 1
  • 17
  • 30
  • CCITT Group 4 (aka T.6) is *very* efficient for black/white (FAX) data. JPEG is inefficient for such data. 1 bit PNG with maximum compression should be close in size, but I don't think there's anything unexpected with what you see here. Instead it's the assumption that the original is uncompressed that is wrong. – Harald K Jan 14 '21 at 15:41
  • ok, thanks, thats good to know too. how can i set maximum compression for png? I know that 1 bit is set with the option -depth 1 (and it barely changes the size of the resulting image) – gmm Jan 14 '21 at 15:46
  • Perhaps [ImageMagick: Lossless max compression for PNG?](https://stackoverflow.com/questions/27267073/imagemagick-lossless-max-compression-for-png)? – Harald K Jan 14 '21 at 15:48

1 Answers1

2

It's hard to say what is happening without seeing your images - not all software uses the same compression method, or quality and some formats, such as PNG are lossless.

As a first attempt, try running this command to check the filename, file size, compression type of all your images:

identify -format "%f: size: %B, compression: %C\n" *png *tif *jpg

Sample Output

zHZB9.png: size: 1849, compression: Zip
result.tif: size: 290078, compression: None
z.tif: size: 213682, compression: LZW
sky.jpg: size: 88162, compression: JPEG
z.jpg: size: 8122, compression: JPEG

Now you can see the numbers you can decide what you want to address. So, to get a list of all the compression types you can use:

identify -list compress

Sample Output

B44A
B44
BZip
DWAA
DWAB
DXT1
DXT3
DXT5
Fax
Group4
JBIG1
JBIG2
JPEG2000
JPEG
LosslessJPEG
Lossless
LZMA
LZW
None
Piz
Pxr24
RLE
RunlengthEncoded
WebP
ZipS
Zip
Zstd

Now, you can experiment, e.g. by making some TIFFs with different settings:

convert -size 1024x768 gradient: 1.tif
convert -size 1024x768 gradient: -compress lzw 2.tif
convert -size 1024x768 gradient: -compress jpeg  3.tif
convert -size 1024x768 gradient: -compress jpeg  -quality 40 4.tif

Now check:

1.tif: size: 1573138, compression: None
2.tif: size: 6316, compression: LZW
3.tif: size: 17520, compression: JPEG
4.tif: size: 9830, compression: JPEG
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I updated my question with the output of the command you suggested. Ive tried experimenting as you suggested converting the tif to different compressions, but they all seem to weigh more than the original. Another weird thing is that a colleague opened one of the images with photoshop, and it says it weighs 1 MB. even though it says it the terminal its just a few kB. And after exporting it to any format, it weighs more than the original! – gmm Jan 14 '21 at 08:38
  • Can you please share the exact command you used and at least one original input and output file - you may need to use Google Drive, or Dropbox if imgur won't accept TIFFs. – Mark Setchell Jan 14 '21 at 08:42
  • yeah, of course: https://www.dropbox.com/s/mk0cl0o8u5aro8o/image_files.zip?dl=0 – gmm Jan 14 '21 at 09:04
  • ah, and the command is the one in the question, ```convert *.tif -set filename: "%t" %[filename:].jpg``` and the same with png – gmm Jan 14 '21 at 09:12
  • 1
    Your TIFFs are one-bit faxes. JPG won't work well, but you can make a one-bit PNG image from them and the size should be similar. – jcupitt Jan 14 '21 at 09:56
  • How? Ive tried ```convert 00000001.tif -depth 1 00000001_1bit.png``` And its a little bit smaller than the png with the original command, but still a lot larger than the tiff image – gmm Jan 14 '21 at 11:07
  • I don't know any way within the confines of the PNG specification to make an efficient representation of bi-level Group4-compressed data. Maybe Fred @fmw42 has an idea? – Mark Setchell Jan 14 '21 at 13:06