美文网首页golang从零起步
golang使用nfnt缩放图片

golang使用nfnt缩放图片

作者: 次序 | 来源:发表于2019-01-27 20:17 被阅读0次

nfnt地址 https://github.com/nfnt/resize

package main

import (
    "fmt"
    "github.com/nfnt/resize"
    "image/jpeg"
    "log"
    "os"
)

func main() {
    // open "test.jpg"
    file, err := os.Open("C:\\Users\\cixu\\Desktop\\1\\5.jpg")
    if err != nil {
        log.Fatal(err)
    }

    // decode jpeg into image.Image
    img, err := jpeg.Decode(file)
    if err != nil {
        log.Fatal(err)
    }
    file.Close()

    // resize to width 1000 using Lanczos resampling
    // and preserve aspect ratio

    kuan:=img.Bounds().Dx()// 
    gao:=img.Bounds().Dy()//
    fmt.Println(kuan)
    fmt.Println(gao)

    m := resize.Resize(uint(kuan), 0, img, resize.Lanczos3)
    //m := resize.Resize(1000, 0, img, resize.Lanczos3)

    out, err := os.Create("C:\\Users\\cixu\\Desktop\\1\\5-2.jpg")
    if err != nil {
        log.Fatal(err)
    }
    defer out.Close()

    // write new image to file
    jpeg.Encode(out, m, nil)
    fmt.Println("------------------")
}

相关文章

网友评论

    本文标题:golang使用nfnt缩放图片

    本文链接:https://www.haomeiwen.com/subject/wgynjqtx.html