简体   繁体   English

如何将Jpeg文件压缩到有限大小(例如200KB)而又不损失很多图像质量

[英]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.hJPEGLIB.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.

相关问题 在保存到数据库之前如何压缩图像大小而不会降低质量 - How to compress image size before saving to database without losing quality 如何在Android上旋转JPEG文件而不会降低质量并获得文件大小? - How to rotate a JPEG file on Android without losing quality and gaining file size? 我必须压缩PNG文件图像,而不会降低质量 - I have to compress the PNG file image, without losing the quality 我怎样才能在不损失质量的情况下减小上传的图像大小(很多) - How can i reduce uploaded image size without losing quality(much) 压缩图像而不会丢失任何质量的android? - Compress image without losing any quality android? 如何使用NodeJS生成指定文件大小(例如x Kb)的随机图像 - How to generate a random image of specified file size (e.g. x Kb) using NodeJS PHP:如何压缩图像而不会失去可见的质量(自动)? - PHP: How to compress images without losing visible quality (automatically)? 不使用Bitmap#compress()方法将JPEG图像压缩到特定质量级别 - Compress JPEG image to specific quality level without using Bitmap#compress() method 如何在不损失质量的情况下扩展图像? - How to expand image without losing quality? 如何在不损失质量的情况下调整图像大小 - How to resize image without losing quality
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM