Extra Nerd
Alright, so I have a bunch of comics stored on my computer in .cbr and .cbz formats. These are popular archive formats for comic books and have been used for some time. However, I also have a Nook Color that cannot read .cbr or .cbz files, but can read .pdfs. So I needed to convert all my files from .cbr or .cbz to .pdf.
Originally, I was using a very useful program called Calibre to do this. It is also a library manager for e-books and other things, and can copy stuff to the Nook Color. Unfortunately, Calibre apparently has a problem of randomly losing the ability to unarchive comic files to transform them into PDF files. This put a serious hitch in my getalong.
(Edit long after the fact: Calibre had a statically linked p7zip library that came with it. In whatever version of Linux I was running at the time, the distributed version of Calibre could not detect this library. This appears to have been fixed in more recent versions of the program, making this entire post moot, but…it was still fun to do some reasonably useful coding for a change.)
After a little work (and learning quite a bit about python), I managed to write my own conversion script (although it’s nowhere near as pretty, it does the job). So here it is, so if I lose track of it on my computer, at least it’ll still be somewhere:
#!/usr/bin/python
from subprocess import call
import os
import string
import shutil
from pyPdf import PdfFileWriter, PdfFileReader
ComicExtList = [".cbr",".cbz", ".cb7"]
ImageExtList = [".jpg",".gif",".png", ".jpeg", ".JPG", ".GIF", ".PNG", ".JPEG", ".tif", ".tiff", ".TIF", ".TIFF", ".bmp", ".BMP"]
ComicConvertList = []
ImageConvertList = []
call("clear")
#setpath = raw_input("Enter the path: ")
setpath = os.getcwd()
if setpath[len(setpath) - 1] == "/":
setpath = setpath[0:len(setpath) - 1]
tempdir = setpath + "/output"
for file in os.listdir(setpath):
if os.path.splitext(file)[1] in ComicExtList:
ComicConvertList.append(file)
print "%d files to convert." % (len(ComicConvertList), )
print ""
ComicConvertList.sort()
for fileIndex in range(len(ComicConvertList)):
if os.path.exists(tempdir) == 0:
os.mkdir(tempdir)
ComicFile = ComicConvertList[fileIndex]
print ""
print ""
print "File %d of %d" % (fileIndex + 1, len(ComicConvertList), )
print ComicFile
print ""
shutil.move(setpath + "/" + ComicFile, tempdir + "/" + ComicFile)
os.chdir(tempdir)
if ComicFile.endswith("cbr"):
call(["unrar", "e", "-inul", tempdir + "/" + ComicFile])
else:
call(["7z", "e", tempdir + "/" + ComicFile], shell = True, stdout = None, stderr = None)
for file in os.listdir(tempdir):
if os.path.splitext(file)[1] in ImageExtList:
ImageConvertList.append(file)
if len(ImageConvertList) > 1:
ImageConvertList.sort()
pdfCatList = PdfFileWriter()
for ImageFileIndex in range(len(ImageConvertList)):
ImageFile = ImageConvertList[ImageFileIndex]
ImageFilePDF = ImageFile[0:len(ImageFile) - 3] + "pdf"
call(["convert", ImageFile, ImageFilePDF])
tempPDF = PdfFileReader(open(ImageFilePDF, "rb"))
pdfCatList.addPage(tempPDF.getPage(0))
outputPDF = open(setpath + "/" + ComicFile[0:len(ComicFile) - 3] + "pdf", "wb")
pdfCatList.write(outputPDF)
outputPDF.close()
shutil.rmtree(tempdir)
ImageConvertList = []
else:
shutil.move(tempdir + "/" + ComicFile, setpath + "/" + ComicFile)
shutil.rmtree(tempdir)
print "Error extracting " + ComicFile