[英]How to Compress Jpeg file to a limited size (e.g 200KB) without losing much image quality
I wish to reduce image size around 200KB before save into database. 在保存到数据库之前,我希望将图像大小减小200KB左右。 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 (eg bmp, jpg, png), but final graphic type will be Jpeg lesser than 200KB).
假设我有一个约39MB的位图文件,我需要将位图转换为Jpeg,然后减小Jpeg文件<= 200KB(所有类型的图形文件都可以转换(例如bmp,jpg,png),但是最终的图形类型可以小于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):
我正在编写以下代码尝试进行转换(在这种情况下,我将jpg品质设置为10,因为我希望文件大小为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. 我使用相同的图像文件在导出对话框的Adobe Lightroom程序中进行转换,将大小限制为200KB,并与通过上述功能BMPtoJPG转换的图像进行比较。 The quality image from Adobe Lightroom is much more better than the function method (both file size around 200KB)
Adobe Lightroom提供的高质量图像比功能方法要好得多(两个文件大小均在200KB左右)
Can anyone show me how to write code reduce image size (limit size to 200KB) while the quality doesn't drop much. 任何人都可以向我展示如何编写代码来减少图像大小(将大小限制为200KB),而质量却不会下降太多。
Thanks and appreciate for your answer. 感谢并感谢您的回答。
If your goal is to have a smaller file content, you can first resize the bitmap, then apply a better CompressionQuality
rate. 如果您的目标是缩小文件内容,则可以先调整位图的大小 ,然后再应用更好的
CompressionQuality
率。
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. TJpegImage
依赖于标准的libjpeg库,而我认为Adobe具有自己的优化实现。 You can try other implementations, like the one included in GDI+ , or a pure pascal implementation or even old Intel's library . 您可以尝试其他实现,例如GDI +中包含的实现,或者纯Pascal实现 ,甚至是旧的Intel库 。
See if your encoder support sampling. 查看您的编码器是否支持采样。 If you sample your Cb and Cr components at a lower rate than the Y component, you can often get better compression with out a lot of quality loss.
如果以比Y分量低的速率对Cb和Cr分量进行采样,则通常可以获得更好的压缩效果而没有很多质量损失。 If you do 4:1:1 you cut the amount of data to compress nearly by a third.
如果执行4:1:1,则将压缩的数据量减少了近三分之一。 2:1:1 cuts it by half.
2:1:1将其削减一半。
This technique called chroma subsampling . 此技术称为色度二次采样 。 Unfortunately, Delphi's stock
TJpegImage
wrapper around IJG implementation ver. 不幸的是,Delphi的股票
TJpegImage
包装器围绕IJG实现版本。 6b does not expose this capability and initializes encoder context to all-default values which corresponds to 4:2:0. 6b没有展示此功能,而是将编码器上下文初始化为对应于4:2:0的全默认值。 Refer to
TJPEGImage.Compress
method code, libjpeg
documentation and jpeglib.h
or JPEGLIB.PAS
from the pasjpeg
package, if you prefer (latter is merely straightforward translation of IJG implementation ver. 6a to Pascal). 如果愿意,请参考来自
pasjpeg
包的TJPEGImage.Compress
方法代码, libjpeg
文档和jpeglib.h
或JPEGLIB.PAS
(后者只是将IJG实现版本6a转换为Pascal的直接翻译)。
Also, you can migrate to the one of other implementation. 另外,您可以迁移到其他实现之一。 In addition to @Arnaud Bouchez list, it is worth to mention yet another Pascal implementation available to reuse: http://www.colosseumbuilders.com/sourcecode
除了@Arnaud Bouchez列表之外,值得一提的还有另一个可重用的Pascal实现: http : //www.colosseumbuilders.com/sourcecode
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.