1 Star 1 Fork 1

xiaoyutab / xgotool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
open_image.go 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
package xfile
import (
"errors"
"image"
"image/gif"
"image/jpeg"
"image/png"
"os"
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
"golang.org/x/image/vp8l"
"golang.org/x/image/webp"
)
// 打开图片文件
func OpenImage(file string) (image.Image, error) {
if fi, err := os.Stat(file); err != nil {
return nil, err
} else if fi.IsDir() {
return nil, errors.New("传入的路径为目录,请调整后再次重试")
}
// 打开文件
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
tmp, err := jpeg.Decode(f)
if err != nil {
tmp, err = png.Decode(f)
if err != nil {
tmp, err = gif.Decode(f)
if err != nil {
tmp, err = webp.Decode(f)
if err != nil {
tmp, err = bmp.Decode(f)
if err != nil {
tmp, err = tiff.Decode(f)
if err != nil {
tmp, err = vp8l.Decode(f)
if err != nil {
return nil, errors.New("使用jpeg、png、gif、webp、bmp、tiff、vp8l进行图像解码均失败")
}
}
}
}
}
}
}
return tmp, err
}
Go
1
https://gitee.com/xiaoyutab/xgotool.git
git@gitee.com:xiaoyutab/xgotool.git
xiaoyutab
xgotool
xgotool
v0.3.11

搜索帮助

53164aa7 5694891 3bd8fe86 5694891