将多个tif和Jpeg合并为单个tif文件,该文件的大小很大 [英] Combine multiple tif and Jpeg into single tif file has huge size

查看:100
本文介绍了将多个tif和Jpeg合并为单个tif文件,该文件的大小很大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将多个tif和jpeg文件合并为一个tif文件.

当单独将多个tif文件组合到一个单个tif文件中时,该文件的大小与原始多个tif文件的大小几乎相同(多个tif文件中的10 MB ---->单个tif文件中的10 MB).太完美了.

但是,当将单独的tif文件和许多Jpeg文件组合到单个tif文件中时,文件大小与原始文件相比很大(10 MB的多个tif和Jpeg文件导致创建了200 MB的tif文件).

有什么方法可以防止JPEG文件出现时文件过大?

使用的代码:

  List< BufferedImage>bufferedImageList = new ArrayList<>();对于(字符串页面:页面){BufferedImage bufferedImage = ImageIO.read(file);bufferedImageList.add(bufferedImage);}字符串文件名="D:\ home \ example.tif";ImageWriter writer = ImageIO.getImageWritersByFormatName("TIF").next();尝试(ImageOutputStream输出= ImageIO.createImageOutputStream(新文件(文件名))){writer.setOutput(输出);ImageWriteParam参数= writer.getDefaultWriteParam();params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);params.setCompressionType("LZW");params.setCompressionQuality(1.0f);writer.prepareWriteSequence(null);对于(BufferedImage image:bufferedImageList){writer.writeToSequence(new IIOImage(image,null,null),params);}writer.endWriteSequence();}writer.dispose(); 

解决方案

如@Henry在评论中所写,使用LZW(或任何其他无损)压缩将不会实现与有损JPEG(对于自然"图像)相同的压缩.).在大多数情况下,采用这种方式将产生较大的文件,但可能具有最高的质量.在许多情况下,这是可以接受的.

另一种选择是通过指定 compressionType "JPEG" 在TIFF文件中也使用JPEG压缩.您可能还需要将 compressionQuality 设置得较低(我相信 0.7f 是ImageIO中JPEG的默认设置),以获得更合理的文件大小.

但是,使用JPEG压缩来重新压缩已经进行JPEG压缩的图像,将会引入世代丢失" ,因为通常无法完美地重建JPEG.通过使用与原始表相同的表进行重新压缩,从技术上讲可以将这种质量损失保持在最低水平,但是由于编码器/解码器中的舍入误差较小,因此在实践中很难做到这一点(即,最好的选择是使用相同的值).使用原始参数编写的原始编码器.

第三个选择是使用/创建专用的TIFF实用程序,该实用程序可以按原样在新的TIFF容器中存储JPEG流.这些文件可能不是超级高效的TIFF,因为它们不支持条带/平铺等,并且某些非标准输入可能仍需要重写才能生成有效的TIFF.这将需要更多的工作,并且需要一些有关TIFF格式的深入知识,但肯定是可行的.

I am trying to combine multiple tif and jpeg files into single tif file.

When multiple tif files alone are combined into one single tif file, the file has nearly the same size as of original multiple tif files (10 MB of multiple tif files ----> 10 MB of single tif file). This is perfect.

However, when tif files alone with many number of Jpeg files are combined to single tif file, the file size is huge compared to original (10 MB of multiple tif and Jpeg file resulted in creating 200 MB tif file).

Is there way to prevent the large file size when JPEG file comes?

Code used:

List<BufferedImage> bufferedImageList = new ArrayList<>();
for (String page : pages) {
BufferedImage bufferedImage = ImageIO.read(file);
    bufferedImageList.add(bufferedImage);
}

String filename = "D:\home\example.tif";
ImageWriter writer = ImageIO.getImageWritersByFormatName("TIF").next();

try (ImageOutputStream output = ImageIO.createImageOutputStream(new File(filename))) {
    writer.setOutput(output);

    ImageWriteParam params = writer.getDefaultWriteParam();
    params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

    params.setCompressionType("LZW");
    params.setCompressionQuality(1.0f);

    writer.prepareWriteSequence(null);

    for (BufferedImage image : bufferedImageList) {
        writer.writeToSequence(new IIOImage(image, null, null), params);
    }
    
    
    writer.endWriteSequence();
    
}

writer.dispose();

解决方案

As @Henry wrote in the comments, using LZW (or any other lossless) compression will not achieve the same compression as lossy JPEG (for "natural" images). Going this route will in most cases result in larger files, but with the highest quality possible. In many cases, this will be acceptable.

An alternative is to use JPEG compression in the TIFF files as well, by specifying compressionType "JPEG". You probably also need to set the compressionQuality to something lower (I believe 0.7f is the default for JPEG in ImageIO), to get more reasonable file sizes.

However, using JPEG compression to recompress images that was already JPEG compressed, will introduce "generational loss", as JPEGs typically cannot be perfectly reconstructed. It's technically possible to keep this quality loss at a minimum, by recompressing with the same tables as the original, but this is hard to achieve in practice, due to minor rounding errors in encoders/decoders (ie. your best bet is using the same encoder that wrote the original, with the exact same parameters).

A third option is to use/create a special purpose TIFF utility, that can store the JPEG streams as-is inside the new TIFF container. These files may not be super-efficient TIFFs, as they won't support strips/tiles etc, and some non-standard inputs may still require re-writing to produce valid TIFFs. This will be a bit more work and require some in-depth knowledge about the TIFF format, but certainly doable.

这篇关于将多个tif和Jpeg合并为单个tif文件,该文件的大小很大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆