Winny
Winny

Reputation: 1278

Get Image Type and Metadata in Racket

How do I get a file's type and if it's an image, get its dimensions?

I want to read files from disk, determine if these files are indeed images, and get their dimensions to determine if they meet my program's criteria.

I noticed the racket/draw packages has a bitmap% type which can load images, but this seems beyond the scope of my task, as I think loading the files into a drawable datatype will exceed my memory-usage goals and lead to confounded code. In addition, if I use this, I'd have to use the bitmap%'s errors to screen out files that are not images.

I also saw a package to manipulate png images, however, I want to also get information on jpeg, gif, and possibly tiff.

So really my question is two fold - is there something like libmagic for Racket, and is there something like imagemagick for Racket?

Upvotes: 1

Views: 234

Answers (1)

soegaard
soegaard

Reputation: 31145

To determine the file type, you can

  • look at the file extension

or

  • read the first bytes of the file and compare with the magic number for some chosen the image formats

Information on magic numbers:https://en.wikipedia.org/wiki/Magic_number_%28programming%29

An alternative is to use an external utility file. The utility is available on many systems. This man page is for the Linux version: https://linux.die.net/man/1/file

With respect to getting information out of a jpeg file, see this package:

https://pkgs.racket-lang.org/package/binary-class-exif

Upvotes: 1

Related Questions