By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,985 Members | 1,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,985 IT Pros & Developers. It's quick & easy.

Determining if file is valid image file

P: n/a
Other than installing PIL, is there a "simple" way using Python only
to determine if a file is a valid image file?

I'd be happy if I could at least identify valid images files for gif,
jpeg and png. Pointers to existing modules or examples would be
appreciated.

The reason why I'd prefer not using PIL is that I'd like to bundle
such a function/module in my app.

André

Aug 2 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On Aug 2, 11:14 am, André <andre.robe...@gmail.comwrote:
Other than installing PIL, is there a "simple" way using Python only
to determine if a file is a valid image file?

I'd be happy if I could at least identify valid images files for gif,
jpeg and png. Pointers to existing modules or examples would be
appreciated.

The reason why I'd prefer not using PIL is that I'd like to bundle
such a function/module in my app.

André
I should have added: I'm interesting in validating the file *content*
- not the filename :-)

Aug 2 '07 #2

P: n/a
André wrote:
On Aug 2, 11:14 am, André <andre.robe...@gmail.comwrote:
>Other than installing PIL, is there a "simple" way using Python only
to determine if a file is a valid image file?

I'd be happy if I could at least identify valid images files for gif,
jpeg and png. Pointers to existing modules or examples would be
appreciated.

The reason why I'd prefer not using PIL is that I'd like to bundle
such a function/module in my app.

André

I should have added: I'm interesting in validating the file *content*
- not the filename :-)
And what's wrong with bundling PIL in your application?

-Larry
Aug 2 '07 #3

P: n/a
André napisa³(a):
>Other than installing PIL, is there a "simple" way using Python only
to determine if a file is a valid image file?

I'd be happy if I could at least identify valid images files for gif,
jpeg and png. Pointers to existing modules or examples would be
appreciated.

The reason why I'd prefer not using PIL is that I'd like to bundle
such a function/module in my app.

André

I should have added: I'm interesting in validating the file *content*
- not the filename :-)
Is the module imghdr enough for your needs?

--
Jarek Zgoda
Skype: jzgoda | GTalk: zg***@jabber.aster.pl | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)
Aug 2 '07 #4

P: n/a
On Thursday 02 August 2007, André wrote:
On Aug 2, 11:14 am, André <andre.robe...@gmail.comwrote:
Other than installing PIL, is there a "simple" way using Python only
to determine if a file is a valid image file?

I'd be happy if I could at least identify valid images files for gif,
jpeg and png. Pointers to existing modules or examples would be
appreciated.

The reason why I'd prefer not using PIL is that I'd like to bundle
such a function/module in my app.

I should have added: I'm interesting in validating the file *content*
- not the filename :-)
The file name has nothing to do with the type :-P

A straightforward way you won't like: read the specs for all formats you're
interested in and write the function yourself ;-)
Aug 2 '07 #5

P: n/a
On Aug 2, 9:35 am, Thomas Jollans <tho...@jollans.comwrote:
On Thursday 02 August 2007, André wrote:
On Aug 2, 11:14 am, André <andre.robe...@gmail.comwrote:
Other than installing PIL, is there a "simple" way using Python only
to determine if a file is a valid image file?
I'd be happy if I could at least identify valid images files for gif,
jpeg and png. Pointers to existing modules or examples would be
appreciated.
The reason why I'd prefer not using PIL is that I'd like to bundle
such a function/module in my app.
I should have added: I'm interesting in validating the file *content*
- not the filename :-)

The file name has nothing to do with the type :-P

A straightforward way you won't like: read the specs for all formats you're
interested in and write the function yourself ;-)
Use the md5 module to create checksums. Links below:

http://www.peterbe.com/plog/using-md...-between-files
http://effbot.org/librarybook/md5.htm
http://docs.python.org/lib/module-md5.html

Larry is right too...what's wrong with bundling PIL or any third party
module?

Mike

Aug 2 '07 #6

P: n/a
On Aug 2, 11:34 am, Jarek Zgoda <jzg...@o2.usun.plwrote:
André napisa³(a):
Other than installing PIL, is there a "simple" way using Python only
to determine if a file is a valid image file?
I'd be happy if I could at least identify valid images files for gif,
jpeg and png. Pointers to existing modules or examples would be
appreciated.
The reason why I'd prefer not using PIL is that I'd like to bundle
such a function/module in my app.
André
I should have added: I'm interesting in validating the file *content*
- not the filename :-)

Is the module imghdr enough for your needs?
Yes, thanks.

--
Jarek Zgoda
Skype: jzgoda | GTalk: zg...@jabber.aster.pl | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)

Aug 2 '07 #7

P: n/a
On Aug 2, 11:38 am, kyoso...@gmail.com wrote:
On Aug 2, 9:35 am, Thomas Jollans <tho...@jollans.comwrote:
On Thursday 02 August 2007, André wrote:
On Aug 2, 11:14 am, André <andre.robe...@gmail.comwrote:
Other than installing PIL, is there a "simple" way using Python only
to determine if a file is a valid image file?
I'd be happy if I could at least identify valid images files for gif,
jpeg and png. Pointers to existing modules or examples would be
appreciated.
The reason why I'd prefer not using PIL is that I'd like to bundle
such a function/module in my app.
I should have added: I'm interesting in validating the file *content*
- not the filename :-)
The file name has nothing to do with the type :-P
A straightforward way you won't like: read the specs for all formats you're
interested in and write the function yourself ;-)

Use the md5 module to create checksums. Links below:
Sorry, I fail to see how this helps me to identify if a file I
retrieve from somewhere is a valid image file...
http://www.peterbe.com/plog/using-md...odule-md5.html

Larry is right too...what's wrong with bundling PIL or any third party
module?
Why not bundling PIL?: Because I'm trying to keep the size of my app
as small as possible.
I don't mind bundling some other modules from third parties (in fact,
I already do include
three modules from ElementTree...).

André
Mike

Aug 2 '07 #8

P: n/a
André wrote:
I should have added: I'm interesting in validating the file *content*
- not the filename :-)
Some formats have identifying headers... I think jpeg is an example of
this. Open it with a hex editor or just read the first few bytes and see
for yourself.

Brad
Aug 2 '07 #9

P: n/a
André napisa³(a):
>>>Other than installing PIL, is there a "simple" way using Python only
to determine if a file is a valid image file?
I'd be happy if I could at least identify valid images files for gif,
jpeg and png. Pointers to existing modules or examples would be
appreciated.
The reason why I'd prefer not using PIL is that I'd like to bundle
such a function/module in my app.
André
I should have added: I'm interesting in validating the file *content*
- not the filename :-)
Is the module imghdr enough for your needs?

Yes, thanks.
Be aware that broken images (i.e. partially downloaded) in many cases
pass the imghdr.what() test. This function checks for patterns in files,
just like "file" utility.

--
Jarek Zgoda
http://jpa.berlios.de/
Aug 2 '07 #10

P: n/a
On Aug 2, 4:25 pm, Jarek Zgoda <jzg...@o2.usun.plwrote:
André napisa³(a):
>>Other than installing PIL, is there a "simple" way using Python only
to determine if a file is a valid image file?
I'd be happy if I could at least identify valid images files for gif,
jpeg and png. Pointers to existing modules or examples would be
appreciated.
The reason why I'd prefer not using PIL is that I'd like to bundle
such a function/module in my app.
André
I should have added: I'm interesting in validating the file *content*
- not the filename :-)
Is the module imghdr enough for your needs?
Yes, thanks.

Be aware that broken images (i.e. partially downloaded) in many cases
pass the imghdr.what() test. This function checks for patterns in files,
just like "file" utility.
That's all I need; I'm not concerned about broken images. I am
writing a web app and need to prevent someone using redirection to
send malicious content when I'm supposedly loading an image file. So,
what I plan to do is open the file using urlopen, preload the image
and see if it is valid; if so, I pass it on to the browser.

To find out more, look for "redirect" on the following page (it is the
first occurence of that word)
http://ha.ckers.org/xss.html

--
Jarek Zgodahttp://jpa.berlios.de/

Aug 2 '07 #11

P: n/a

"Jarek Zgoda" <jz****@usun.plwrote in message
news:f8**********@nemesis.news.tpi.pl...
André napisa³(a):
>>>Other than installing PIL, is there a "simple" way using Python only
to determine if a file is a valid image file?
[...]
Be aware that broken images (i.e. partially downloaded) in many cases
pass the imghdr.what() test.
To put it another way, the only way to determine whether a coded file is
valid may be to decode it. And even then, it may be corrupted in the sense
that the decoded version may have artifacts not in the original. I have
seen the latter both in jpeg images and movie DVDs.

tjr

Aug 2 '07 #12

P: n/a
Terry Reedy napisa³(a):
>>>>Other than installing PIL, is there a "simple" way using Python only
to determine if a file is a valid image file?
[...]
>Be aware that broken images (i.e. partially downloaded) in many cases
pass the imghdr.what() test.

To put it another way, the only way to determine whether a coded file is
valid may be to decode it. And even then, it may be corrupted in the sense
that the decoded version may have artifacts not in the original. I have
seen the latter both in jpeg images and movie DVDs.
That's what I mean, images that cann't be read using PIL sometimes are
recognized by imghdr.what(), as it happens with "file" too - both of
these tools are the identification (not validation) utilities. To be
sure the image is "really valid", you have to use some image
manipulation program (or library), like ImageMagick (or PIL). Sometimes
imghdr.what() is enough, sometimes you need more. ;)

--
Jarek Zgoda
http://jpa.berlios.de/
Aug 2 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.