Changing the bit-depth of figures produced using Matplotlib
Asked Answered
S

1

7

I'm using matplotlib to generate some figures via savefig. These figures are black and white and need to be saved at a very high resolution (1000 dpi) in TIFF format. It would therefore be beneficial to save them with a reduced bit depth so as to use less memory.

To that end, my question: how does one specify the bit depth when saving figures with matplotlib?

Thanks!

Snowflake answered 12/9, 2014 at 14:12 Comment(4)
You mean to save to PNG?Decrease
I'm actually saving in TIFF format, although am interested in answers relating to any raster format, including PNG.Snowflake
I would be surprised if matplotlib supports such an option directly, but you can of course always convert the image afterwards. Alternatively, you can use your favorite python image library to save a numpy array image representation generated from matplotlib content.Vraisemblance
Thanks David! I did as you suggest. I gave up trying to do it in matplotlib for the reasons given in my answer. Cheers!Snowflake
S
1

So far I get the impression that matplotlib doesn't support a bit-depth option. I'm thus using imagemagick to convert the image posthoc:

convert -monochrome +dither A.tiff B.tiff

Several things I'll mention in case someone else is trying to do similarly:

When I first changed the bitdepth by running convert -monochrome A.tiff B.tiff, the fonts looked unacceptably ugly (even at 1000 DPI!). This was because of antialiasing, which matplotlib performs by default. I couldn't find any option to turn this off, but its negative effects (when downsampling the DPI) can be largely circumvented by enabling dithering. Therefore, even if there is an option to change the DPI of the output image in matplotlib, it isn't useful unless it performs dithering or unless there's also an option to disable antialiasing.

Short answer, I would suggest to anyone in a similar situation as me to do their monochrome conversion posthoc as I have done.

Snowflake answered 15/9, 2014 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.