Skip to content

Instantly share code, notes, and snippets.

@walshbr
Last active August 18, 2016 15:41
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 walshbr/11edd4dc6cfcb81096e705a866873c3b to your computer and use it in GitHub Desktop.
Save walshbr/11edd4dc6cfcb81096e705a866873c3b to your computer and use it in GitHub Desktop.
Crawls over all .png images in a directory and compresses them by converting to jpg. Deletes pngs afterwards. Ignores non .png files. Run from root of the directory you want to crawl over.
import os
from PIL import Image
def all_files(dirname):
for (root, _, files) in os.walk(dirname):
for fn in files:
yield os.path.join(root, fn)
def convert_image(file):
img = Image.open(file)
print(os.path.splitext(file)[0] + '.jpg')
img.save(os.path.splitext(file)[0] + '.jpg')
os.remove(file)
def main():
fns = []
for fn in all_files('.'):
if os.path.splitext(fn)[1] == '.png':
fns.append(fn)
for fn in fns:
convert_image(fn)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment