Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turned into a command-line tool #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 19 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
# comics2pdf
A script to convert comic files (cbr, cbz) to pdf, in python3. Works in Windows. Linux not tested.
<h4>
A command-line tool to convert comic files (cbr, cbz) to pdf, in python3. Works in Windows. Linux not tested.
</h4>

## Getting Started
# install
<h4>
download the repo and run the command:
<br><code>python setup.py develop</code>
</h4>

To use it, just place the .py script in the same directory the file(s) to convert are in and run the command:
# Usage
<h4>
go the directory of the file(s) and run the command:
<br><code>c2p</code>

```
python comic2pdf.py
```

### Prerequisites

Script in Python 3.6 (probably won't work with Python 2.x versions). Requires the "zipfile", "patool" and "pillow"(aka PIL) modules in order to work correctly. To install them run the following commands:

```
pip install zipfile
```
```
pip install patool
```
```
pip install pillow
```
In windows you'll need to run cmd.exe using the "run as administrator" option for those commands to work.

### Installing

Just place all the .cbr/.cbz files desired to convert to pdf in one directory, place the script in that same directory and run the following command:

```
python comic2pdf.py
```

It's a good idea to rename (before running the script) the .cbr/.cbz files that have names like "01.cbz" to "Comic Name 01.cbz", as the output .pdf will get its name from the input comic file.
It's a good idea to rename (before running the script) the .cbr/.cbz files that have
names like "01.cbz" to "Comic Name 01.cbz", as the output .pdf will get its name from
the input comic file.

</h4>
## Built With

* [Python 3]

## Authors and Acknowledgments

<h4>
* **MComas1**

* **theunderdog**
Based on a python script by **bransorem** (https://github.com/bransorem/comic2pdf).
</h4>
105 changes: 0 additions & 105 deletions comic2pdf.py

This file was deleted.

117 changes: 117 additions & 0 deletions comics2pdf/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Converts .cbr and .cbz files to .pdf
#
# Use: python comic2pdf.py
# -- Only works with comicbook files that contain JPG's (for now).
# -- The script should be in the same directory the file(s) to convert are in.
#
# Author: MComas1
# Date: 14-09-18
# License: You can do what you want with it.
# Mainly based on a script by Bransorem (https://github.com/bransorem/comic2pdf)
# Turned to a command-line tool by theunderdog (https://github.com/theunderdog)
# import os, sys, zipfile, patoolib
# from PIL import Image
# import PIL.ExifTags
def main_func():
try:
import os, sys, zipfile, patoolib
from PIL import Image
import PIL.ExifTags
except Exception as ex:
print('installing dependencies...')
os.system('pip install pillow')
main_func()
quit()
class main_class():
def __init__(self):

self.failed = False

def nlog_info (self,msg, out=open("comic2pdf_log.txt","a")):
"""Print info message to stdout (or any other given output)."""
print("patool:", msg, file=out)

def olog_info (self,msg, out=sys.stdout):
"""Print info message to stdout (or any other given output)."""
print("patool:", msg, file=out)

def handlerar2(self,filein):
tmp_dir = os.getcwd()+"\\Teemp\\"
os.mkdir(tmp_dir)
original = sys.stdout
sys.stdout = open("comic2pdf_log.txt","a")
patoolib.util.log_info = self.nlog_info
patoolib.extract_archive(filein, outdir=tmp_dir)
newfile = filein.replace(filein[-4:],".pdf")
self.toPDF2(newfile,tmp_dir,7)
self.cleanDir(tmp_dir)
print("------------------------------------------------------------")

sys.stdout = original
print("\""+newfile[:-4]+"\" successfully converted!")

def handlezip(self,filein):
zip_ref = zipfile.ZipFile(filein, 'r')
tmp_dir = os.getcwd()+"\\Teemp\\"
zip_ref.extractall(tmp_dir)
zip_ref.close()
newfile = filein.replace(filein[-4:],".pdf")
self.toPDF2(newfile,tmp_dir,0)
try:
self.cleanDir(tmp_dir)
except:
aaa = 223
print("\""+newfile[:-4]+"\" successfully converted!")

def toPDF2(self,filename, newdir,ii):
ffiles = os.listdir(newdir)
if (len(ffiles) == 1):
self.toPDF2(filename,newdir+ffiles[0]+"\\",ii)
else:
# imagelist is the list with all image filenames
im_list = list()
firstP = True
im = None
for image in ffiles:
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:
aaaaa = 4

if (firstP):
im = im1
firstP = False
else: im_list.append(im1)
else: continue
#print(exif)
im.save(filename, "PDF" ,resolution=100.0, save_all=True, append_images=im_list)
self.cleanDir(newdir)
#print("OK")

def cleanDir(self,dir):
try:
files = os.listdir(dir)
for file in files:
os.remove(dir+"\\"+file)
os.rmdir(dir)
except: print("No dir to clean!")

def opendir(self,directory):
# look at all files in directory
#print(os.listdir(directory))
for file in os.listdir(directory):
# file extension cbr only
if (file[-4:] == '.cbz' or file[-4:] == '.zip'):
# change to zip
self.handlezip(file)
elif (file[-4:] == '.cbr' or file[-4:] == '.rar'):
# change to rar
self.handlerar2(file)
if self.failed:
print ("WARNING: some items were skipped")


obj = main_class()
obj.opendir(os.getcwd())
3 changes: 3 additions & 0 deletions comics2pdf/command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def main():
from comics2pdf import main_func
main_func()
15 changes: 15 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import setuptools

setuptools.setup(
name="comics2pdf",
install_requires = setuptools.find_packages(),
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
]
,
entry_points = {
'console_scripts':['c2p=comics2pdf.command:main']
}
)