2014-03-31 106 views
1

我有以下代码来操作和压缩TIFF图像。LZW压缩方法不使用imagick压缩TIFF图像

<?php 

try{ 
    $imagesrc = "C:\\server\\www\\imagick\\src.tif"; 
    $imagedestination = "C:\\server\\www\\imagick\\converted.tif"; 
    $im=new Imagick(); 
    $im->readImage($imagesrc); //read image for manipulation 
    $im->setImageColorSpace(Imagick::COLORSPACE_CMYK); 

    $im->setImageDepth(8); //8 Bit 

    $im->setImageResolution(300,300); //set output resolution to 300 dpi 
    $im->setImageUnits(1); //0=undefined, 1=pixelsperInch, 2=PixelsPerCentimeter 
    $im->setImageCompression(Imagick::COMPRESSION_LZW); 
    $im->setImageCompressionQuality(80); 
    $im->writeImage($imagedestination); 
    $im->clear(); 
    $im->destroy(); 
    $im=NULL; 
}catch(ImagickException $e){ 
    echo "Could not convert image - ".$e->getMessage(); 
} 
?> 

源图像是19MB。当我使用此代码时,生成的TIF图像大约为25MB。也就是说,代码根本不压缩图像。此外,其它的压缩方法没有效果上得到的TIFF文件,但然而如果我使用的压缩方法Imagick::COMPRESSION_JPEG,得到的图像是2MB

我不能使用JPEG压缩,因为我米使用所得TIFF图像与itextsharp在PDF中嵌入。 JPEG压缩会导致itextsharp不支持的多条带tiff图像。

所以我的问题有两种可能的答案。而且其中的任何一个答案都适用于我

  1. 如何有效压缩tif?
  2. 如何将多条带tif图像转换为单条。

感谢

+0

'源图像是19MB' - 这听起来像一个相当大的图像,因为COMPRESSION_LZW/q = 0.8会生成更大的文件。图像的尺寸是给定的,不能改变? – VolkerK

+0

你的图片的尺寸是多少? –

+0

@VolkerK,我们可以在质量上妥协,但不能在图像维度上妥协。图片尺寸为3655x3735像素。源图片尺寸和尺寸可能因用户上传的内容而异。 – WatsMyName

回答

1

用php-imagick摆弄让我无处,所以我尝试Magick.NET
只有通过设置rows-per-strip定义的数字大于图像中的行(即#strips = 1),iTextSharp才会使用CompressionMethod.JPEG接受图像。
但它仍然无法正常工作。我在计算机上的所有图像查看器都能正确呈现图像,但在PDF文档中已损坏。
而我发现这个论坛条目http://itext-general.2136553.n4.nabble.com/TIFF-with-color-pages-COMPRESS-JPEG-problem-td3686051.html

Jpeg压缩tiff图像并不真正支持iText,它们可能工作,但很可能不是。
不知道Paulo Soares-3的权限是多么的权威,但我放弃了。
因此:这不是一个答案。但也许你想用.NET端口拨弄一样,所以这里是我的测试代码 - 好运:

using iTextSharp.text; 
using iTextSharp.text.pdf; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace imagick_itext_test 
{ 
    class Program 
    { 
     static Image getNormalizedImage(string path) 
     { 
      Image rv; 
      using (MemoryStream mems = new MemoryStream()) 
      { 
       using (ImageMagick.MagickImage image = new ImageMagick.MagickImage(path)) 
       { 
        image.Format = ImageMagick.MagickFormat.Tiff; 
        image.ResolutionUnits = ImageMagick.Resolution.PixelsPerInch; 
        image.Depth = 300; 
        image.BitDepth(8); // for printing you said? ;-) 
        image.Adjoin = false; // is there multi-image in jpeg anyway? 
        image.Interlace = ImageMagick.Interlace.Jpeg; // try Interlace.Plane and Interlace.No 
        image.CompressionMethod = ImageMagick.CompressionMethod.JPEG; // everything's fine when using .LZW here 
        image.Quality = 35; // 85, 80 not even 50 got me significant reduction in file size (src size=18MB) 
        //image.SetDefine(ImageMagick.MagickFormat.Tiff, "rows-per-strip", image.Height.ToString()); 
        image.SetDefine(ImageMagick.MagickFormat.Tiff, "rows-per-strip", "8192"); 
        image.Strip(); // remove additional data, e.g. comments 
        image.Write(mems); 
       } 

       // store the tiff(jpeg) image for inspection 
       using (FileStream fos = new FileStream(@"c:\temp\so_conv.tiff", FileMode.Create)) 
       { 
        mems.Position = 0; 
        mems.CopyTo(fos); 
       } 
       mems.Position = 0; 
       rv = Image.GetInstance(mems); 
       //rv.ScalePercent(24f); // works for me ... 
      } 
      return rv; 
     } 

     static void Main(string[] args) 
     { 
      using (Document doc = new Document()) 
      { 
       using (PdfWriter w = PdfWriter.GetInstance(doc, new FileStream(@"c:\temp\so_pdf_test.pdf", FileMode.Create))) 
       { 
        doc.Open(); 
        doc.SetMargins(50, 50, 50, 50); 
        doc.Add(new Paragraph("SO Image Test")); 
        doc.Add(getNormalizedImage(@"c:\temp\src.tif")); 
        doc.Close(); 
       } 
      } 
     } 
    } 
} 

VS2012 - .NET 4.5,ImageMagick的-6.8.8-10-Q16-64静电。 EXE
两个Magick.NET和iTextSharp的已经通过的NuGet添加到项目:

  • iTextSharp的5.50
  • Magick.NET-Q16-64 6.8.8.901
0

我得从什么在TIFF =中最好省略JPEG,是最小的。 然后是ZIP压缩,然后是LZW,然后是RLE。

**输入文件:jpeg 500kb。在TIFF 1.25MB * JPEG * ZIP 2.0MB * LZW 2.5MB * LRE 3.2MB

一件事 - 为TIFF压缩不要设置质量,因为它是loseless格式 - 它只是它(套忽略到100数数)。您可以将它设置为tiff的jpeg压缩 - 没有其他。

但是你可以做的是在保存之前添加行$im->stripImage();。这会从文件中去除一些信息 - 可能会使它变小。

另请检查您的Imagick版本我是:ImageMagick 6.7.7-7 2012-06-21 Q16,它运作良好。