如何解决生成WebP文件格式的缩略图
以下代码是使用github.com/disintegration/imaging
包创建缩略图
对于jpeg之类的典型图像格式,效果很好,但是对于webp
文件格式,效果不佳。
下面的代码出现了错误imaging: unsupported image format
。
是否有更健壮的方法来生成典型图像类型(jpg,gif,tiff,bmp等)和webp的缩略图?
package main
import (
"bytes"
"fmt"
"github.com/disintegration/imaging"
"github.com/chai2010/webp"
"io/ioutil"
)
//https://stackoverflow.com/questions/8340751/webp-encoder-decoder-in-go
func main() {
//img,_ := imaging.Open("ml/input/apple.jpg")
// Load webp
data,_ := ioutil.ReadFile("ml/input/waterski2.webp")
// Decode webp
img,_ := webp.Decode(bytes.NewReader(data))
//Create thumbnail
dstImage := imaging.Thumbnail(img,400,imaging.Lanczos)
err1:=imaging.Save(dstImage,"ml/output/waterski2.webp")
if err1!=nil{
fmt.Println(err1)
}
}
解决方法
我知道它最终可以与webp
Save
方法一起使用。
package main
import (
"bytes"
"github.com/chai2010/webp"
"github.com/disintegration/imaging"
"io/ioutil"
)
//https://stackoverflow.com/questions/8340751/webp-encoder-decoder-in-go
func main() {
//img,_ := imaging.Open("ml/input/apple.jpg")
// Load webp
data,_ := ioutil.ReadFile("ml/input/waterski2.webp")
// Decode webp
img,_ := webp.Decode(bytes.NewReader(data))
//Create thumbnail
dstImage := imaging.Thumbnail(img,400,imaging.Lanczos)
webp.Save("ml/output/waterski2.webp",dstImage,&webp.Options{})
//err1:=imaging.Save(dstImage,"ml/output/waterski2.webp")
//if err1!=nil{
// fmt.Println(err1)
//}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。