2

I have an opencv image:

img = cv2.imread(filename)
img = cv2.GaussianBlur(img, (5, 5), 0.15)

now I want to write it directly into zip

zipf = zipfile.ZipFile('out.zip', 'w', zipfile.ZIP_DEFLATED)
zipf.write(...)

Guess it's something to do with cv2.imencode() but not sure.
Can you help? Thanks

Eran
  • 423
  • 4
  • 12

2 Answers2

2

Found the answer:

zipf = zipfile.ZipFile('out.zip', 'w', zipfile.ZIP_DEFLATED)
retval, buf = cv2.imencode('.png', img)
zipf.writestr(name_in_zip, buf)

works like a charm.

Eran
  • 423
  • 4
  • 12
0

It seems that you can find answers to your question under this link: stackoverflow.com/questions/30049201/how-to-compress-a-file-with-shutil-make-archive-in-python/45711390

aurelia
  • 493
  • 8
  • 12
  • it's for the general case, but i wanted to know how to do it for opencv images specifically. – Eran Dec 01 '20 at 14:28