Jump to content
Ian Branch

Physically reduce jpeg image size??

Recommended Posts

Hi Team,

I have a table with a blob field that contains jpeg files of approx 256kB size.

I want to reduce their size to around 128kB size.  Thus reducing the overall table size.

Iterating through the table isn't an issue,  I need to read the image, reduce its physical size then write it back to the field.  Preferably without too much degradation.

Thoughts/suggestions appreciated.

 

Regards & TIA,

Ian

Share this post


Link to post

Do you want to resize the image or increase the compression? And which framework, vcl or fmx? And if you want to resize, what resampling do you want to use? 

Edited by David Heffernan
  • Like 1

Share this post


Link to post

If you don't want to compromise quality there is https://www.jpegmini.com/

There are some ways for developers to automate this but you can also buy a standalone app for 59$ and do it manually (first extract images to a folder then back to database).

 

I think that with a free trial you can see how much it helps in your case.

Edited by Cristian Peța

Share this post


Link to post

David,

VCL, Windows 32bit.  The goal is to reduce the physical and therefore stored size.  resize or compression, which ever achieves the objective.

 

Cristian,

I had considered and even played with an external tool but this has to run on a Client's PC/Server so I would prefer some manner that would be self contained.

 

Ian

Edited by Ian Branch

Share this post


Link to post
Guest

@Ian Branch I can't figure what is your questions exactly, 

As David asked and you didn't answer, i will repeat the questions on your question

1) Do you want code on how to read image from DB then write it back ? i will assume this is not the question.

2) Do you need code that decrease the size of jpeg ? i will assume this is the question

...

 

 

While not sure what is needed, but here a thought

The easiest to decrease the size of jpeg is to stretch it down, if we have a BMP then we can calculate it precisely by decreasing the dimension of it by the square root of 1/2 , root(0.5) is something around 0.70

So open read jpeg then stretch it down by 30% then save it compressed, the size will not be 50% exactly as it is not a bitmap, but will be close enough to your target, adjust that 70% decreasing or 30% scaling down, same thing.

Decreasing dimensions by 50% will give you 25% of the original area.

Share this post


Link to post

Hi Kas Ob,

I thought I had answered the questions but obviously not adequately.

I can read the image from the table and I can write the image back to the table, what I don't know, and yes I would appreciate some code, is how to reduce its size, or 'stretch it down', as you put it.

Ian

Share this post


Link to post
function ResponseWithScaledJPG (const AFilename : string; var Response: TIdHTTPResponseInfo) : boolean;
var     JPEGImg      : TJPEGImage;
        AStream      : TStream;
begin
  result:=false;
  if FileExists (AFilename) then
   begin
    AStream := TMemoryStream.Create;
    JPEGImg := TJpegImage.Create;
    try
     JPEGImg.LoadFromFile(AFilename);
     if JPEGImg.Width<500 then
      JPEGImg.Scale:=jsFullSize
     else
      if JPEGImg.Width<1000 then
       JPEGImg.Scale:=jsHalf
      else
       if JPEGImg.Width<2000 then
        JPEGImg.Scale:=jsQuarter
       else
        JPEGImg.Scale:=jsEighth;
     JPEGImg.SaveToStream(AStream);
     result:=true;
    finally
     JPEGImg.Free;
     Response.ContentType := mime.ImageJPeg;
     Response.ContentStream := AStream;
    end;
   end;
end;

....use this as base for your own solution. This function is from a little INDY webserver for responding with scaled pic's to jpeg-requests for limiting the traffic payload. 

  • Like 1

Share this post


Link to post
Guest

I didn't know and never used TJpegImage.Scale !

 

There is tens of solutions how how to do it, just google and pick the one your feel comfortable with

http://www.delphigroups.info/2/4/313095.html

 

But i think that your problem is not Jpeg per se, but its relation with TBitmap and TPicture, so i suggest to search and play with them, open few files and assign them, change their format and encoding, use small test VCL application for that, you will feel way better getting more familiar with their functionality and simplicity, specially you need to know what TJPEGImage.DIBNeeded and TJPEGImage.JPEGNeeded actually do, find that on your own !

Share this post


Link to post

The thing is, both resizing and compression will change the image and only you can judge which you prefer. It depends on what you are trying to do with these images, what sort of images they are. In my view you need to engage with that side of things first, not in a coding setting, to work out precisely what transformation you want. Then implementing it in code should be simple. 

  • Like 1

Share this post


Link to post

I bet that using

JPEGImg.Scale:=js*

Is really fast but not sure how good visual quality will it have.

Bitmap32 library has good quality Resamplers but for sure it'll take way longer than that.

 

-Tee- 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×