【问题标题】:Generating thumbnails for webp file format为 webp 文件格式生成缩略图
【发布时间】:2020-09-30 22:56:30
【问题描述】:

以下代码是使用github.com/disintegration/imaging包创建缩略图 适用于 jpeg 等典型图像格式,但不适用于 webp 文件格式。

我收到 errorimaging: 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.Decode 方法返回一个image.Image,这正是imaging.Thumbnail 函数所需要的。\

标签: go webp


【解决方案1】:

我发现它最终可以使用 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)
    //}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-05
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 2011-02-19
    • 1970-01-01
    相关资源
    最近更新 更多