in reply to How do I find the type of an image?

Actually, I've had the same idea of using 'file' command but it's not available on windows OS which will make my script unmovable

Image::Magick uses Image::Info module which uses the exact same regexp I've used.

It might sound very optimistic for those two matches, but what i am actually interested in and will use it for is just (png,gif,jpeg) and it all depends on the way you use it.

Finally, the main intention behind writing it, is not relying on file extensions to determine if a file is an image or not as extensions are useless in Linux

  • Comment on Re: How do I find the type of an image?

Replies are listed 'Best First'.
Re: Answer: How do I find the type of an image?
by zentara (Cardinal) on Dec 28, 2009 at 16:41 UTC
    Finally, the main intention behind writing it, is not relying on file extensions to determine if a file is an image or not as extensions are useless in Linux

    ... that is not true..... .htm, .html, .jpg,. gif,.bmp,.png,...etc..extensions...etc....are all very important in the linux file system..... what is useless in linux is the old 8.3 MSDOS file types by extension.... where executables or text type is set by the 8.3 conventions

    ...... in linux, if i try to open a JPG named "somejpeg" it will not be recognized without expensive calles to mime type programs...... even in linux you need "someimage.jpg" for it to be recognized

    .....only in disk filesystem forensics (like recovering data from a broken filesystem) would you really need to manually ascertain file type


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku

      what I meant by 'File extensions is useless in Linux is this

      pico -w my.anything ======= write inside it #!/usr/bin/perl print "Hello world\n"; === close it (CTRL + X) & save it. chmod +x my.anything now you can run it ./my.anything and it will execute your perl script. * you can even name it my.jpg & it will execute your perl script

      I am using Ubuntu Netbook, I have many images saved without extensions ... and it works that way.

        * you can even name it my.jpg & it will execute your perl script

        ..... yes, the basic linux filesystem has file permissions..... not file extensions... to determine if a file is an executable..... this is far safer than extensions as you are beginning to find out..... you probably are coming from a MSWindow's background and need to learn more about the improved security of linux....

        it probably is getting crazy for you, if you are running a dual boot with linux and windows..... then you will start seeing files from the Windows OS, that seem marked executable, or vice versa... or you do alot of double-clicking on icons and expect the system to do what you want".. :-)

        .... the way it works on linux is:

        ....you instruct your shell to execute a program, and regardless of it's name it will see if its marked as executable by you or your group..... if it is...it tries to execute it.... but if you try to execute a jpg or bmp.... it won't run....and will just return an error...... and then there are the shebang lines :-)....... maybe read linux programming... and the Rute User's Guide

        ...that brings us to default programs and apps which will properly load your file....its a part of your system setup....ubuntu uses the Gnome Desktop to determine file types thru a MIME-information setup..... first look for extension....then check type with a file-like command

        ...this is all getting to the point of you needing to read up on linux....it isn't a Perl problem

        ...see monitor suid and world writtable files for an example of checking perms.....

        ....also on ubuntu....try installing the program Midnight Commander, go thru it's menus and see what info is tracked on files

        I have many images saved without extensions ... and it works that way.

        ...sure it does..... but that is because the Ubuntu Desktop handles it all for you..... as a matter of fact, your only problem may be that you have a destop setting set to hide file extensions, like they do on Winblows

        why don't you right-click on those images and get the info?...... are you trying to automatically rename something like the browser's cache of images? :-) which are named like numbers?

        ..... you need to learn how Desktops work...... it's actually a program that has MIME type records built in..... go see MIME modules for Perl


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku
Re^2: How do I find the type of an image?
by JavaFan (Canon) on Dec 29, 2009 at 23:41 UTC
    Actually, I've had the same idea of using 'file' command but it's not available on windows OS which will make my script unmovable
    I think you are confusing "not available" with "not installed by default". But if the latter is a reason for you script to be unmoveable, I urge you to not write your program in Perl. After all, perl is as unavailable on Windows as file is: neither comes by default with the OS. But both have been ported to the platform.
Re^2: How do I find the type of an image?
by matze77 (Friar) on Dec 30, 2009 at 10:56 UTC