The Artima Developer Community
Sponsored Link

The Explorer
Converting .jpeg images in .cbz format
by Michele Simionato
September 2, 2009
Summary
A quick recipe for readers of digital comics

Advertisement

There are two standards formats for digital comics, denoted by the extensions .cbz (zipped files) and .cbr (files compressed with the rar utility). There are many free programs to read comics in such formats: for instance CDisplay on Windows and Comix on Linux. The other day I got a comic book as a set of images in different directories, and I could not read it with Comix. So I wrote the following Python script to generate the .cbz files for me. It may be of use to somebody, so I have decided to put it in the public domain (it took me 15 minutes to write it). You can also use it to zip all the .jpeg images you have in your hard disk, especially your pictures.

"""
A small program to collect jpeg images into .cbz files. Usage:

$ python %s <root>

The program recursively scans all subdirectories of <root>.
"""

import os, sys, zipfile

def write(msg):
    sys.stdout.write(msg)
    sys.stdout.flush()

def cancel(msg):
    write('\x08' * len(msg))

def makecbz(dirname):
    for cd, dirs, files in os.walk(dirname):
        images = [os.path.join(cd, f) for f in files if f.lower().endswith(
                ('.jpg', '.jpeg'))]
        if images:
            out = cd + '.cbz'
            write('Writing %s [0000]' % out)
            z = zipfile.ZipFile(out, 'w')
            try:
                for i, img in enumerate(images):
                    msg = '[%04d]' % (i+1)
                    cancel(msg)
                    z.write(img)
                    write(msg)
            finally:
                write('\n')
                z.close()

if __name__ == '__main__':
    try:
        dirname = sys.argv[1]
    except IndexError:
        sys.exit(__doc__ % sys.argv[0])
    makecbz(dirname)

Talk Back!

Have an opinion? Readers have already posted 3 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Michele Simionato adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Michele Simionato started his career as a Theoretical Physicist, working in Italy, France and the U.S. He turned to programming in 2003; since then he has been working professionally as a Python developer and now he lives in Milan, Italy. Michele is well known in the Python community for his posts in the newsgroup(s), his articles and his Open Source libraries and recipes. His interests include object oriented programming, functional programming, and in general programming metodologies that enable us to manage the complexity of modern software developement.

This weblog entry is Copyright © 2009 Michele Simionato. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use