Skip to content

Commit

Permalink
[ADDED] support for nested folders inside files
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulMonnery committed Oct 29, 2019
1 parent 361b47d commit 1638b9d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 51 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ data/
target/
src/
.vscode
*~
*~
*.ui
111 changes: 61 additions & 50 deletions comics2pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,71 +25,79 @@ def separator():
return ('/')


def handle_rar(filein):
tmp_dir = tmp_directory + separator() + "c2p" + separator()
os.mkdir(tmp_dir)
def handle_rar(file_to_exctract, tmp_dir):
try:
os.mkdir(tmp_dir)
except:
print("Temporary folder already exists")
print("Extracting pictures in the CBR file...")
patoolib.extract_archive(filein, outdir=tmp_dir)
newfile = filein.replace(filein[-4:], ".pdf")
patoolib.extract_archive(file_to_exctract, outdir=tmp_dir)
newfile = file_to_exctract.replace(file_to_exctract[-4:], ".pdf")
print("Creating the PDF file...")
to_pdf(newfile, tmp_dir, 7)
to_pdf(newfile, tmp_dir)
shutil.rmtree(tmp_dir, ignore_errors=True)
print('\x1b[1;32m' + "\"" + newfile[:-4] + "\" successfully converted!" + '\x1b[0m')


def handle_zip(filein):
tmp_dir = tmp_directory + separator() + "c2p" + separator()
zip_ref = zipfile.ZipFile(filein, 'r')
def handle_zip(file_to_exctract, tmp_dir):
zip_ref = zipfile.ZipFile(file_to_exctract, 'r')
print("Extracting pictures in the CBZ file...")
zip_ref.extractall(tmp_dir)
zip_ref.close()
newfile = filein.replace(filein[-4:], ".pdf")
newfile = file_to_exctract.replace(file_to_exctract[-4:], ".pdf")
print("Creating the PDF file...")
to_pdf(newfile, tmp_dir, 0)
to_pdf(newfile, tmp_dir)
shutil.rmtree(tmp_dir, ignore_errors=True)
print('\x1b[1;32m' + "\"" + newfile[:-4] + "\" successfully converted!" + '\x1b[0m')


def to_pdf(filename, newdir, ii):
ffiles = os.listdir(newdir)
if (len(ffiles) == 1):
to_pdf(filename, newdir + ffiles[0] + separator(), ii)
else:
im_list = list()
firstP = True
im = None
index = 0
list_len = len(ffiles)
for image in sorted(ffiles):
index += 1
sys.stdout.flush()
sys.stdout.write("Conversion: {0:.0f}%\r".format(
index / list_len * 100))
if (image.endswith(".jpg") or image.endswith(".JPG")
or image.endswith(".jpeg") or image.endswith(".JPEG")):
im1 = Image.open(newdir + image)
try:
im1.save(newdir + image, dpi=(96, 96))
except:
print("Error")

if (firstP):
im = im1
firstP = False
else:
im_list.append(im1)
else:
continue
print("Saving the PDF file...")
im.save(filename, "PDF", resolution=100.0, save_all=True, append_images=im_list)
shutil.rmtree(newdir, ignore_errors=True)
def get_files(f, dir):
files = os.listdir(dir)
for file in files:
path = dir + separator() + file
if os.path.isdir(path):
get_files(f, path)
else:
f.append(path)


def to_pdf(filename, newdir):
image_list = []
get_files(image_list, newdir)
im_list = list()
is_first_picture = True
im = None
index = 0
list_len = len(image_list)

for image in sorted(image_list):
index += 1
sys.stdout.flush()
sys.stdout.write("Conversion: {0:.0f}%\r".format(index / list_len * 100))
img = Image.open(image)
try:
if img.mode == 'RGBA':
img = img.convert('RGB')
img.save(image, dpi=(96, 96))
except:
print("Error")

if (is_first_picture):
im = img
is_first_picture = False
else:
im_list.append(img)
print("Saving the PDF file...")
im.save(filename, "PDF", resolution=100.0, save_all=True, append_images=im_list)
shutil.rmtree(newdir, ignore_errors=True)


def launch_convert(file):
tmp_dir = tmp_directory + separator() + "c2p" + separator()
if (file[-4:] == '.cbz' or file[-4:] == '.zip'):
handle_zip(file)
handle_zip(file, tmp_dir)
elif (file[-4:] == '.cbr' or file[-4:] == '.rar'):
handle_rar(file)
handle_rar(file, tmp_dir)


def opendir(directory):
Expand All @@ -99,15 +107,18 @@ def opendir(directory):
print("WARNING: some items were skipped")


def start():
def main():
if len(sys.argv) > 1:
if sys.argv[1] == '-d' and os.path.isdir(sys.argv[2]):
opendir(sys.argv[2])
elif sys.argv[1] == '-f' and os.path.isfile(sys.argv[2]):
launch_convert(sys.argv[2])
else:
print("Bad argument. Please use:\n\t-d [path/to/folder] to all files in folder\n\t-f [path/to/file] to convert a single file")
print("Bad argument. Please use:\n\t-d [path/to/folder] to all \
files in folder\n\t-f [path/to/file] to convert a single file")
else:
print("Please specifie arguments.\n\t-d [path/to/folder] to all files in folder\n\t-f [path/to/file] to convert a single file")
print("Please specifie arguments.\n\t-d [path/to/folder] to all \
files in folder\n\t-f [path/to/file] to convert a single file")

start()
if __name__ == "__main__":
main()

0 comments on commit 1638b9d

Please sign in to comment.