The second package of Golang simple image processing library: picture cropping

The second package of Golang simple image processing library: picture cropping

One. Features

  1. Image processing library official at the Go language
  2. Jpg and png images to be scaled / cropped library after simple package

two. Instructions for use

1. First, download

go get -v -u github.com/hunterhug/go_image

2. main function

For scaling the like to the width, the first parameter is an image file, the second parameter is the path to save the file after scaling

err := ScaleF2F(filename, savepath, width)

Width and height, etc. according to scaling, a first parameter is an image file, the second parameter is the path to save the file after scaling

err = ThumbnailF2F(filename, save1path, width, height)

True file type detection image file and return the real file name for the image file location parameter

realfilename, err := RealImageName(savepath)

Change the image file name, the first parameter is the name of the original file, the second parameter is the renamed file name, and the third parameter indicates whether to force renamed

err=ChangeImageName(savepath,realfilename,false)

three. Examples of Use

3.1.example.go

    package main
    
    import (
        "fmt"
        . "github.com/hunterhug/go_image/go_image"
    )
    
    //将某一图片文件进行缩放后存入另外的文件中
    func main() {
        //打印当前文件夹位置
        fmt.Printf("本文件文件夹位置:%s\n", CurDir())
    
        //图像位置
        filename := "./testdata/gopher.png"
    
        //保存位置
        savepath := "./testdata/gopher400.jpg"
        save1path := "./testdata/gopher400*400.png"
    
        //宽度,高度
        width := 200
        height := 400
    
        //按照宽度进行等比例缩放
        err := ScaleF2F(filename, savepath, width)
        if err != nil {
            fmt.Printf("%s\n", err.Error())
        }else{
            fmt.Printf("生成按宽度缩放图%s\n",savepath)
        }
    
        //按照宽度和高度进行等比例缩放
        err = ThumbnailF2F(filename, save1path, width, height)
        if err != nil {
            fmt.Printf("%s\n", err.Error())
        }else{
            fmt.Printf("生成按宽度高度缩放图%s\n",save1path)
        }
    
        //查看图像文件的真正名字
        //如 ./testdata/gopher400.jpg其实是png类型,但是命名错误,需要纠正!
        realfilename, err := RealImageName(savepath)
        if err != nil {
            fmt.Printf("%s\n", err.Error())
        } else {
            fmt.Printf("真正的文件名:%s\n", realfilename)
        }
    
        //文件改名,不强制性
        err=ChangeImageName(savepath,realfilename,false)
        if err!=nil{
            fmt.Printf("文件改名失败:%s%s",realfilename,err.Error())
        }
    
        //文件改名,强制性
        err=ChangeImageName(savepath,realfilename,true)
        if err!=nil{
            fmt.Printf("文件改名失败:%s%s",realfilename,err.Error())
        }else{
            fmt.Println("改名成功")
        }
    }

3.2 Results

本文件文件夹位置:/home/silly/golang/pikapika/src/github.com/hunterhug/go_image
生成按宽度缩放图./testdata/gopher400.jpg
生成按宽度高度缩放图./testdata/gopher400*400.png
真正的文件名:./testdata/gopher400.png
文件改名失败:./testdata/gopher400.png文件已经存在

Original Image

Scaling cropping width 200px

Guess you like

Origin www.cnblogs.com/nima/p/11751334.html