From 361b47d405f354620337250bfbe3c60155de52c5 Mon Sep 17 00:00:00 2001 From: Paul Monnery Date: Wed, 16 Oct 2019 14:44:58 +0200 Subject: [PATCH] [ADDED] windows support --- .gitignore | 1 + comics2pdf.py | 49 ++++++++++++++++++++++------------------------- comics2pdf_gui.py | 21 +++++++++++++++----- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 29729f0..d44ce08 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ venv/ +data/ target/ src/ .vscode diff --git a/comics2pdf.py b/comics2pdf.py index b2465a3..4820df3 100755 --- a/comics2pdf.py +++ b/comics2pdf.py @@ -11,42 +11,49 @@ import patoolib from PIL import Image import PIL.ExifTags +import shutil +import tempfile +import platform + +tmp_directory = tempfile.gettempdir() +current_os = platform.system() + +def separator(): + if current_os == 'Windows': + return ('\\') + else: + return ('/') + def handle_rar(filein): - tmp_dir = "/tmp/c2p/" + tmp_dir = tmp_directory + separator() + "c2p" + separator() os.mkdir(tmp_dir) print("Extracting pictures in the CBR file...") patoolib.extract_archive(filein, outdir=tmp_dir) newfile = filein.replace(filein[-4:], ".pdf") print("Creating the PDF file...") to_pdf(newfile, tmp_dir, 7) - try: - clean_tmp_dir(tmp_dir) - except: - print("Error while deleting 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') - tmp_dir = "/tmp/c2p/" print("Extracting pictures in the CBZ file...") zip_ref.extractall(tmp_dir) zip_ref.close() newfile = filein.replace(filein[-4:], ".pdf") print("Creating the PDF file...") to_pdf(newfile, tmp_dir, 0) - try: - clean_tmp_dir(tmp_dir) - except: - print("Error while deleting 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] + "/", ii) + to_pdf(filename, newdir + ffiles[0] + separator(), ii) else: im_list = list() firstP = True @@ -58,7 +65,8 @@ def to_pdf(filename, newdir, ii): 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")): + 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)) @@ -73,19 +81,8 @@ def to_pdf(filename, newdir, ii): else: continue print("Saving the PDF file...") - im.save(filename, "PDF", resolution=100.0, - save_all=True, append_images=im_list) - clean_tmp_dir(newdir) - - -def clean_tmp_dir(dir): - try: - files = os.listdir(dir) - for file in files: - os.remove(dir + "/" + file) - os.rmdir(dir) - except: - print("No dir to clean!") + im.save(filename, "PDF", resolution=100.0, save_all=True, append_images=im_list) + shutil.rmtree(newdir, ignore_errors=True) def launch_convert(file): @@ -97,7 +94,7 @@ def launch_convert(file): def opendir(directory): for file in sorted(os.listdir(directory)): - launch_convert(directory + '/' + file) + launch_convert(directory + separator() + file) if False: print("WARNING: some items were skipped") diff --git a/comics2pdf_gui.py b/comics2pdf_gui.py index 793c89d..a164486 100755 --- a/comics2pdf_gui.py +++ b/comics2pdf_gui.py @@ -15,6 +15,8 @@ from PIL import Image import PIL.ExifTags import shutil +import tempfile +import platform class Explorer(QtWidgets.QWidget): @@ -64,6 +66,14 @@ def __init__(self): self.global_process = 0 self.nb_files = 0 self.done = 0 + self.tmp_directory = tempfile.gettempdir() + self.current_os = platform.system() + + def separator(self): + if self.current_os == 'Windows': + return ('\\') + else: + return ('/') def on_button_clicked(self): explorer = Explorer(self.file.isChecked()) @@ -166,7 +176,7 @@ def launch_convert(self, file): self.handle_rar(file) def handle_rar(self, filein): - tmp_dir = "/tmp/c2p/" + tmp_dir = self.tmp_directory + self.separator() + "c2p" + self.separator() os.mkdir(tmp_dir) patoolib.extract_archive(filein, outdir=tmp_dir) newfile = filein.replace(filein[-4:], ".pdf") @@ -174,8 +184,8 @@ def handle_rar(self, filein): shutil.rmtree(tmp_dir, ignore_errors=True) def handle_zip(self, filein): + tmp_dir = self.tmp_directory + self.separator() + "c2p" + self.separator() zip_ref = zipfile.ZipFile(filein, 'r') - tmp_dir = "/tmp/c2p/" zip_ref.extractall(tmp_dir) zip_ref.close() newfile = filein.replace(filein[-4:], ".pdf") @@ -184,10 +194,10 @@ def handle_zip(self, filein): def to_pdf(self, filename, newdir, ii): self.indication.setText("Exctracting images...") - self.ratio.setText(str(self.done) + '/' + str(self.nb_files)) + self.ratio.setText(str(self.done) + self.separator() + str(self.nb_files)) ffiles = os.listdir(newdir) if (len(ffiles) == 1): - self.to_pdf(filename, newdir + ffiles[0] + "/", ii) + self.to_pdf(filename, newdir + ffiles[0] + self.separator(), ii) else: im_list = list() firstP = True @@ -199,7 +209,8 @@ def to_pdf(self, filename, newdir, ii): index += 1 local_process = index / list_len * 100 // self.nb_files self.conversionProgress.setProperty("value", "{0:.0f}".format(local_process + self.global_process)) - if (image.endswith(".jpg") or image.endswith(".JPG") or image.endswith(".jpeg") or image.endswith(".JPEG")): + if (image.endswith(".jpg") or image.endswith(".JPG") + or image.endswith(".jpeg") or image.endswith(".JPEG")): if local_process * self.nb_files > 95: if increased == False: self.done += 1