Skip to content

Instantly share code, notes, and snippets.

@ty0x2333
Last active February 24, 2017 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ty0x2333/212cef95e7a85ef5a92cbb0c9e1033c4 to your computer and use it in GitHub Desktop.
Save ty0x2333/212cef95e7a85ef5a92cbb0c9e1033c4 to your computer and use it in GitHub Desktop.
1ximage.py
#!/usr/bin/env python
# https://gist.github.com/luckytianyiyan/212cef95e7a85ef5a92cbb0c9e1033c4
import os
import fnmatch
import argparse
__author__ = 'luckytianyiyan@gmail.com'
__version__ = '0.2.0'
__copyright__ = 'MIT License'
BLUE = '\033[1;94m'
GREEN = '\033[1;92m'
ENDC = '\033[0m'
def iter_files(path, fnexp):
for root, dirs, files in os.walk(path):
for filename in fnmatch.filter(files, fnexp):
yield os.path.join(root, filename)
def extant_dir(x):
if not os.path.exists(x):
# Argparse uses the ArgumentTypeError to give a rejection message like:
# error: argument input: x does not exist
raise argparse.ArgumentTypeError("{0} does not exist".format(x))
elif not os.path.isdir(x):
raise argparse.ArgumentTypeError("{0} isn't a directory".format(x))
return x
def main(argv=None):
parser = argparse.ArgumentParser(description='find all 1x image in the specified directory.')
parser.add_argument('--version', action='version', version=__version__)
parser.add_argument('path', help='destination directory', type=extant_dir)
args = parser.parse_args(argv)
total = 0
for file_path in iter_files(args.path, '*@2x.png'):
file_name = os.path.basename(file_path)
(name, _) = file_name.split('@')
if not list(iter_files(args.path, name + '@3x.png')):
continue
imgs_1x = list(iter_files(args.path, name + '.png'))
if imgs_1x:
total += 1
print BLUE + '=> ' + name + ENDC
for img in imgs_1x:
print img
print u'\U0001F37B ' + GREEN + 'total: %d' % total + ENDC
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment