PDF文件的视差,以确定像素完美性 [英] Visual diff of PDF files in order to determine pixel perfectness

查看:162
本文介绍了PDF文件的视差,以确定像素完美性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要重构一些报告(使用Jasper生成)使用MS Reporting Services。原始报告的副本以PDF格式提供。要求是让新的报告像素完美,这是非常繁琐...

I need to refactor some reports (generated with Jasper) using MS Reporting Services. Copies of original reports are available in PDF. The requirement is to make the new reports "pixel perfect" which is very cumbersome...

为了使生活更容易我想要一个工具,覆盖原始和

For making life easier I would like to have a tool which overlays the original and generated report PDFs in order to measure if they are pixel perfect or not.

是否有这样的工具?

推荐答案

最简单,可立即使用的方法:使用 ImageMagick的比较 (也可在Windows上使用)。

The most simple, immediately available method to do this: use ImageMagick's compare (which is also available on Windows).

页面(虽然它使用Ghostscript作为其委托首先将PDF页面呈现为像素图像):

It can even compare PDF pages (though it uses Ghostscript as its delegate to render the PDF pages to pixel images first):

 compare.exe         ^
    tested.pdf[0]    ^
    reference.pdf[0] ^
   -compose src      ^
    delta.pdf

结果 delta.pdf 会将每个像素显示为红色,之间的两个比较的PDF页面。所有相同的像素将是纯白色的。 [0] 告诉比较使用每个文件的第一页进行比较(页计数为零)

The resulting delta.pdf will depict each pixel as red which has a different color between the two compared PDF pages. All identical pixels will be purely white. The [0] tell compare to use the first pages of each file for comparison (page count is zero-based).

您可以使用以下示例查看此操作的效果:

You can see how this works out with the following example:

 compare.exe                      ^
    http://qtrac.eu/boson1.pdf[1] ^
    http://qtrac.eu/boson2.pdf[1] ^
   -compose src                   ^
    delta.pdf

这些是各自的页面(转换为缩小的PNG用于web显示)。参考页在左侧,修改页是中间,delta-pixel-are-red图像在右侧:

Here are the respective pages (converted to scaled-down PNGs for web display). The reference page is on the left, the modified page is the middle one, the 'delta-pixel-are-red' image is on the right:

   

  

通过跳过 -compose src 参数可以获得略微不同的视觉效果。然后你会得到原始文件的像素为灰色阴影背景(上下文),增量像素为红色:

A slightly different visual result you can get by skipping the -compose src parameter. Then you'll get the original file's pixels as a gray-shaded background (for context) with the delta pixels in red:

 compare.exe                      ^
    http://qtrac.eu/boson1.pdf[1] ^
    http://qtrac.eu/boson2.pdf[1] ^
    delta.pdf

   

  

如果您不喜欢像素差异的红色,请使用 -highlight-color

If you don't like the red color for pixel differences, use -highlight-color:

 compare.exe                      ^
    http://qtrac.eu/boson1.pdf[1] ^
    http://qtrac.eu/boson2.pdf[1] ^
   -highlight-color green         ^
    delta.pdf

用于呈现PDF页面的默认分辨率为72 dpi。如果您需要更高的精度,可以使用 -density 参数切换到300 dpi,如下所示:

The default resolution used to render the PDF pages is 72 dpi. Should you need a higher precision, you can switch to 300 dpi using the -density parameter like this:

 compare.exe                      ^
   -density 300                   ^
    http://qtrac.eu/boson1.pdf[1] ^
    http://qtrac.eu/boson2.pdf[1] ^
    delta.pdf

/ strong>切换到更高的密度会减慢过程并创建更大的文件。

Note, switching to higher densities will slow down the process and create bigger files.

您甚至可以为delta图像创建一个* .txt文件,描述每个像素的坐标和相应的颜色值:

You can even create a *.txt file for the delta image which describes each pixel's coordinates and the respective color values:

 compare                          ^
    http://qtrac.eu/boson1.pdf[1] ^
    http://qtrac.eu/boson2.pdf[1] ^
   -compose src                   ^
   -highlight-color black         ^
    delta.txt

然后简单地计算总的黑色像素数(对不起,这是Unix / Linux / MacOSX语法) p>

Then simply count the number of total vs. black pixels (sorry, this is Unix/Linux/MacOSX syntax):

 total_pixels=$(( $(cat delta.txt | wc -l) - 1))
 black_pixels=$(( $(grep black delta.txt | wc -l) -1 ))

在上面的例子中,我得到

In the example used for the illustrations above, I get

 total_pixels=500990
 black_pixels=8727

当然,理想的结果是

 black_pixels=0

这篇关于PDF文件的视差,以确定像素完美性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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