简体   繁体   English

如何压缩图像?

[英]How to compress image?

I have problem with image compression. 我在图像压缩方面遇到问题。 I need to compres a lot of files (700-900kb) to files 70-80kb without loss of quality. 我需要将许多文件(700-900kb)压缩为70-80kb的文件,而又不损失质量。 (or small loss ) I found menu item "Save for Web & Devices ..." in Photoshop. (或小损失)我在Photoshop中找到菜单项“为Web和设备保存...”。 It works great. 效果很好。 But I don't want to use photoshop programmatically. 但是我不想以编程方式使用photoshop。 May be someone knows how to solve this problem with other third party components or frameworks? 可能有人知道如何使用其他第三方组件或框架解决此问题?

Thanks for any ideas! 感谢您的任何想法!

.NET has a number of image decoding/encoding libraries, often tied to a particular GUI framework (eg in Windows Forms you have System.Drawing.Image and for WPF, see the Imaging Overview chapter on msdn). .NET具有许多图像解码/编码库,通常与特定的GUI框架相关联(例如,在Windows窗体中,您具有System.Drawing.Image ;对于WPF,请参阅msdn上的“ 映像概述”一章)。

There are also third party libraries specialized in image conversion/compression that you can find online (both free and non free) 您还可以在线找到专门从事图像转换/压缩的第三方库(免费和非免费)

Generally though, the amount of saving you get from compressing an image highly depends on the original format. 通常,压缩图像所节省的费用在很大程度上取决于原始格式。 If you already have JPEG photos with normal compression (quality of 85%) then there is not much you can do in terms of making them smaller except resizing them. 如果您已经拥有具有正常压缩(质量85%)的JPEG照片,那么除了调整它们的大小之外,您无法做很多其他事情来缩小它们。 If you have raw bitmaps (eg BMP, uncompressed/low compression TIFF etc.) then you can expect quite large savings with most compressing formats 如果您有原始位图(例如BMP,未压缩/低压缩TIFF等),那么大多数压缩格式都可以节省很多钱

When choosing image format, consider this: 选择图像格式时,请考虑以下因素:

  • Photos and similar: JPEG will often do fine. 照片和类似图片:JPEG通常会很好。 Good savings with reasonable quality loss 节省大量资金,但质量损失合理
  • Screenshots and similar: PNG will generally give best results (PNG is lossless). 屏幕截图和类似内容:PNG通常会产生最佳效果(PNG无损)。 JPEG will often create highly visible artifacts on screenshots JPEG通常会在屏幕截图上创建高度可见的伪像

Compressing an already compressed image (ie PNG, JPEG etc.) with a general purpose compression algorithm like ZIP or RAR will in practice not give you any savings. 实际上,使用通用压缩算法(例如ZIP或RAR)压缩已压缩的图像 (即PNG,JPEG等)将不会给您带来任何节省。 You may actually end up with a bigger file. 您实际上可能会得到一个更大的文件。

You can have a look at the FreeImage project. 您可以看一下FreeImage项目。 It has a C# wrapper that you can use. 它具有您可以使用的C#包装器。

Imagemagick allows you to batch-processing on files and offers a everything you could possible ask for when it comes to handling of images Imagemagick允许您对文件进行批处理,并提供处理图像时可能需要的一切

Eg to resize every image in folder (destroy originals) to QVGA do 例如,将文件夹中的每个图像(破坏性原件)的大小调整为QVGA

mogrify -resize 320x240 *.jpg

To preserve aspect ratio do 要保持宽高比

mogrify -resize 320x240! *.jpg

If you need to traverse a directory structure, this is how you can do it in *nix based systems (also destroying originals) 如果您需要遍历目录结构,则可以在基于* nix的系统中进行操作(也可以销毁原始文件)

find . -type f -name *.jpg -exec convert -resize 800x800 {} \;

There is also an quality switch available, see here 还有一个质量开关,请看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM