Loading and saving image

suggest change

In memory, an image can be seen as a matrix of pixels (colors). However, when an image is stored in permanent storage, it may be stored as is (RAW format), as a bitmap, or in another image format with a compression algorithm that saves storage space, for example PNG, JPEG, or GIF. When loading an image in a particular format, the image must be decoded to image.Image with the corresponding algorithm. The image.Decode function, declared as

func Decode(r io.Reader) (Image, string, error)

is provided for this particular usage. In order to be able to handle various image formats, prior to calling the image.Decode function, the decoder must be registered through image.RegisterFormat function defined as

func RegisterFormat(name, magic string,
    decode func(io.Reader) (Image, error), decodeConfig func(io.Reader) (Config, error))

Currently, the image package supports three file formats: JPEG, GIF and PNG. To register a decoder, add the following

import _ "image/jpeg"    //register JPEG decoder

to the application’s main package. Somewhere in your code (not necessarily in the main package), to load a JPEG image, use the following snippets:

f, err := os.Open("inputimage.jpg")
if err != nil {
    log.Fatalf("os.Open() failed with %s\n", err)
}
defer f.Close()

img, fmtName, err := image.Decode(f)
if err != nil {
    log.Fatalf("image.Decode() failed with %s\n", err)
}

// `fmtName` contains the name used during format registration
// Work with `img` ...

Save to PNG

To save an image in a particular format, the corresponding encoder must be imported explicitly, for example:

import "image/png"    //needed to use `png` encoder

then an image can be saved with the following snippets:

f, err := os.Create("outimage.png")
if err != nil {
    log.Fatalf("os.Create() failed with %s\n", err)
}
defer f.Close()

// Encode to `PNG` with `DefaultCompression` level
// then save to file
err = png.Encode(f, img)
if err != nil {
    log.Fatalf("png.Encode() failed with %s\n", err)
}

If you want to specify a compression level other than DefaultCompression, create an encoder, for example:

enc := png.Encoder{
    CompressionLevel: png.BestSpeed,
}
err := enc.Encode(f, img)

Save to JPEG

To save to jpeg format, use the following:

import "image/jpeg"

// Somewhere in the same package
f, err := os.Create("outimage.jpg")
if err != nil {
    log.Fatalf("os.Create() failed with %s\n", err)
}
defer f.Close()

// Specify the quality, from 1 to 100
// Higher is better
opt := jpeg.Options{
    Quality: 90,
}
err = jpeg.Encode(f, img, &opt)
if err != nil {
    log.Fatalf("jpeg.Encode() failed with %s\n", err)
}

Save to GIF

To save the image to GIF file, use the following snippets.

import "image/gif"

// Somewhere in the same package
f, err := os.Create("outimage.gif")
if err != nil {
    log.Fatalf("os.Create() failed with %s\n", err)
}
defer f.Close()

opt := gif.Options {
    NumColors: 256,
    // Add more parameters as needed
}

err = gif.Encode(f, img, &opt)
if err != nil {
    log.Fatalf("gif.Encode() failed with %s\n", err)
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents