5

I'd like to save multiple objects, e.g. figures, created in a loop directly to a zip file, without saving them in directory.

At the moment I'm saving the figures in a folder and then zipping them.

    import matplotlib.pyplot as plt
    from zipfile import ZipFile
    
    for i in range(10):
        plt.plot([i, i])
        plt.savefig('fig_' + str(i) + '.png')
        plt.close()

    image_list = []
    for file in os.listdir(save_path):
        if file.endswith(".png"):
            image_list.append(os.path.join(save_path, file))

    with ZipFile(os.path.join(save_path, 'export.zip'), 'w') as zip:
        for file in image_list:
            zip.write(file)

In case of positive answer, is there a way to do the same for any kind of object or does it depend on object type?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Look at `ZipFile.writestr()`. Desplite the function name, the parameter `data` may be `bytes` or a string, and if it is a string it will be encoded as UTF-8 before being stored. – BoarGules Apr 10 '19 at 16:14
  • Hi, it's not clear how to save each figure with the relative name in the final zip file, even if I create an empty zip file and then I append with `zipfile.ZipFile('zipfile_append.zip', mode='a')` – Alessandro Bitetto Apr 11 '19 at 08:30

1 Answers1

5
  1. @BoarGules pointed out [Python.Docs]: ZipFile.writestr(zinfo_or_arcname, data, compress_type=None, compresslevel=None) that allows creating a zip file member with contents from memory

  2. [SO]: how to save a pylab figure into in-memory file which can be read into PIL image? (@unutbu's answer) shows how to save a plot image contents in memory ([Matplotlib]: matplotlib.pyplot.savefig), via [Python.Docs]: class io.BytesIO([initial_bytes])

  3. All you have to do, is combining the above 2

code00.py:

#!/usr/bin/env python

import sys
import matplotlib.pyplot as plt
import zipfile
import io


def main(*argv):
    zip_file_name = "export.zip"
    print("Creating archive: {:s}".format(zip_file_name))
    with zipfile.ZipFile(zip_file_name, mode="w") as zf:
        for i in range(3):
            plt.plot([i, i])
            buf = io.BytesIO()
            plt.savefig(buf)
            plt.close()
            img_name = "fig_{:02d}.png".format(i)
            print("  Writing image {:s} in the archive".format(img_name))
            zf.writestr(img_name, buf.getvalue())


if __name__ == "__main__":
    print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(sys.argv[1:])
    print("\nDone.")
    sys.exit(rc)

Output:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q055616877]> dir /b
code00.py

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q055616877]> "e:\Work\Dev\VEnvs\py_pc064_03.08.07_test0\Scripts\python.exe" code00.py
Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)] 64bit on win32

Creating archive: export.zip
  Writing image fig_00.png in the archive
  Writing image fig_01.png in the archive
  Writing image fig_02.png in the archive

Done.

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q055616877]> dir /b
code00.py
export.zip

As you'll notice, the (image) files are not compressed at all (archive size is slightly greater than the sum of the member sizes). To enable compression, also pass compression=zipfile.ZIP_DEFLATED to ZipFile's initializer.

CristiFati
  • 38,250
  • 9
  • 50
  • 87