Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Determining if file is valid image file

Reply
Thread Tools

Determining if file is valid image file

 
 
=?iso-8859-1?B?QW5kcuk=?=
Guest
Posts: n/a
 
      08-02-2007
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é

 
Reply With Quote
 
 
 
 
=?iso-8859-1?B?QW5kcuk=?=
Guest
Posts: n/a
 
      08-02-2007
On Aug 2, 11:14 am, André <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      08-02-2007
André wrote:
> On Aug 2, 11:14 am, André <(E-Mail Removed)> wrote:
>> 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
 
Reply With Quote
 
Jarek Zgoda
Guest
Posts: n/a
 
      08-02-2007
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: http://www.velocityreviews.com/forums/(E-Mail Removed) | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)
 
Reply With Quote
 
Thomas Jollans
Guest
Posts: n/a
 
      08-02-2007
On Thursday 02 August 2007, André wrote:
> On Aug 2, 11:14 am, André <(E-Mail Removed)> wrote:
> > 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

A straightforward way you won't like: read the specs for all formats you're
interested in and write the function yourself
 
Reply With Quote
 
kyosohma@gmail.com
Guest
Posts: n/a
 
      08-02-2007
On Aug 2, 9:35 am, Thomas Jollans <(E-Mail Removed)> wrote:
> On Thursday 02 August 2007, André wrote:
>
> > On Aug 2, 11:14 am, André <(E-Mail Removed)> wrote:
> > > 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
>
> 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

 
Reply With Quote
 
=?iso-8859-1?B?QW5kcuk=?=
Guest
Posts: n/a
 
      08-02-2007
On Aug 2, 11:34 am, Jarek Zgoda <(E-Mail Removed)> wrote:
> 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: (E-Mail Removed) | voice: +48228430101
>
> "We read Knuth so you don't have to." (Tim Peters)



 
Reply With Quote
 
=?iso-8859-1?B?QW5kcuk=?=
Guest
Posts: n/a
 
      08-02-2007
On Aug 2, 11:38 am, (E-Mail Removed) wrote:
> On Aug 2, 9:35 am, Thomas Jollans <(E-Mail Removed)> wrote:
>
>
>
> > On Thursday 02 August 2007, André wrote:

>
> > > On Aug 2, 11:14 am, André <(E-Mail Removed)> wrote:
> > > > 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

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



 
Reply With Quote
 
Dave Hughes
Guest
Posts: n/a
 
      08-02-2007
André wrote:

> 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.


Any reason you don't want to bundle PIL? The license looks like a
fairly standard BSD style license to me which I don't think precludes
you from bundling it (other than having to reproduce the (very small)
license text in any documentation).

Otherwise, it depends on exactly what you mean by "valid". You could do
something as simple as check the "magic" number in the header of the
file. Most image formats have something like this:

* PNG: byte sequence 89 50 4E 47 0D 0A 1A 0A
* GIF: "GIF89a" or "GIF87a"
* JPG: byte sequence FF D8 FF E0 nn nn 4A 46 49 46 00 (for JFIF)

Naturally, this won't guarantee the rest of the file is valid, but
might be sufficient for your purposes (it's one of the methods the
"file" command uses for recognizing file types).


HTH,

Dave.
--

 
Reply With Quote
 
brad
Guest
Posts: n/a
 
      08-02-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error: The image file ... is valid, but is for a machine type othe =?Utf-8?B?QUpBWQ==?= Windows 64bit 5 02-17-2014 05:03 AM
"The image file ___ is valid but ...." Charlie Russel - MVP Windows 64bit 1 04-04-2010 05:30 AM
"The image file .. is valid, but is for a machine type other than =?Utf-8?B?SmFtZXM=?= Windows 64bit 11 12-17-2007 03:16 PM
Help w/Tracking Source of bogus error message: "Bad Image": SensAPI.DLL not a valid Windows Image mutu Computer Support 1 05-01-2005 11:00 AM
determining an image file height and width Irishmaninusa ASP .Net 1 07-16-2004 04:34 PM



Advertisments