From 28f75725628d45c11e23f128c9bfea441e8cae82 Mon Sep 17 00:00:00 2001 From: Castro0o Date: Fri, 10 Nov 2017 20:15:32 +0100 Subject: [PATCH] imposition and wiki-download in da house --- README | 30 +++++++++++++++- imgs2pdf.sh | 2 +- imposition.py | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ wiki-download.py | 83 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 206 insertions(+), 2 deletions(-) create mode 100755 imposition.py create mode 100755 wiki-download.py diff --git a/README b/README index 27a4d14..015f490 100644 --- a/README +++ b/README @@ -2,9 +2,37 @@ ## Software dependencies: -pdftk, imagemagick +Packages: pdftk, imagemagick +Install in Debian/Ubuntu: apt-get install pdftk imagemagick + +Install in Mac with brew (http://brew.sh/): brew install pdftk imagemagick + +Python libraries: mwclient + +Install in Debian/Ubuntu w/ pip: sudo pip install mwclient + ## Scripts + +### wiki-download.py +Dowloads media files from a wiki, according to a sematic ask query. + +Usage: +python wiki-download.py --help + +python wiki-download.py -d imgs -a [[Modification_date::+]] + + +### imposition.py +Converts 8 images (jpg or png) from a given directory, into a single sheet 4x2 imposition. + +Usage: +python wiki-download.py --help + +python imposition.py --dir imgs/ --size a3 --order random + + + ### imgs2pdf.sh Converts a directory of images onto a single PDF diff --git a/imgs2pdf.sh b/imgs2pdf.sh index 2d23e31..455e2ed 100755 --- a/imgs2pdf.sh +++ b/imgs2pdf.sh @@ -33,7 +33,7 @@ then echo "processing..." for IMG in $DIR* # convert imgs to tmp pdfs do PDF=${IMG%.*}.pdf - convert -density 200 $IMG $PDF +# convert -density 200 $IMG $PDF done pdftk $DIR*.pdf cat output $FILENAME # concatonate single pdf into 1 pdf diff --git a/imposition.py b/imposition.py new file mode 100755 index 0000000..ec435de --- /dev/null +++ b/imposition.py @@ -0,0 +1,93 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +import os, tempfile, shlex, subprocess +from argparse import ArgumentParser +from random import shuffle + +#### +# Arguments +# TODO: add example to description +#### +p = ArgumentParser(description='Script converts 8 images (jpg or png) from a given directory into a single page 4x2 imposition. Example: python imposition.py --dir imgs/ --size a3 --order random ') +p.add_argument("--dir", metavar='', default='', required=True, help="Image directory, which stores the source images for the imposition") +p.add_argument("--size", metavar='', default="A4", choices=['A4','a4','A3','a3'], help="Size of the printing sheet: A4 or A3. Default:A4") +p.add_argument("--order", metavar='', default="alphabet", choices=["alphabet", "reverse", "random"], help="Image distribution Order in the imposition. Possible Values: alphabet (alphabetically) , reverse (reverse alphabetically), random. Default: alphabet") +p.add_argument("--output", metavar='' ,default="output.pdf", help="Output file. Can either be a pdf, jpg or png. Default: output.pdf") +args = p.parse_args() + +temp_dir = tempfile.mkdtemp() + +# dimensions +args.size = (args.size).upper() +global size_sheet +if args.size == 'A4': + size_sheet = (3508, 2480) #landscape: widthxheight in px +elif args.size == 'A3': + size_sheet = (4960, 3508) #landscape: widthxheight in px +size_pages = (size_sheet[0]/4,size_sheet[1]/2 )# size of each of the pages which makes up the printing sheet + +# image selection +listdir = os.listdir( os.path.abspath(args.dir)) +listdir = [f for f in listdir if any (x in f for x in ['.jpeg','.jpg','.png','.JPEG','.JPG','.PNG', '.Jpg', '.Jpeg','.Png']) ] #only bitmaps allowed + +# create list of 8 ordered images +global listdir_order +if args.order == 'alphabet': + listdir.sort() + listdir_order = listdir +elif args.order == 'reverse': + listdir.sort() + listdir.reverse() + listdir_order = listdir +elif args.order == 'random': + shuffle(listdir) + listdir_order = listdir + +print listdir_order +listdir_order = listdir_order[0:8] +pagenumbers = [2,5,4,3,1,6,7,0] # from from top left to bottom right ( order in imagemagick creates mosaic) +listdir_order = [listdir_order[n] for n in pagenumbers] # reorder according to pagenumbers +print 'selected files according to pagenumbers:', listdir_order + +# image conversion: -extent +tmpfiles = [ (temp_dir +'/'+ str(n)+'.jpg') for n, img in enumerate(listdir_order) ] +for n, img in enumerate(listdir_order): + template_convert = 'convert -background white "{targetimg}" -gravity Center -resize {w}x{h} -extent {w}x{h} {tmpfile}' + cmd_convert = template_convert.format(targetimg=(os.path.abspath(args.dir))+'/'+img, + w=size_pages[0], + h=size_pages[1], + tmpfile=tmpfiles[n]) + cmd_convert_l = shlex.split(cmd_convert) + subprocess.call(cmd_convert_l) + +# montage +## order & rotation +tmpfiles_top = tmpfiles[0:4] # top row will be rotated in the mosaic +tmpfiles_top = ' '.join(tmpfiles_top) +tmpfiles_bottom = tmpfiles[4:8] +tmpfiles_bottom = ' '.join(tmpfiles_bottom) +tmp_output = (temp_dir +'/'+'output.jpg') +# command +template_montage = "montage -geometry +1+1 -tile 4x2 \( -rotate 180 {topfiles} \) {bottomfiles} {output}" +cmd_montage = template_montage.format(topfiles=tmpfiles_top, + bottomfiles=tmpfiles_bottom, + output = tmp_output) +cmd_montage_l = shlex.split(cmd_montage) +subprocess.call(cmd_montage_l) + +# resize output +output_f = os.path.abspath('./'+args.output) +template_convert = "convert {input_f} -resize {w}x{h} -units PixelsPerInch -density 300x300 {output}" +cmd_convert = template_convert.format( + input_f= tmp_output, + w=size_sheet[0], + h=size_sheet[1], + output=output_f) +cmd_convert_l = shlex.split(cmd_convert) +subprocess.call(cmd_convert_l) + +print '** imposition save in', output_f, '**' + +# garbage collection +cmd_rm = shlex.split( ('rm -rf '+ temp_dir) ) +subprocess.call(cmd_rm ) # rm temp dir diff --git a/wiki-download.py b/wiki-download.py new file mode 100755 index 0000000..41d7fe4 --- /dev/null +++ b/wiki-download.py @@ -0,0 +1,83 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +from mwclient import Site, image +from argparse import RawTextHelpFormatter +from argparse import ArgumentParser +import pprint, os, urllib2 + +#### +# Arguments +# todo add search query +#### +p = ArgumentParser(description=""" +# Description: Script dowloads media files from a wiki, according to a sematic ask query. + +# Examples: +Downloads files with property Modification_date: +python wiki-download.py -d imgs -a [[Modification_date::+]] + +Downloads files semantically tagged as belonging to Year 2007 and having Document_Type Poster: +python wiki-download.py -d imgs -a [[Year::2007]][[Document_Type::Poster]]""", formatter_class=RawTextHelpFormatter) + +p.add_argument("--host", metavar='', default="aa.xpub.nl") +p.add_argument("--path", metavar='', default="/", help="Wiki path. Should end with /") +p.add_argument("--ask", "-a", metavar='', default="", help="Ask query to be sent to the wiki API.") +p.add_argument("--download", "-d", metavar='', default='', help="Local directory to store files from wiki. If no directory provided files wont be downloaded") +p.add_argument("--verbose", "-v", action='store_true', help="Increase verbosity. If not given no files will be downloaded") + +args = p.parse_args() +print args + +######### +# defs +######### +def mwsite(host, path): + site = Site(('http',host), path) + return site + + +site = mwsite(args.host, args.path) +print site + + + +query= ('[[File:+]]'+ args.ask) + +# examples: +# [[File:+]][[Year::+]][[Document_Type::Poster]] +# [[File:+]][[Year::2007]][[Document_Type::Poster]] +# same as: api.php?action=ask&query=[[File:%2B]][[Year::2007]][[Document_Type::Poster]] +# can write compound query such as Year::2000 Medium::Flyer + + +for answer in site.ask(query): + + print answer + img = image.Image(site, answer) + img_name = answer.title() + img_text = img.text() + img_info = img.imageinfo # dict includes url and extensive metadata + # FILE DOWNLOAD + if 'url' in img_info.keys() and len(args.download) > 0: + img_url = (img_info['url']).replace('https','http') + + if os.path.exists(args.download) is False: + os.makedirs(args.download) + + img_data = urllib2.urlopen( img_url ) + img_file = open('{}/{}'.format(args.download, img_name), 'wb') + img_file.write(img_data.read()) + img_file.close() + print 'Saved to {}'.format(args.download + '/' + img_name) + if args.verbose: + print 'File text:',img_text + pprint.pprint( img_info ) + + print + + + + + +