BMP -> PNG -> BMP adds 50% to file size?!

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
zaaj
Posts: 3
Joined: 2011-10-27T13:50:27-07:00
Authentication code: 8675308

BMP -> PNG -> BMP adds 50% to file size?!

Post by zaaj »

I'm trying to propose storing test result BMP images as PNG, and I've written a Windows CMD file to mass convert tens of GB of BMPs using ImageMagick 6.7.3-2 2011-10-21 Q8. It works great, but I wanted to make sure I can re-create an identical BMP file if it's needed for further processing later. I can convert back to BMP, but an 8MB BMP converts to a 4MB PNG, which converts with ImageMagick to 12MB. Opening the PNG with the GIMP and saving as BMP brings it back to the original BMP's file size. All images compare to 0 different pixels with compare using:
compare -metric AE -fuzz 0
and the only difference between the outputs of identify -verbose on the different size BMPs is the resolution, is tagged in the larger BMP, file size/time stamps and the identify performance metrics.

I've searched around for settings to try, after making sure I was runing a Q8 version, such as adding +matte or -colors 256, or even going through ppm and a second convert to throw away any gamma info which might have been in the PNG. Oddly enough, -colors 256 gives the 12MB file, but -colors 255 gives a 4MB BMP file - half the original's 8MB. identify -verbose states that the 4MB bmp has a depth of 4/8-bit, Gray: 8-bit, but Colormap: 16, and only 16 colors are listed in the map.

The 12MB files are probably fine to use, but I'd like to understand what's going on, and be able to reproduce the original file size if possible, since this whole project was to save server disk space. Anyone?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: BMP -> PNG -> BMP adds 50% to file size?!

Post by fmw42 »

I don't believe I can help much as I know little about BMP, but you find what you need at http://www.imagemagick.org/Usage/formats/#bmp
zaaj
Posts: 3
Joined: 2011-10-27T13:50:27-07:00
Authentication code: 8675308

Re: BMP -> PNG -> BMP adds 50% to file size?!

Post by zaaj »

Thanks for the really quick reply. Unfortunately, that's one of the pages I'd already found and where I got the suggestions to try +matte and piping through PPM format...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: BMP -> PNG -> BMP adds 50% to file size?!

Post by fmw42 »

Did you try BMP3 or BMP2?
zaaj
Posts: 3
Joined: 2011-10-27T13:50:27-07:00
Authentication code: 8675308

Re: BMP -> PNG -> BMP adds 50% to file size?!

Post by zaaj »

I thought I had - I know I had tried BMP3 with no difference I could tell, but BMP2 did the trick - brought the PNG back to a BMP the same size as the original. BMP3 and 4 must include extra info per pixel, even in Q8 versions. Thanks, fmw42, for prompting me to be that much more thorough in trying every option, it's just what I needed!

So, in case an example would help anyone else, I had been using:

Code: Select all

convert -quality 94 %BMP% %PNG%
The "4" in "-quality 94" specifies a filter which imperically gave me the best compression on the BMP images I'm working with, YMMV based on your images of course.

My batch file then programatically checks to make sure the resulting image was identical:

Code: Select all

compare -metric AE -fuzz 0 %BMP% %PNG% miff: 2>&1 1> nul | grep -q "^0$"
and if the error level was 0 (found 0 pixels different), I'd delete the %BMP% file.
Now, I've got the return convert working as:

Code: Select all

convert %PNG% bmp2:%BMP%
(I had also found a snippit of Perl code which copied the time-stamp from one file to another at http://www.rocketaware.com/perl/perlfaq ... stamp_.htm, and include a call to that in my batch file, but that's a bit off topic for an ImageMagick forum, as is the Windows "FOR /F" loop syntax I found at http://ss64.com/nt/for_cmd.html)
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: BMP -> PNG -> BMP adds 50% to file size?!

Post by glennrp »

The -quality 94 shouldn't have any effect on the size of the BMP,
only on the size of the intermediate PNG. If you wouldn't mind sharing
one of your images that exhibits this behavior, please post a link
or mail it directly to me: glennrp at gmail.com
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: BMP -> PNG -> BMP adds 50% to file size?!

Post by glennrp »

Looking at the output of
  • convert 00min.png -debug coder default.bmp
versus
  • convert 00min.png -debug coder bmp2:bmp2.bmp
it seems that the relevant difference is that the (default)
BMP3 encoder uses BI_RLE8 compression while the BMP2
encoder uses BI_RGB.
  • convert 00min.png -compress none none.bmp
also produces a reasonably-sized output (just a few bytes
larger because of the bigger BMP3 header but otherwise
fine).
Post Reply