def is_ok_for_wallpaper(image_url):
    """Decide whether an image_url is appropriate as a wallpaper.
    
    An image_url is good if (1) it's resolution is large enough,
    (2) rotation is landscape, and (3) ratio is OK.
    """
    minimum_pixels = cfg.SIZE_THRESHOLD[0] * cfg.SIZE_THRESHOLD[1] * \
                     ((100.0 - cfg.SIZE_TOLERANCE_PERCENTAGE)/100.0)
    file_name = web.get_file_name(image_url)
    file_path = cfg.PHOTO_DIR + file_name
    try:
        img = Image.open(file_path)
    except IOError:
        print("# warning: I/O error with {0}".format(file_name))
        return False
    # else, if the image could be opened
    width, height = img.size

    large = (width * height) >= minimum_pixels
    landscape = width > height
    ratio = float(width) / float(height)
    ratio_ok = (cfg.RATIO_INTERVAL[0] <= ratio <= cfg.RATIO_INTERVAL[1])

    ok_for_wallpaper = (large and landscape and ratio_ok)

    return ok_for_wallpaper
def is_ok_for_wallpaper(image_url):
    """Decide whether an image_url is appropriate as a wallpaper.
    
    An image_url is good if (1) it's resolution is large enough,
    (2) rotation is landscape, and (3) ratio is OK.
    """
    minimum_pixels = cfg.SIZE_THRESHOLD[0] * cfg.SIZE_THRESHOLD[1] * \
                     ((100.0 - cfg.SIZE_TOLERANCE_PERCENTAGE)/100.0)
    file_name = web.get_file_name(image_url)
    file_path = cfg.PHOTO_DIR + file_name
    try:
        img = Image.open(file_path)
    except IOError:
        print("# warning: I/O error with {0}".format(file_name))
        return False
    # else, if the image could be opened
    width, height = img.size
    
    large = (width * height) >= minimum_pixels
    landscape = width > height
    ratio = float(width) / float(height)
    ratio_ok = (cfg.RATIO_INTERVAL[0] <= ratio <= cfg.RATIO_INTERVAL[1])
    
    ok_for_wallpaper = (large and landscape and ratio_ok)
        
    return ok_for_wallpaper
def delete_bad_images(bad_image_urls):
    """Delete images from the file system that are not suitable for wallpapers."""
    for url in bad_image_urls:
        try:
          os.remove(cfg.PHOTO_DIR + web.get_file_name(url))
        except Exception as e:
          print("Error while removing: {0}, E: {1}".format(url, e))

    print("# removed image(s): {0}".format(len(bad_image_urls)))
def resize_large_images(image_urls):
    """Resize wallpaper images that are too large."""
    global cnt_resized_images

    for image_url in image_urls:
        file_name = web.get_file_name(image_url)
        file_path = cfg.PHOTO_DIR + file_name
        img = Image.open(file_path)
        width = img.size[0]
        if width > cfg.MAX_WIDTH:
            resize.resize_to_screen_width(file_path)
            cnt_resized_images += 1
def resize_large_images(image_urls):
    """Resize wallpaper images that are too large."""
    global cnt_resized_images
    
    for image_url in image_urls:
        file_name = web.get_file_name(image_url)
        file_path = cfg.PHOTO_DIR + file_name
        img = Image.open(file_path)
        width = img.size[0]
        if width > cfg.MAX_WIDTH:
            resize.resize_to_screen_width(file_path)
            cnt_resized_images += 1
def delete_bad_images(bad_image_urls):
    """Delete images from the file system that are not suitable for wallpapers."""
    for url in bad_image_urls:
        os.remove(cfg.PHOTO_DIR + web.get_file_name(url))

    print("# removed image(s): {0}".format(len(bad_image_urls)))
def delete_bad_images(bad_image_urls):
    """Delete images from the file system that are not suitable for wallpapers."""
    for url in bad_image_urls:
        os.remove(cfg.PHOTO_DIR + web.get_file_name(url))

    print("# removed image(s): {0}".format(len(bad_image_urls)))