473,409 Members | 2,034 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,409 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 6362
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.