Unn-

mkadvice-0.1

Mar 18th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from argparse import ArgumentParser
  5. from collections import Counter
  6. from cStringIO import StringIO
  7.  
  8. from PIL import Image, ImageDraw, ImageFont
  9. from requests import get as req_get
  10.  
  11. DEFAULT_OUT = 'output.jpg'
  12. FONT = '/usr/share/fonts/ubuntu-font-family/Ubuntu-C.ttf'
  13. WIDTH = 214  # Poster width
  14. HIGHT = 317  # Poster hight
  15.  
  16. class Movie:
  17.     def __init__(self, imdb_id, title, genre, poster=None):
  18.         self.imdb_id = imdb_id
  19.         self.title = title
  20.         self.genre = genre
  21.         self.poster = poster
  22.    
  23.  
  24. def get_poster_url(imdb_id):
  25.     """Returns string of poster URL if exists or None otherwise.
  26.    Takes as parameter string of IMDB's id (e.g., 'tt1234567').
  27.    """
  28.  
  29.     payload = {'id': imdb_id, 'plot': 'none'}
  30.     json = req_get('http://imdbapi.org/', params=payload).json()
  31.     return json.get('poster')
  32.  
  33.  
  34. def make_caption(title):
  35.     """Returns image with caption."""
  36.  
  37.     fake_poster = Image.new('RGB', (WIDTH, HIGHT), 'lightgrey')
  38.     caption = ImageDraw.Draw(fake_poster)
  39.     caption_font = ImageFont.truetype(FONT, 24)
  40.     caption.text((5, 10), title, fill='black', font=caption_font)
  41.     return fake_poster
  42.  
  43.  
  44. def main(input_file, output_file):
  45.     # Read movies from file
  46.     movies = [Movie(*(i.strip().capitalize() for i in line.split(',', 2)))
  47.               for line in open(input_file, 'r')]
  48.  
  49.     # Count columns (genres) and max number of posters in column
  50.     genres = Counter(mov.genre for mov in movies)
  51.     unique_genres = genres.keys()
  52.     unique_genres.sort()
  53.     max_posters = max(genres.values())
  54.  
  55.     # Download posters
  56.     for mov in movies:
  57.         poster_url = get_poster_url(mov.imdb_id)
  58.         if poster_url:
  59.             mov.poster = req_get(poster_url).content
  60.  
  61.     # Create canvas
  62.     canv_width = 20 + (WIDTH+20) * len(unique_genres)
  63.     canv_hight = 100 + HIGHT*max_posters + 20
  64.     canvas = Image.new('RGB', (canv_width, canv_hight), 'black')
  65.     draw = ImageDraw.Draw(canvas)
  66.    
  67.     # Add genres to canvas
  68.     genre_font = ImageFont.truetype(FONT, 42)
  69.     shift = 0
  70.     for genre in unique_genres:
  71.         draw.text((20 + shift, 20), genre, fill='white', font=genre_font)
  72.         shift += WIDTH + 20
  73.  
  74.     # Add posters to canvas
  75.     p_counter = Counter()  # Current number of posters in each column (genre)
  76.     y_counter = Counter()  # Current y value in each column
  77.     x, y = 0, 0
  78.     for mov in movies:
  79.         if mov.poster:
  80.             poster = Image.open(StringIO(mov.poster))
  81.             if poster.size != (WIDTH, HIGHT):
  82.                 poster = poster.resize((WIDTH, HIGHT))
  83.         else:
  84.             poster = make_caption(mov.title)
  85.  
  86.         # Set the (x, y) in appliance with genre
  87.         for genre in unique_genres:
  88.             if genre == mov.genre:
  89.                 x = 20 + (WIDTH+20) * unique_genres.index(genre)
  90.                 y = 100 + y_counter[genre]
  91.                 p_counter[genre] += 1
  92.                 y_counter[genre] = HIGHT * p_counter[genre]
  93.  
  94.         canvas.paste(poster, (x, y))
  95.     canvas.save(output_file)
  96.    
  97.  
  98. if __name__ == '__main__':
  99.     parser = ArgumentParser(description='Downloads posters and makes "advice"')
  100.     parser.add_argument('input_file',
  101.                         metavar='FILE',
  102.                         help='Text file containing movies (id, title, genre)')
  103.     parser.add_argument('-o', '--out',
  104.                         dest='output_file',
  105.                         metavar='OUTPUT',
  106.                         default=DEFAULT_OUT,
  107.                         help='Name of output image (default: '+DEFAULT_OUT+')')
  108.     args = parser.parse_args()
  109.     main(args.input_file, args.output_file)
Advertisement
Add Comment
Please, Sign In to add comment