Go Actual Warfare - Goan implements thumbnail generation server disintegration/imaging, nfnt/resize

Keywords: github Google Programming Go

Life goes on, go on~~~~

Today, do something about image processing, and learn to share with you.

A long time ago, the standard library of pictures provided by golang was introduced.
Image, image/color, image/png, image/jpeg package for Go language learning (the way to go)

When you search on google or Baidu, you will find many references to graphics-go/graphics, but the library does not know why, the official seems to no longer provide, it is difficult to find.

That's OK. We still have github-oriented programming!!!

Take a look at the previous code:
Generate pictures

package main

import "image"
import "image/color"
import "image/png"
import "os"

func main() {
    // Create an 100 x 50 image
    img := image.NewRGBA(image.Rect(0, 0, 100, 50))

    // Draw a red dot at (2, 3)
    img.Set(2, 3, color.RGBA{255, 0, 0, 255})

    // Save to out.png
    f, _ := os.OpenFile("out.png", os.O_WRONLY|os.O_CREATE, 0600)
    defer f.Close()
    png.Encode(f, img)
}

disintegration/imaging

github address:
https://github.com/disintegration/imaging

Star: 1284

Obtain:
go get -u github.com/disintegration/imaging

thumb
The following code generates a thumbnail of three pictures.
Come from:
https://www.socketloop.com/tutorials/golang-generate-thumbnails-from-images

package main

import (
    "image"
    "image/color"
    "runtime"

    "github.com/disintegration/imaging"
)

func main() {
    // use all CPU cores for maximum performance
    runtime.GOMAXPROCS(runtime.NumCPU())

    // input files
    files := []string{"1.jpg", "2.jpg", "3.jpg"}

    // load images and make 100x100 thumbnails of them
    var thumbnails []image.Image
    for _, file := range files {
        img, err := imaging.Open(file)
        if err != nil {
            panic(err)
        }
        thumb := imaging.Thumbnail(img, 100, 100, imaging.CatmullRom)
        thumbnails = append(thumbnails, thumb)
    }

    // create a new blank image
    dst := imaging.New(100*len(thumbnails), 100, color.NRGBA{0, 0, 0, 0})

    // paste thumbnails into the new image side by side
    for i, thumb := range thumbnails {
        dst = imaging.Paste(dst, thumb, image.Pt(i*100, 0))
    }

    // save the combined image to file
    err := imaging.Save(dst, "dst.jpg")
    if err != nil {
        panic(err)
    }
}

Generating thumbnail service
Next, thumbnails of different sizes are generated according to the url provided by the user.

The net/http in golang can be referred to as follows:
The way to Go

package main

import (
    "fmt"
    "image"
    "image/color"
    "image/png"
    "net/http"
    "runtime"
    "strconv"
    "strings"

    "github.com/disintegration/imaging"
)

func main() {
    // use all CPU cores for maximum performance
    runtime.GOMAXPROCS(runtime.NumCPU())
    http.HandleFunc("/", doImageHandler)
    http.ListenAndServe("localhost:8080", nil)
}
func doImageHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("%q\n", strings.Split(r.URL.Path, "/"))
    url := strings.Split(r.URL.Path, "/")
    if len(url) != 3 {
        return
    }

    thumbnails_size := strings.Split(url[2], "_")
    if len(thumbnails_size) != 2 {
        return
    }
    thumbnails_width, _ := strconv.Atoi(thumbnails_size[0])
    thumbnails_height, _ := strconv.Atoi(thumbnails_size[1])

    img, err := imaging.Open(url[1])
    if err != nil {
        panic(err)
    }
    thumb := imaging.Thumbnail(img, thumbnails_width, thumbnails_height, imaging.CatmullRom)
    dst := imaging.New(thumbnails_width, thumbnails_height, color.NRGBA{0, 0, 0, 0})
    dst = imaging.Paste(dst, thumb, image.Pt(0, 0))

    if err != nil {
        panic(err)
    }
    header := w.Header()
    header.Add("Content-Type", "image/jpeg")

    png.Encode(w, dst)
}

For example, browser input: http://localhost:8080/1.jpg/80_90
A thumbnail of 1.jpg of 80*90 is generated.

nfnt/resize

github address:
https://github.com/nfnt/resize

Star: 1544

Obtain:
go get github.com/nfnt/resize

Scale-up and zoom-in pictures

package main

import (
    "image/jpeg"
    "log"
    "os"

    "github.com/nfnt/resize"
)

func main() {
    file, err := os.Open("1.jpg")
    if err != nil {
        log.Fatal(err)
    }

    img, err := jpeg.Decode(file)
    if err != nil {
        log.Fatal(err)
    }
    file.Close()

    // resize to width 1000 using Lanczos resampling
    // and preserve aspect ratio
    m := resize.Resize(100, 0, img, resize.Lanczos3)

    out, err := os.Create("test_resized.jpg")
    if err != nil {
        log.Fatal(err)
    }
    defer out.Close()

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

Posted by stevieontario on Thu, 07 Feb 2019 15:36:17 -0800