Created
December 18, 2016 01:05
-
-
Save mavc/2e5af5c14a01d209f89f72122d8a2d86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import os | |
from os import path | |
from PIL import Image | |
import shutil | |
from sys import argv | |
EXTENSIONS = ('.jpg', '.png') | |
WALLPAPER_RATIO_MIN = 1.25 | |
WALLPAPER_RATIO_MAX = 2.0 | |
VERTICAL_PIXELS_MIN = 1000 | |
def is_wallpaper(image): | |
w, h = image.size | |
return (h >= VERTICAL_PIXELS_MIN and | |
WALLPAPER_RATIO_MIN <= (w * 1.0 / h) <= WALLPAPER_RATIO_MAX) | |
def walk_directory(dirname): | |
return (path.join(root, f) | |
for root, _d, files in os.walk(dirname) | |
for f in files | |
if path.splitext(f)[1].lower() in EXTENSIONS) | |
def main(args): | |
if len(args) != 2: | |
raise ValueError() | |
src, dst = args | |
for f in walk_directory(src): | |
try: | |
with Image.open(f) as im: | |
is_wall = is_wallpaper(im) | |
if is_wall: | |
print(f) | |
shutil.copy(f, dst) | |
except IOError as e: | |
print(e) | |
if __name__ == '__main__': | |
main(argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment