473,246 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

Determining if file is valid image file

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
12 6351
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
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
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
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
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
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
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
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stephen Oakes | last post by:
I am trying to upload an image to a server, but I get this warning: Warning: imagecreatefromjpeg(): 'filename' is not a valid JPEG file The error occurs when I am trying to create a thumbnail...
3
by: Jefferis NoSpamme | last post by:
Hello all, I'm trying to limit the file size to 1 meg on upload of image files and I am trying a script from javascript internet, but it is giving me errors on IE ² is null or not an object ³...
4
by: Frank Kociemba | last post by:
Hi NG, how can i compare, if a file ends with jpg,jpeg,gif,tif is really such a file and not an unnamed one. Is ther a solution or an example who to do it in c#? Frank
3
by: premprakashbhati | last post by:
hi, good evening.. i am going to upload an image in a web form .....for that iam using HTML input(file) control and one web control button i.e., Upload_Button() here is the code ...its work fine...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.