Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from argparse import ArgumentParser
- from collections import Counter
- from cStringIO import StringIO
- from PIL import Image, ImageDraw, ImageFont
- from requests import get as req_get
- DEFAULT_OUT = 'output.jpg'
- FONT = '/usr/share/fonts/ubuntu-font-family/Ubuntu-C.ttf'
- WIDTH = 214 # Poster width
- HIGHT = 317 # Poster hight
- class Movie:
- def __init__(self, imdb_id, title, genre, poster=None):
- self.imdb_id = imdb_id
- self.title = title
- self.genre = genre
- self.poster = poster
- def get_poster_url(imdb_id):
- """Returns string of poster URL if exists or None otherwise.
- Takes as parameter string of IMDB's id (e.g., 'tt1234567').
- """
- payload = {'id': imdb_id, 'plot': 'none'}
- json = req_get('http://imdbapi.org/', params=payload).json()
- return json.get('poster')
- def make_caption(title):
- """Returns image with caption."""
- fake_poster = Image.new('RGB', (WIDTH, HIGHT), 'lightgrey')
- caption = ImageDraw.Draw(fake_poster)
- caption_font = ImageFont.truetype(FONT, 24)
- caption.text((5, 10), title, fill='black', font=caption_font)
- return fake_poster
- def main(input_file, output_file):
- # Read movies from file
- movies = [Movie(*(i.strip().capitalize() for i in line.split(',', 2)))
- for line in open(input_file, 'r')]
- # Count columns (genres) and max number of posters in column
- genres = Counter(mov.genre for mov in movies)
- unique_genres = genres.keys()
- unique_genres.sort()
- max_posters = max(genres.values())
- # Download posters
- for mov in movies:
- poster_url = get_poster_url(mov.imdb_id)
- if poster_url:
- mov.poster = req_get(poster_url).content
- # Create canvas
- canv_width = 20 + (WIDTH+20) * len(unique_genres)
- canv_hight = 100 + HIGHT*max_posters + 20
- canvas = Image.new('RGB', (canv_width, canv_hight), 'black')
- draw = ImageDraw.Draw(canvas)
- # Add genres to canvas
- genre_font = ImageFont.truetype(FONT, 42)
- shift = 0
- for genre in unique_genres:
- draw.text((20 + shift, 20), genre, fill='white', font=genre_font)
- shift += WIDTH + 20
- # Add posters to canvas
- p_counter = Counter() # Current number of posters in each column (genre)
- y_counter = Counter() # Current y value in each column
- x, y = 0, 0
- for mov in movies:
- if mov.poster:
- poster = Image.open(StringIO(mov.poster))
- if poster.size != (WIDTH, HIGHT):
- poster = poster.resize((WIDTH, HIGHT))
- else:
- poster = make_caption(mov.title)
- # Set the (x, y) in appliance with genre
- for genre in unique_genres:
- if genre == mov.genre:
- x = 20 + (WIDTH+20) * unique_genres.index(genre)
- y = 100 + y_counter[genre]
- p_counter[genre] += 1
- y_counter[genre] = HIGHT * p_counter[genre]
- canvas.paste(poster, (x, y))
- canvas.save(output_file)
- if __name__ == '__main__':
- parser = ArgumentParser(description='Downloads posters and makes "advice"')
- parser.add_argument('input_file',
- metavar='FILE',
- help='Text file containing movies (id, title, genre)')
- parser.add_argument('-o', '--out',
- dest='output_file',
- metavar='OUTPUT',
- default=DEFAULT_OUT,
- help='Name of output image (default: '+DEFAULT_OUT+')')
- args = parser.parse_args()
- main(args.input_file, args.output_file)
Advertisement
Add Comment
Please, Sign In to add comment