SoFunction
Updated on 2025-03-05

Basic operation of golang image processing library image

Basic Operation

Basic reading and saving of pictures.

Read

Image reading is similar to file reading, you need to get the stream first:

  • The decoder for registering pictures (such as: jpgimport _ "image/jpeg", pngimport _ "image/png"
  • passOpen the file to get the stream;
  • passDecode the stream and get the picture;
import _ "image/jpeg"
func readPic()  {
    f, err := ("C:\\")
    if err != nil {
        panic(err)
    }
    defer ()

    img, fmtName, err := (f)
    if err != nil {
        panic(err)
    }
    ("Name: %v, Bounds: %+v, Color: %+v", fmtName, (), ())

    return img
}

The first parameter returned after decoding is the Image interface:

type Image interface {
  ColorModel()  // Return to the color model of the picture  Bounds() Rectangle       // Return to the picture frame  At(x, y int)  // Return the color of (x,y) pixels}

New

Create a new picture is very simple, justYou can create a transparent background image

img := ((0, 0, 300, 300))

save

Saving the image is also very simple. After encoding, you can write to the file stream:

  • Decoder for registering pictures
  • passCreate a file;
  • PassEncode pictures and write files;
func savePic(img *) {
    f, err := ("C:\\")
    if err != nil {
        panic(err)
    }
    defer ()
    b := (f)
    err = (b, img, nil)
    if err != nil {
        panic(err)
    }
    ()
}

Image modification

Many operations require drawing pictures:

func Draw(dst Image, r , src , sp , op Op)
func DrawMask(dst Image, r , src , sp , mask , mp , op Op)​

Main parameters description:

  • dst: background image of drawing
  • r: Drawing area of ​​background image
  • src: The diagram to be drawn
  • sp: To draw the start point of the graph src
  • op: combination method

DrawMask has an additional mask mask parameter, and Draw is a special form (the mask-related parameter is nil).

Convert

The jpg image you read is not in RGBA format (in YCbCr format); you need to convert the format before the operation:

  • Create an RGBA image of the same size;
  • Draw jpg onto the newly created image;
func jpg2RGBA(img ) * {
    tmp := (())

    (tmp, (), img, ().Min, )
    return tmp
}

Crop

The subImage method can be used to easily crop images (need to be in RGBA format)

func subImg() {
    pic := readPic()
    ("Type: %T\n", pic)
    img := jpg2RCBA(pic)

    sub := ((0, 0, ().Dx(), ().Dy()/2))
    savePic(sub.(*))
}

Zoom

Image scaling is divided into scaling that maintains the scale or not; when maintaining the scale, you must determine the location of the new picture (whether it is centered) and how to fill in the blanks.
To scale, new libraries need to be introduced/x/image/draw

When maintaining scaling, you need to calculate the size of the zoomed image first:

  • Calculate the scaling ratios of width and height respectively, whichever is the smallest;
  • If it is centered (otherwise it is on the upper left), you need to calculate the fill size and then calculate the position based on this;
func calcResizedRect(width int, src , height int, centerAlign bool)  {
    var dst 
    if width*() < height*() { // width/ < height/
        ratio := float64(width) / float64(())

        tH := int(float64(()) * ratio)
        pad := 0
        if centerAlign {
            pad = (height - tH) / 2
        }
        dst = (0, pad, width, pad+tH)
    } else {
        ratio := float64(height) / float64(())
        tW := int(float64(()) * ratio)
        pad := 0
        if centerAlign {
            pad = (width - tW) / 2
        }
        dst = (pad, 0, pad+tW, height)
    }

    return dst
}

With the scaled size, you can scale the picture by bilinear interpolation bilinear

  • img is the image width and height to be scaled as the size after scaling
  • keepRatio is whether to maintain scale
  • fill is the color of fill (R, G, and B are all fill)
  • centerAlign: Whether the image is stored in the center when maintaining scale
import (
    "image"
    "image/color"

    "/x/image/draw"
)

func resizePic(img , width int, height int, keepRatio bool, fill int, centerAlign bool)  {
    outImg := ((0, 0, width, height))
    if !keepRatio {
        (outImg, (), img, (), , nil)
        return outImg
    }

    if fill != 0 {
        fillColor := {R: uint8(fill), G: uint8(fill), B: uint8(fill), A: 255}
        (outImg, (), &{C: fillColor}, {}, )
    }
    dst := calcResizedRect(width, (), height, centerAlign)
    (outImg, (), img, (), , nil)
    return outImg
}

This is the article about the introduction of golang image processing library image. For more related golang image processing library image content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!