Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gagomes
Last active January 6, 2018 11:07
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 gagomes/e30f2571c8edda8bb87eb4437a7f0424 to your computer and use it in GitHub Desktop.
Save gagomes/e30f2571c8edda8bb87eb4437a7f0424 to your computer and use it in GitHub Desktop.
scan pictures in a given path and copy them out onto another
#!/usr/bin/python
# I used this script to scan pictures in a 15 year old
# windows hard-drive and copy them to a specific folder with a name
# based on an increasing sequence. It also seemed to pick multiple
# formats that weren't exactly the type of pictures I would care about,
# so I filtered for those (icons, bitmaps, etc)
#
import sys
import os
from PIL import Image
import shutil
def main():
rootdir = sys.argv[1]
out = os.path.abspath(sys.argv[2])
i = 0
for root, subdirs, files in os.walk(rootdir):
for file in files:
path = os.path.join(root, file)
try:
image = Image.open(path)
#image.load()
except:
continue
filename, file_extension = os.path.splitext(path)
# skip unwanted filetypes
if file_extension.lower() in ['.ico', '.bmp', '.gif']:
continue
if file_extension == '':
dest = '%d' % (i)
else:
dest = '%d%s' % (i, file_extension)
dest = os.path.join(out, dest)
print(dest)
shutil.copyfile(path, dest)
i += 1
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment