Chapter 15. Image-Processing Recipes
15.0 Introduction
The standard library for 2D image manipulation is the image package and the main interface is image.Image. To work with the different image formats, you need to register the format first by initializing the format’s package in the program’s main package:
import_"image/png"
Importing a package with an underscore allows you to create the package-level variables and also execute the init function in the image/png package. You can import more than one format; it doesn’t matter and the compiler won’t complain (because you’re naming it (“_”)—if you don’t name it, the compiler will complain).
Before you start using the image package, it’s important to know some of its most often-used interfaces and structs.
Image and Other Interfaces
The image.Image type is an interface that represents a rectangular grid of color.Color pixel values, taken from a color model. This is the main interface for the image package. Structs that implement this interface have to implement the ColorModel, Bounds, and At methods:
typeImageinterface{ColorModel()color.ModelBounds()RectangleAt(x,yint)color.Color}
The color.Color interface has a method that returns the four values—red, green, blue, and alpha:
typeColorinterface{RGBA()(r,g,b,auint32)}
The color.Color interface represents a color. The At method in image.Image returns the color at the specific location at x, y.
A image.Rectangle is a rectangle defined by its top-left and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access