<s> from io import BytesIO <EOL> try : <EOL> from PIL import Image as im <EOL> IMAGE_UTILS_AVAILABLE = True <EOL> Image = im . open <EOL> def resize ( image , size ) : <EOL> output = BytesIO ( ) <EOL> back = im . new ( '<STR_LIT>' , size , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> image . thumbnail ( size , im . ANTIALIAS ) <EOL> offset = [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> if image . size [ <NUM_LIT:0> ] >= image . size [ <NUM_LIT:1> ] : <EOL> offset [ <NUM_LIT:1> ] = int ( back . size [ <NUM_LIT:1> ] / <NUM_LIT:2> - image . size [ <NUM_LIT:1> ] / <NUM_LIT:2> ) <EOL> else : <EOL> offset [ <NUM_LIT:0> ] = int ( back . size [ <NUM_LIT:0> ] / <NUM_LIT:2> - image . size [ <NUM_LIT:0> ] / <NUM_LIT:2> ) <EOL> back . paste ( image , tuple ( offset ) ) <EOL> back . save ( output , image . format ) <EOL> contents = output . getvalue ( ) <EOL> output . close ( ) <EOL> return contents <EOL> except ImportError : <EOL> IMAGE_UTILS_AVAILABLE = False <EOL> LARGEST_ICON_SIZE = <NUM_LIT> <EOL> SMALLEST_ICON_SIZE = <NUM_LIT:16> <EOL> sizes = [ LARGEST_ICON_SIZE , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:32> , SMALLEST_ICON_SIZE ] <EOL> def nearest_icon_size ( width , height ) : <EOL> maximum = max ( width , height ) <EOL> if maximum >= LARGEST_ICON_SIZE : <EOL> return LARGEST_ICON_SIZE <EOL> if maximum <= SMALLEST_ICON_SIZE : <EOL> return SMALLEST_ICON_SIZE <EOL> for i in range ( len ( sizes ) - <NUM_LIT:1> ) : <EOL> current_size = sizes [ i ] <EOL> next_size = sizes [ i + <NUM_LIT:1> ] <EOL> if current_size > maximum >= next_size : <EOL> return next_size </s>
