Created
March 17, 2015 01:55
-
-
Save arvinsim/601c808aefe656bc68b6 to your computer and use it in GitHub Desktop.
Check if a file is valid PNG
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import argparse | |
from PIL import Image | |
# Get filename from arguments | |
parser = argparse.ArgumentParser(description='get arguments') | |
parser.add_argument('filename') | |
args = parser.parse_args() | |
print args.filename | |
# Check if file exists | |
if not os.path.isfile(args.filename): | |
raise IOError('Is not a file') | |
# TODO: Check if file is valid png | |
im = Image.open(args.filename) | |
im.verify() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can do this without requiring PIL. Simply get the file contents and match the first chars ([:8]) with "\x89PNG\r\n\x1a\n" and char [12:16] with "IHDR".