Last active
August 1, 2017 03:38
-
-
Save NorimasaNabeta/7b567992dcd6270276f2190fadf65a07 to your computer and use it in GitHub Desktop.
Pdf book from the image files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: python; coding: utf-8 -*- | |
# | |
# | |
import os | |
from optparse import OptionParser | |
from matplotlib.backends.backend_pdf import PdfPages | |
from scipy.misc import imread | |
import matplotlib.pyplot as plt | |
import numpy as np | |
## | |
# | |
# | |
def plotImage(file, grayscale=True): | |
if grayscale : | |
from skimage import io | |
im = io.imread(file, as_grey=True) | |
plt.imshow(im, cmap=plt.get_cmap("gray")) | |
else: | |
im = imread(file) | |
plt.imshow(im) | |
# no-grid, no-scales and no bbox | |
a = plt.gca() | |
a.get_xaxis().set_visible(False) | |
a.get_yaxis().set_visible(False) | |
for spines in a.spines.values(): | |
spines.set_visible(False) | |
## | |
# | |
# | |
def fileSignature(filePath): | |
magic = { | |
'SWF': [ '43', '57', '53' ], | |
'ID3': [ '49', '44', '33' ], | |
'MP3': [ 'FF', 'FB' ], | |
'JPG': [ 'FF', 'D8' ], | |
'PNG': [ '89', '50', '4E', '47' ], | |
'BMP': [ '42', '4D' ], | |
'GIF': [ '47', '49', '46', '38' ], | |
'GZIP': [ '1F', '8B' ] | |
} | |
def buffer2hexlist(buffer, start, count): | |
result = [] | |
for item in range(count): | |
result.append('%02X' % ord(buffer[start + item])) | |
return result | |
# | |
# | |
with open(filePath, "rb") as fd: | |
data = fd.read(8) | |
fd.close() | |
blist = buffer2hexlist(data, 0, 8) | |
for ftype, signature in magic.iteritems(): | |
flag = True | |
for id, val in zip(signature, blist): | |
if id != val: | |
flag = False | |
break | |
if flag: | |
return ftype | |
return 'UNKNOWN' | |
## | |
# glab files | |
# | |
def filePaths( directory ): | |
file_paths = []; | |
for root, directories, files in os.walk(directory): | |
for filename in files: | |
filepath = os.path.join(root, filename) | |
sig = fileSignature(filepath) | |
if sig in ['JPG', 'PNG', 'GIF']: | |
file_paths.append(filepath) | |
return file_paths | |
# | |
# | |
# | |
if __name__ == '__main__': | |
parser = OptionParser() | |
parser.add_option("-o", "--output", dest="filename", default="pages.pdf", | |
help="write report to FILE", metavar="FILE") | |
parser.add_option("-g", "--grayscale", | |
action="store_false", dest="gray", default=True, | |
help="don't print status messages to stdout") | |
parser.add_option("-q", "--quiet", | |
action="store_false", dest="verbose", default=True, | |
help="don't print status messages to stdout") | |
(options, args) = parser.parse_args() | |
files = filePaths( args[0] ) | |
pp = PdfPages( options.filename ) | |
for idx in range(len(files)): | |
if options.verbose: | |
print idx, files[idx] | |
plt.subplots() | |
plotImage(files[idx], options.gray) | |
f = plt.gcf() | |
# A4-portlate | |
f.set_size_inches(8.27, 11.69) | |
pp.savefig(f, bbox_inches='tight') | |
plt.close() | |
pp.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add the grayscale mode.