使用libvips / golang调整动画.webp的大小

huangapple go评论95阅读模式
英文:

Resize animated .webp using libvips / golang

问题

我有一个Go程序,我想要对一个动画的.webp图像进行调整大小,然后再写入.webp格式。

使用imagemagick,这个方法完美地工作:

exec.Command("magick", tmpWebPFile, "-quality", "70", "-resize", resizeStr, "-layers", "coalesce", outputFile)

为了提高一些速度,我决定尝试使用libvips:

vips webpsave animated.webp out.webp

但是只有第一帧被输出了。

我尝试使用github.com/davidbyttow/govips包:

image, _ := vips.NewImageFromFile(tmpWebPFile)
data, _, _ := image.Export(vips.NewDefaultWEBPExportParams())
os.WriteFile("test.webp", data, 0644)

仍然只有第一帧。

有什么建议吗?

英文:

I have a Go program where I want to take an animated .webp image, resize it and then write it to .webp again.

This worked flawlessly using imagemagick:

exec.Command("magick", tmpWebPFile, "-quality", "70", "-resize", resizeStr, "-layers", "coalesce", outputFile)

In order to gain some speed, I decided to try with libvisp:

vips webpsave animated.webp out.webp

Only thing that comes out is the first frame.

I tried using the github.com/davidbyttow/govips package:

image, _ := vips.NewImageFromFile(tmpWebPFile)
data, _, _ := image.Export(vips.NewDefaultWEBPExportParams())
os.WriteFile("test.webp", data, 0644)

Still just the first frame.

Any suggestions?

答案1

得分: 1

啊,这个方法起作用了:

params := vips.NewImportParams()
params.NumPages.Set(-1)
image, _ := vips.LoadImageFromFile(tmpWebPFile, params)
image.ThumbnailWithSize(width, newHeight, vips.InterestingAll, vips.SizeDown)
data, _, _ := image.ExportWebp(&vips.WebpExportParams{
	Quality:       70,
})
os.WriteFile("out.webp", data, 0644)
英文:

Ah, this did the trick:

params := vips.NewImportParams()
params.NumPages.Set(-1)
image, _ := vips.LoadImageFromFile(tmpWebPFile, params)
image.ThumbnailWithSize(width, newHeight, vips.InterestingAll, vips.SizeDown)
data, _, _ := image.ExportWebp(&vips.WebpExportParams{
	Quality:       70,
})
os.WriteFile("out.webp", data, 0644)

huangapple
  • 本文由 发表于 2023年6月3日 04:29:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76393304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定