How to Compress Jpeg file to a limited size (e.g 200KB) without losing much image quality

Sherlyn Chew picture Sherlyn Chew · Jul 8, 2014 · Viewed 10.8k times · Source

I wish to reduce image size around 200KB before save into database. Let say I have a bitmap file around 39MB, I need to convert Bitmap to Jpeg and then reduce the Jpeg file <= 200KB (all type of graphics file are possible to convert (e.g bmp, jpg, png), but final graphic type will be Jpeg lesser than 200KB). I'm writing the following code try to convert (in this case i set the jpg quality to 10 since i want the file size to be 200KB):

    function BMPtoJPG(var BMPpic, JPGpic: string):boolean;
    var Bitmap: TBitmap;
        JpegImg: TJpegImage;
    begin
      Result:= False;
      Bitmap := TBitmap.Create;
      try
        Bitmap.LoadFromFile(BMPpic) ;
        JpegImg := TJpegImage.Create;
        try
          JpegImg.CompressionQuality := 10; //in this case i set the quality to 10 so the file size will be around 200KB)
          JpegImg.Assign(Bitmap);
          JpegImg.Compress;
          JpegImg.SaveToFile(JPGpic) ;
          Result:=True;
        finally
          JpegImg.Free
        end;
      finally
        Bitmap.Free;
      end;
    end;

I use the same image file to convert in Adobe Lightroom program in export dialog limit the size to 200KB and compare with the image converted by the above function BMPtoJPG. The quality image from Adobe Lightroom is much more better than the function method (both file size around 200KB)

Can anyone show me how to write code reduce image size (limit size to 200KB) while the quality doesn't drop much.

Thanks and appreciate for your answer.

Answer

Arnaud Bouchez picture Arnaud Bouchez · Jul 8, 2014

If your goal is to have a smaller file content, you can first resize the bitmap, then apply a better CompressionQuality rate.

As a result, the global quality of the image would probably be better.

The TJpegImage relies on the standard libjpeg library, whereas I suppose that Adobe has its own tuned implementation. You can try other implementations, like the one included in GDI+, or a pure pascal implementation or even old Intel's library.