3

I'm using ImageMagick to do some image manipulation. I'm trying to stream a file from a service that returns an input stream and do image manipulation on the fly without letting the file hit the HDD.

When I convert the input stream to file using InputStream, OutputStream, FileOutputStream classes, the resulting file size is the same as the file that's stored on the service (initial file size is 9 MB). This is fine. When I run the input stream thru ImageMagick, even though I haven't done any manipulations, the resulting file is way smaller (2 MB). I'm not sure what I'm doing wrong or if ImageMagick is doing something to the file without me knowing. Here's the snippet of my code:

IMOperation op = new IMOperation();
ConvertCmd convert = new ConvertCmd();

InputStream is = s.getFileFromBucket(BUCKET, filename);
op.addImage("-");  // read from stdin
op.addImage(outfile);
Pipe pipeIn = new Pipe (is,null);
convert.setInputProvider(pipeIn);
convert.run(op);
is.close();

So I expected the file to be the same size going out as it was coming in since I haven't done anything to it yet but it's smaller.

Someone had suggested perhaps the service died midway thru while streaming and that's why the file is so small. I don't think this is the case because every test I've run with it going thru ImageMagick, the output file size is the same (2 MB). Also, when I use my viewer to look at the resulting file, it displays the file without problems. Lastly, when I convert the input stream to file, that always results in a file that's 9 MB and displays fine with the viewer.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
Classified
  • 5,759
  • 18
  • 68
  • 99

2 Answers2

3

Jpeg is a compressed format, Imagemagick is doing some compressions automatically.

See: http://www.imagemagick.org/Usage/formats/#jpg

"As the JPEG quality gets lower, the size of the image also gets smaller. The default quality setting, when no JPEG quality is set, either by the user, or from the source image format file, is about 92%, which is a very high quality."

Maybe your input Jpeg is lossless -> 100%

EDIT: you can test this by taking the original picture and saving it as jpeg with an arbitrary image processing program by setting the compression rate to 92. Just compare the filesizes afterwards.

romedius
  • 775
  • 6
  • 20
  • thx for finding this on their website for me. i used the tip below by kurt pfeifle and you are right, the quality of the 2mb file was 92. i couldn't find another program that allowed me to change the quality to 92 (not even gimp) so i had to use imagemagick to do this. after running the original 9mb file thru IM with a compression rate of 92, the resulting file is the 2mb file, exactly to the byte. thanks for helping me solve my problem =) – Classified Sep 28 '12 at 08:00
2

You should do what romedius suggested (+1).

Additionally, just run

identify -verbose input.stream  > input.txt
identify -verbose output.stream > output.txt

Then compare the two results to see exactly what's different.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • thx for this tip. i used your tip and checked that the quality is 92 for the 2mb file. thx for helping me solve this problem =) – Classified Sep 28 '12 at 08:02