Ian Branch 127 Posted June 30, 2021 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
David Heffernan 2345 Posted June 30, 2021 (edited) 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 June 30, 2021 by David Heffernan 1 Share this post Link to post
Cristian Peța 103 Posted June 30, 2021 (edited) 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 June 30, 2021 by Cristian Peța Share this post Link to post
Ian Branch 127 Posted June 30, 2021 (edited) 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 June 30, 2021 by Ian Branch Share this post Link to post
Guest Posted July 1, 2021 @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
Ian Branch 127 Posted July 1, 2021 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
ConstantGardener 31 Posted July 1, 2021 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. 1 Share this post Link to post
Guest Posted July 1, 2021 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
julkas 12 Posted July 1, 2021 Try recommendation-for-compressing-jpg-files-with-imagemagick Share this post Link to post
David Heffernan 2345 Posted July 1, 2021 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. 1 Share this post Link to post
Tommi Prami 130 Posted July 1, 2021 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
Rollo62 536 Posted July 2, 2021 23 hours ago, julkas said: Try recommendation-for-compressing-jpg-files-with-imagemagick Interesting idea to add some small "blur" with compression, I thought filters like lancosz, added enough "blur" already. I have to check that next time, if this idea can offer visually better image quality. Share this post Link to post
Martin Wienold 35 Posted July 2, 2021 We use WIC Image Scaling in one of our applications. Some information with examples https://weblogs.asp.net/bleroy/resizing-images-from-the-server-using-wpf-wic-instead-of-gdi And an MSDN article https://docs.microsoft.com/en-us/windows/win32/wic/-wic-bitmapsources-howto-scale 2 Share this post Link to post
JiyaHana 0 Posted January 20 (edited) Hey try to use library like ImageMagick in Delphi 12 Athens on the other hand you can compress your jpeg images from online application such as https://jpegcompressor.com/. It supports various type of formats and compress images without losing their original quality. Edited January 20 by JiyaHana Share this post Link to post