Skip to content

Commit

Permalink
[ADDED] windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulMonnery committed Oct 16, 2019
1 parent d063862 commit 361b47d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
venv/
data/
target/
src/
.vscode
Expand Down
49 changes: 23 additions & 26 deletions comics2pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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):
Expand All @@ -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")

Expand Down
21 changes: 16 additions & 5 deletions comics2pdf_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from PIL import Image
import PIL.ExifTags
import shutil
import tempfile
import platform

class Explorer(QtWidgets.QWidget):

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -166,16 +176,16 @@ 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")
self.to_pdf(newfile, tmp_dir, 7)
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")
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 361b47d

Please sign in to comment.