-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgs2pdf.py
executable file
·103 lines (91 loc) · 3.24 KB
/
imgs2pdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
# -*- coding: UTF8 -*-
#
# Copyright 2009, 2013 Raul Soriano <GatoLoko@gmail.com>
#
# Imgs2PDF is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Imgs2PDF is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
"""
Usage: imgs2pdf [OPTIONS...]
-d, --debug Shows debug information.
-h, --help Shows this help.
-o NAME, --output=NAME Sets the name of the generated PDF.
-t TITLE, --title=TITLE Sets the PDF title.
-v, --version Shows imgs2pdf version.
"""
import os
from sys import argv, exit
import getopt
from PIL import Image
from reportlab.pdfgen import canvas
__VERSION__ = "3.0"
EXTENSIONES = [".jpg", ".gif", ".png", ".jpeg", ".JPG", ".GIF", ".PNG", ".JPEG"]
TITULO = os.path.split(os.getcwd())[-1]
SALIDA = "".join([TITULO, ".pdf"])
DEBUG = 0
def listaimagenes():
"""Return a list of image files from the working directory."""
imagenes = []
for archivo in os.listdir(os.getcwd()):
if os.path.isfile(archivo):
if os.path.splitext(archivo)[-1] in EXTENSIONES:
imagenes.append(archivo)
return sorted(imagenes)
def main():
"""Script main function."""
global TITULO, SALIDA, DEBUG
try:
opcs, args = getopt.getopt(argv[1:], "cdho:t:v",
["debug", "help", "output=", "title=",
"version"])
except getopt.GetoptError:
print(__doc__)
exit(2)
for opc, arg in opcs:
if opc in ("-d", "--debug"):
DEBUG = 1
import sys
import gc
elif opc in ("-h", "--help"):
print(__doc__)
exit(1)
elif opc in ("-o", "--output"):
SALIDA = arg
elif opc in ("-t", "--title"):
TITULO = arg
elif opc in ("-v", "--version"):
print("imgs2pdf %s" % __VERSION__)
exit(1)
imagenes = listaimagenes()
pdf = canvas.Canvas(SALIDA)
pdf.setTitle(TITULO)
for imagen in imagenes:
print("Proccesing %s" % imagen)
# Open the image file
imagefile = Image.open(imagen)
# Resize each page to fit the image size
print(" Resizing page to %s width and %s height" % imagefile.size)
pdf.setPageSize(imagefile.size)
# Draw the image in the current page
pdf.drawImage(canvas.ImageReader(imagen), 0, 0,
preserveAspectRatio=True)
if DEBUG == 1:
print(sum([sys.getsizeof(o) for o in gc.get_objects()]))
# Close the current page and create a new one
pdf.showPage()
# Cerramos el PDF
pdf.save()
if __name__ == "__main__":
main()