缩放图像似乎会导致模糊

时间:2021-02-25 12:48:13

标签: python image-processing pycairo

我正在为 reportlab.graphics.renderPM 构建一个 pycairo 后端渲染器;我发现与旧的基于 lgpl_libart 的后端模块 _renderPM.c 几乎完全一致

但是,我发现缩放图像的渲染方式有所不同。例如见

https://www.reportlab.com/static/test0.pnghttps://www.reportlab.com/static/test0-cairo.png

下面的图片原尺寸为 110x44,上面的图片缩小到一半。 这是代码片段

d = Drawing(200, 100)
d.add(Rect(1,1,d.width-2,d.height-2,strokeWidth=2,strokeColor=toColor('red'),fillColor=toColor('lightblue')),name='bg0')
def addImage(x,y,w,h):
    img = ImageShape(x, y, w, h, IMAGENAME)
    d.add(Rect(img.x-1,img.y-1,img.width+2,img.height+2,strokeWidth=2,strokeColor=toColor('green'),fillColor=toColor('black')),name='bg1')
    d.add(img)
addImage(40,60,55,22)
addImage(10,10,110,44)
return d

pycairo 代码是一个管道,它在加载了 __fromPIL 的 gif 图像上使用 _aapixbuf

@classmethod
def __fromPIL(cls, im, fmt='RGB24', alpha=1.0, forceAlpha=False):
    if 'A' not in im.getbands() or forceAlpha:
        im.putalpha(int(alpha * 255))
    fmt = cls.__str2format(fmt)
    return cairo.ImageSurface.create_for_data(bytearray(im.tobytes('raw', 'BGRa')),
            fmt, im.width, im.height,
            cairo.ImageSurface.format_stride_for_width(fmt,im.width),
            )


def _aapixbuf(self, x, y, dstW, dstH,
                data, srcW, srcH, planes=3,
                ):
    ctx = self.ctx
    ctx.save()
    ctx.set_antialias(cairo.Antialias.DEFAULT)
    ctx.set_operator(cairo.Operator.OVER)
    ctx.translate(x,y+dstH)
    ctx.scale(dstW/float(srcW),-dstH/float(srcH))
    ctx.set_source_surface(self.__fromPIL(data,self._fmt, forceAlpha=False))
    ctx.paint()
    ctx.restore()

有什么办法可以让缩放后的图像看起来更干净一点?我尝试了抗锯齿和运算符的各种组合,但无法获得与 libart 渲染相同的外观。

0 个答案:

没有答案