go - 为 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, 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, 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)
//}
}
推荐阅读
- redis - 有没有办法自动取消订阅 Redis 中的频道?
- c - 使用 fscanf 在 C 中逐行读取文本文件
- form-recognizer - 获取分析表单 API 返回错误 3003
- java - 将时间戳从 db 和 Timezone 列转换为 RFC 3999 格式
- powershell - 在 Powershell Scriptblock 中调用 Restmethod URI 不起作用
- google-api - 如何将谷歌文档复制到谷歌驱动服务中的另一个文件夹
- shell - 关于在 vim 命令模式下使用 shell 管道的困惑
- java - Microsoft 图形搜索功能 Java
- c - pthread_join 中的 retval 函数
- r - Edit Windows environmental variable