Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call image.verify() on downloads to check they aren't corrupt. #17

Closed
adamcik opened this issue Jun 19, 2011 · 5 comments

Comments

Projects
None yet
2 participants
@adamcik

This comment has been minimized.

Copy link
Contributor Author

commented Jun 19, 2011

Seems image.verify() will not catch half downloaded files it seems, while Image Magick's identify will. However, using image.load() will cause IOError: image file is truncated (232 bytes not processed) so that would probably be worth a try.

@adamcik adamcik closed this Jun 19, 2011

@adamcik adamcik reopened this Jun 19, 2011

@adamcik

This comment has been minimized.

Copy link
Contributor Author

commented Jun 19, 2011

Other possible checks could be:

  • Ensure that file has some minimum number of colors
  • Ensure that content-size from http header matches size of downloaded file
  • Ensure that file size matches file size that header indicates
@jodal

This comment has been minimized.

Copy link
Owner

commented Apr 7, 2012

Two alternatives, pasted by @adamcik the other day:

import pil

def option1(file):
    parser = pil.ImageFileParser()
    try:
        while True:
            data = file.read(1024)
            if not data:
                break
            parser.feed(data)
        image = parser.close()
    except IOError as e:
        print 'Bad image: %s' % e

def option2(path):
    image = pil.Image.open(path)
    try:
        image.load()
    except IOError as e:
        print 'Bad image: %s' % e
@adamcik

This comment has been minimized.

Copy link
Contributor Author

commented Apr 8, 2012

Also im.getcolors(maxcolors) returns None once maxcolors is passed, so one could use im.getcolors(3) is None as at test to ensure that image has at least three colors. Not sure if this would add any value over just .verify() - but nice to know about at least.

http://www.pythonware.com/library/pil/handbook/image.htm#Image.getcolors

@jodal

This comment has been minimized.

Copy link
Owner

commented Jun 9, 2012

option1 in the listing above is exactly the same as Django do in django/core/files/images.py to get the height and width from the image, so I would think option would be redundant in addition to using Django's ImageField.

option2 has been implemented in ac358a0.

@jodal jodal closed this Jun 9, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.