Last active
August 18, 2016 15:41
-
-
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.
This file contains 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
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