Create a BMP from pyplot

I am working on a data visualization in which the computational side is created in Python and the frontend in JavaScript. It is pretty sophisticated, Cartography of SPT2021.

I would like to fill polygons with word clouds. The only library for word clouds supporting polygonal shapes is wordcloud, which has a pretty simple code, SPT2021/masked.ipynb at main · rodighiero/SPT2021 · GitHub

My problem is that the mask is given by a BMP. When I create the image with Matplotlib, I don’t understand how I can create a BMP from my pyplot to send it to the wordcloud library.

Thank you for your help,
Dario

I don’t think Matplotlib can write bmp, but the Pillow library (former PIL) can do so. You could avoid writing a temporary by using a memory mapped file. Or call imagemagick on the command line to convert to bmp.