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é | |
Share this Question
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 :-) | |
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 | |
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) | |
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 ;-) | |
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 | |
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)
| |
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
| |
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 | |
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/ | |
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/
| |
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 | |
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/ | | This discussion thread is closed Replies have been disabled for this discussion. | | Question stats - viewed: 5485
- replies: 12
- date asked: Aug 2 '07
|