Back to my blog Back to my projects

Scripts

Changeset 8255535


Ignore:
Timestamp:
06/28/10 19:28:01 (3 years ago)
Author:
Aurélien Bompard <aurelien@…>
Branches:
master
Children:
a5c46b2
Parents:
8491ac9
git-author:
Aurélien Bompard <aurelien@…> (06/28/10 19:28:01)
git-committer:
Aurélien Bompard <aurelien@…> (06/28/10 19:28:01)
Message:

make-songs-list: always resize covers to save space

File:
1 edited

Legend:

Unmodified
Added
Removed
  • make-songs-list.py

    r6fdcac4 r8255535  
    1818import optparse 
    1919import atexit 
     20import re 
    2021from pprint import pprint 
     22 
     23import PIL 
    2124 
    2225from reportlab.platypus import BaseDocTemplate, Paragraph, Spacer, Image, Table, TableStyle, Frame, PageTemplate 
     
    2730 
    2831 
    29  
    3032defaultPageSize = (defaultPageSize[1], defaultPageSize[0]) 
    3133PAGE_HEIGHT=defaultPageSize[1] 
    3234PAGE_WIDTH=defaultPageSize[0] 
    33 MARGIN = 8 
     35MARGIN = 10 
    3436COLSEP = 5 
    3537 
    3638songs = [] 
    3739 
    38 def get_songs(): 
     40def get_songs(cover_size, dpi): 
    3941    songs_dirs = ["/usr/share/performous/songs", 
    4042                  "/usr/local/share/games/ultrastar/songs", 
     
    4547                  "~/.fretsonfire/songs", 
    4648                 ] 
     49    available_songs = [] 
    4750    for songs_dir in songs_dirs: 
    4851        songs_dir = os.path.expanduser(songs_dir) 
     
    5356                if not cur_file.lower().endswith(".txt"): 
    5457                    continue 
    55                 filepath = os.path.join(root, cur_file) 
    56                 song = process_file(filepath) 
    57                 if song: 
    58                     songs.append(song) 
     58                available_songs.append(os.path.join(root, cur_file)) 
     59    for index, available_song in enumerate(available_songs): 
     60        song = process_file(available_song, cover_size, dpi) 
     61        if song: 
     62            songs.append(song) 
     63        sys.stdout.write("\rAdding song %d/%d" 
     64                         % (index+1, len(available_songs))) 
     65        sys.stdout.flush() 
     66    print 
     67    print "Building PDF..." 
    5968    songs.sort(cmp=lambda x,y: cmp(x["artist"],y["artist"])) 
    6069 
    61 def process_file(filepath): 
     70def process_file(filepath, cover_size, dpi): 
    6271    song = {} 
    6372    file_h = open(filepath, "r") 
     
    7281        if tag not in song: 
    7382            continue 
    74         song[tag] = os.path.realpath(os.path.join( 
     83        song["image"] = os.path.realpath(os.path.join( 
    7584                        os.path.dirname(filepath), song[tag])) 
    76         if song[tag].count("  "): 
    77             tmpimg, tmpimg_path = tempfile.mkstemp( 
    78                                     prefix="mksonglist-", suffix=".jpg") 
    79             print >>sys.stderr, "Warning on image '%s': " % song[tag] \ 
    80                  +"the filename contains two consecutive spaces, " \ 
    81                  +"ReportLab does not like that. Using a workaround." 
    82             origimg = open(song[tag]) 
    83             os.write(tmpimg, origimg.read()) 
    84             origimg.close() 
    85             atexit.register(os.remove, tmpimg_path) 
    86             song[tag] = tmpimg_path 
    87         if not os.path.exists(song[tag]): 
    88             del song[tag] 
     85        if not os.path.exists(song["image"]): 
     86            del song["image"] 
     87            continue 
     88        # Resize the image 
     89        tmpimg, tmpimg_path = tempfile.mkstemp( 
     90                                prefix="mksonglist-", suffix=".jpg") 
     91        img = PIL.Image.open(song["image"]) 
     92        img.thumbnail((cover_size * dpi, cover_size * dpi), PIL.Image.ANTIALIAS) 
     93        img.save(os.fdopen(tmpimg, "w"), "JPEG") 
     94        atexit.register(os.remove, tmpimg_path) 
     95        song["image"] = tmpimg_path 
     96        break 
    8997    return song 
    9098 
    9199def filter_songs(karaoke): 
     100    filter_re = re.compile("(.*)\s+\(.*\)") 
    92101    for song in songs[:]: 
    93         if not karaoke and song["title"].count("(Karaoke)"): 
     102        title = song["title"] 
     103        if not karaoke and title.count("(Karaoke)"): 
    94104            songs.remove(song) 
     105            continue 
     106        filter_match = filter_re.match(title) 
     107        if filter_match: 
     108            short_title = filter_match.group(1) 
     109            if short_title in [ s["title"] for s in songs ]: 
     110                songs.remove(song) 
     111                continue 
    95112 
    96113def make_pdf(output, title, cover_size): 
     
    128145    table_contents = [] 
    129146    for song in songs: 
    130         image = None 
    131         label = Paragraph("%s - %s" % (song["artist"], song["title"]), text_style) 
    132         if "cover" in song and os.path.exists(song["cover"]): 
    133             image = song["cover"] 
    134         elif "background" in song and os.path.exists(song["background"]): 
    135             image = song["background"] 
    136         if image: 
    137             image = Image(image, cover_size, cover_size) 
    138             #image.drawHeight = cover_size *I.drawHeight / I.drawWidth 
    139             #image.drawWidth = cover_size 
    140         else: 
    141             image = Spacer(1, cover_size) 
    142         row = [image, label] 
    143         table_contents.append(row) 
     147        try: 
     148            image = None 
     149            label = Paragraph("%s - %s" % (song["artist"], song["title"]), text_style) 
     150            if "image" in song: 
     151                image = Image(song["image"], cover_size, cover_size) 
     152                #image.drawHeight = cover_size *I.drawHeight / I.drawWidth 
     153                #image.drawWidth = cover_size 
     154            else: 
     155                image = Spacer(1, cover_size) 
     156            row = [image, label] 
     157            table_contents.append(row) 
     158        except Exception: 
     159            print >>sys.stderr, "Error when printing %s" % repr(song) 
     160            raise 
    144161    Story.append(Table(table_contents, 
    145162                       colWidths=(cover_size + 2, col_width - (cover_size + 2)), 
     
    158175    parser.add_option("-c", "--cover-size", dest="cover_size", default=32, 
    159176                      type="int", help="Cover size (default: %default pt)") 
     177    parser.add_option("--dpi", dest="dpi", default=96, 
     178                  help="Image resolution in Dots Per Inch (default: %default)") 
    160179    opts, args = parser.parse_args() 
    161180    if len(args) != 0: 
     
    165184if __name__ == '__main__': 
    166185    opts = parse_opts() 
    167     get_songs() 
     186    get_songs(opts.cover_size, opts.dpi) 
    168187    #pprint(songs) 
    169188    filter_songs(opts.karaoke) 
Note: See TracChangeset for help on using the changeset viewer.