Image processing 调整图像大小
我在这里使用Go resize软件包:Image processing 调整图像大小,image-processing,go,Image Processing,Go,我在这里使用Go resize软件包: 我正在从S3中提取一个图像,如下所示: image_data, err := mybucket.Get(key) // this gives me data []byte 之后,我需要调整图像的大小: new_image := resize.Resize(160, 0, original_image, resize.Lanczos3) // problem is that the original_image has to be of type ima
image_data, err := mybucket.Get(key)
// this gives me data []byte
new_image := resize.Resize(160, 0, original_image, resize.Lanczos3)
// problem is that the original_image has to be of type image.Image
err : = mybucket.Put('newpath', new_image, 'image/jpg', 'aclstring')
// problem is that new image needs to be data []byte
[]字节
转换为-->图像。图像
并返回到-->数据[]字节
?读取
想做吗?试试神奇吧:
这将调整大小并很好地将保存结果裁剪到文件:
vipsthumbnail original.jpg -s 700x200 -o 700x200.jpg -c
从Go打来的电话:
func resizeExternally(from string, to string, width uint, height uint) error {
var args = []string{
"--size", strconv.FormatUint(uint64(width), 10) + "x" +
strconv.FormatUint(uint64(height), 10),
"--output", to,
"--crop",
from,
}
path, err := exec.LookPath("vipsthumbnail")
if err != nil {
return err
}
cmd := exec.Command(path, args...)
return cmd.Run()
}
您可以使用,它由(一个用C编写的快速图像处理库)提供支持
如果您正在寻找一种图像大小调整解决方案作为一项服务,请查看操作使用的是特定的库/包,但我认为“调整图像大小”问题可以在没有该包的情况下解决 您可以使用
golang.org/x/image/draw
调整de image的大小:
input, _ := os.Open("your_image.png")
defer input.Close()
output, _ := os.Create("your_image_resized.png")
defer output.Close()
// Decode the image (from PNG to image.Image):
src, _ := png.Decode(input)
// Set the expected size that you want:
dst := image.NewRGBA(image.Rect(0, 0, src.Bounds().Max.X/2, src.Bounds().Max.Y/2))
// Resize:
draw.NearestNeighbor.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil)
// Encode to `output`:
png.Encode(output, dst)
在这种情况下,我选择了draw.NearestNeighbor,因为它速度更快,但看起来更糟。但还有其他方法,您可以在上看到:
draw.NearestNeighbor
NearestNeighbor是最近邻插值器。它非常快,但通常给出的结果质量很低。放大时,结果将看起来“块状”draw.approxblinear
近似线性是最近邻插值和双线性插值的混合。它是快速的,但通常会给出中等质量的结果绘制双线性图
双线性是帐篷的核心。这是缓慢的,但通常会给出高质量的结果draw.CatmullRom
CatmullRom是CatmullRom内核。它非常慢,但通常会给出非常高质量的结果
jpeg.Encode
bytes
是标准库中的一个包。如果您知道输入图像是jpeg,则可以执行jpeg.Decode
(而不是image.Decode
)从何处导入此调整大小
包?如果不需要系统调用,还有一个问题。观察一下这种方法是否使用了可移植性是很有用的。使用纯Go可以为Go支持的每个OS/arch提供可移植性。@joonas.fi,建议使用这个库吗?@gbenroscience oops我有一个输入错误,我的意思是“失去可移植性”。我的意思是,由于它调用的是一个外部程序,所以你的围棋程序并不像纯围棋所能提供的那样独立和可移植。接受答案中的代码在这方面更好。这个答案与原始问题有什么关系?这很好,谢谢!工作起来很有魅力。
func resizeExternally(from string, to string, width uint, height uint) error {
var args = []string{
"--size", strconv.FormatUint(uint64(width), 10) + "x" +
strconv.FormatUint(uint64(height), 10),
"--output", to,
"--crop",
from,
}
path, err := exec.LookPath("vipsthumbnail")
if err != nil {
return err
}
cmd := exec.Command(path, args...)
return cmd.Run()
}
input, _ := os.Open("your_image.png")
defer input.Close()
output, _ := os.Create("your_image_resized.png")
defer output.Close()
// Decode the image (from PNG to image.Image):
src, _ := png.Decode(input)
// Set the expected size that you want:
dst := image.NewRGBA(image.Rect(0, 0, src.Bounds().Max.X/2, src.Bounds().Max.Y/2))
// Resize:
draw.NearestNeighbor.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil)
// Encode to `output`:
png.Encode(output, dst)