Page 1 of 1

Scaling Image if MIME type is different than image extension

Posted: 2008-11-11T15:44:12-07:00
by sim123
I posted this topic earlier, however can not see the same into list thats why reposting it.

I have a image "test.gif" (GIF image) and accidentally I change the extension to .jpeg "test.jpeg" but Image's MIME type is still JPEG Image so if i scale it using IM (resizing/ thumbnail) how this image will be treated as JPEG image because the extension is .jpeg/jpg or as GIF image because MIME type of the image is "Image/JPEG"?

Thanks for all the help and support.

Re: Scaling Image if MIME type is different than image extension

Posted: 2008-11-11T17:22:00-07:00
by sim123
I think the earlier post I was talking about is not actually posted, so please reply to this post.

Thanks for all the help and support.

Re: Scaling Image if MIME type is different than image extension

Posted: 2008-11-11T18:02:26-07:00
by anthony
Most of the time IM will try to work out what 'type' an image file is basied on the 'magic' of the file. That is the contents of the file determines the file type IM attempts to use

However if magic determination fails, the suffix is used.

The prefix format: however will override the normal magic determination.

I am not exactly certain of the exact order in which Im attempts to read a unknown image file. But it will do its best to figure it out.

Re: Scaling Image if MIME type is different than image extension

Posted: 2008-11-11T20:33:33-07:00
by sim123
I didn't understand what do you mean by "unknown image type".

If IM determines the image type or magick is JPEG for an image whose extension in the name is .gif, so it will treat it at JPEG write? and if I am doing this
convert test.gif -resize 200x200> -write test_1.gif

where test is JPEG type of image, will IM do a conversion internally to convert JPEG into GIF and scaled image will be of type GIF? or IM will save it as JPEG but with .gif extension?

Thanks for all the help and support.

Re: Scaling Image if MIME type is different than image extension

Posted: 2008-11-11T20:53:44-07:00
by anthony
When reading it will either
  • attemtp to read it a GIF and fail (bad magic) then determine it is JPEG and correctly read it.
  • OR just note that it is JPEG and read it as JPEG without even trying to read as GIF.
which it does is not really known.

However after the image is read in the save to a file with a suffix of 'GIF' will save as a GIF image regardless of the source!

If you want it as a JPEG image with a .gif suffix you can override this with a prefix to force the output format.

For example

Code: Select all

convert test.gif -resize 200x200\> JPEG:test_1.gif
will write a file "test_1.gif" containing a image in JPEG format!!!!


NOTE the escape for the '>' otherwise the shell may create a file for the commands standard output and Im will think "200x200" is the output filename!!!!

Also -write is only needed if the save is in the middle of a long sequence of operations. The last argument of convert is assumed to be a '-write' operation.

In your previous command the shell would create a -write file
and remove it from IM arguments!

Re: Scaling Image if MIME type is different than image extension

Posted: 2008-11-11T21:20:19-07:00
by sim123
i tried this command and I wanted the image to be scaled if its size is greater than 200x200 however when image type is JPEG and I store it as GIF IM actually scaled it to be 200x200, why is is so?

Is there a way I could find out type of the image (a command) as I am going to use convert for generic image types (from java) and will provide the new image name based on original image name.

Thanks for all the help & support.

Re: Scaling Image if MIME type is different than image extension

Posted: 2008-11-11T21:41:38-07:00
by anthony
Because the shell was seeing the '>' and not IM. As I mentioned before.

the shell sees '> -write' so it removes this and creates a file called "-write". This is completely seperate to IM itself.

1/ you don't need a -write option the last argument is the file to create
2/ your must backslash or quote the > so the shell does not act on it but passes it to IM.