openzim / python-libzim

Libzim binding for Python: read/write ZIM files in Python
https://pypi.org/project/libzim/
GNU General Public License v3.0
60 stars 19 forks source link

Reading an image from Wikipedia ZIM #185

Closed guillaumeguy closed 5 months ago

guillaumeguy commented 5 months ago

Hi folks!

Thanks for this amazing work.

If folks want to read an image from ZIM, while the documentation is not super clear about it, it's actually possible (don't forget the I/ in the front, at least for the Wikipedia ZIm dump)

See sample code in Python:

from PIL import Image
import io

zim = Archive("PATH_TO_WIKIPEDIA_ZIM.zim")
print(f"Main entry is at {zim.main_entry.get_item().path}")
entry = zim.get_entry_by_path("I/William_Shakespeare_by_John_Taylor.jpg.webp")
print(f"Entry {entry.title} at {entry.path} is {entry.get_item().size}b.")
# load bytes of image
im = Image.open(io.BytesIO(entry.get_item().content))
im

A nice picture of Shakespeare should appear.

Happy to put it more formally in a notebook in /example if you'd like.

rgaudin commented 5 months ago

@guillaumeguy thank you for the snippet. I don't think we want to maintain an examples folder at the moment and the basics are in the README.

We are considering a better documentation though with a full API reference and some samples