dongyi6195
2018-12-17 08:15
浏览 88
已采纳

如何使用API​​平滑光栅化PDF?

I'd like to rasterize a PDF source (in this case to PNG, 500x500 pixels) using these golang bindings for ImageMagick6. On the CLI, I can do this using

convert -density 5000 -define psd:fit-page=500x test.pdf -resize 500x test.png

which results in a smoothly rendered image.

What I'm failing to do right now is to produce something similar using the API: either the resulting image has scaled pixels or is blurry and has a size 500x500 pixels, or it's in the "original" size.

Here's a minimum snippet of my playground code:

package main

import "gopkg.in/gographics/imagick.v2/imagick"

func main() {
    imagick.Initialize()
    defer imagick.Terminate()
    mw := imagick.NewMagickWand()
    defer mw.Destroy()

    mw.SetImageResolution(5000,5000)
    mw.SetOption("psd:fit-page", "500x")

    mw.ReadImage("test.pdf")
    mw.ResizeImage(500, 500, imagick.FILTER_POINT, 1)
    mw.SetIteratorIndex(0) // This being the page offset
    mw.SetImageFormat("png")
    mw.WriteImage("test.png")
}

I got confused by density, image size, image resolution and canvas size I guess. How is it meant to be used?

图片转代码服务由CSDN问答提供 功能建议

我想使用用于ImageMagick6的这些golang绑定。 在CLI上,我可以使用

  convert -density 5000 -define psd:fit-page = 500x test.pdf -resize 500x test.png 
   
 
 

这会产生平滑渲染的图像。

我现在无法做的是使用API​​生成类似的东西: 生成的图像已缩放像素或模糊,尺寸为500x500像素,或为“原始”尺寸。

以下是我的游乐场代码的最小片段: \ n

 包main 
 
import“ gopkg.in/gographics/imagick.v2/imagick"

func main(){
 imagick.Initialize()
延迟imagick.Terminate(  )
 mw:= imagick.NewMagickWand()
延迟mw.Destroy()
 
 mw.SetImageResolution(5000,5000)
 mw.SetOption(“ psd:fit-page”,“ 500x”)\  n 
 mw.ReadImage(“ test.pdf”)
 mw.ResizeImage(500,500,imagick.FILTER_POINT,1)
 mw.SetIteratorIndex(0)//这是页面偏移量
 mw.SetImageFormat(  “ png”)
 mw.WriteImage(“ test.png”)
} 
   
 
 

我知道了 我猜是浓度图像尺寸图像分辨率画布尺寸使用的。 是什么意思?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxuan4556 2019-01-31 10:54
    已采纳

    Your GO version of the convert command is missing the "density" argument. Replacing the call to SetImageResolution with one to SetOption and using a filter to smooth out the edges results in a smooth image:

    package main
    
    import "gopkg.in/gographics/imagick.v2/imagick"
    
    func main() {
        imagick.Initialize()
        defer imagick.Terminate()
        mw := imagick.NewMagickWand()
        defer mw.Destroy()
    
        mw.SetOption("density", "5000")
        mw.SetOption("psd:fit-page", "500x")
    
        mw.ReadImage("test.pdf")
    
        mw.ResizeImage(500, 500, imagick.FILTER_LAGRANGE, 1)
        mw.SetIteratorIndex(0) // This being the page offset
        mw.SetImageFormat("png")
        mw.WriteImage("test.png")
    }
    
    点赞 评论

相关推荐 更多相似问题