imgprint

package module
v0.0.0-...-d58adc8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2024 License: MIT Imports: 9 Imported by: 0

README

go-imgprint

Package imgprint implements tools for printing text on images, for the Go programming language.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-imgprint

GoDoc

Example

Here a simple example of how to use imgprint:

// This is the image that we print the text on.
var img image.Image // = ...

// This is where on the image where we start printing the text string.
x,y = 10,50


imgprint.ImgPrint(img, x, y, "Hello world")

The color of the text defaults to hot magenta.

If you want to pick the color the text yourself, then you would use imgprint.ColoredString(), as in:

// This is the image that we print the text on.
var img image.Image // = ...

// This is where on the image where we start printing the text string.
x,y = 10,50

var textColor color.Color = color.NRGBA{0x1E,0x5A,0xA8, 0xFF} // blue

imgprint.ImgPrint(img, x, y, imgprint.ColoredString(textColor,"Hello world"))

Import

To import package imgprint use import code like the follownig:

import "github.com/reiver/go-imgprint"

Installation

To install package imgprint do the following:

GOPROXY=direct go get https://github.com/reiver/go-imgprint

Author

Package imgprint was written by Charles Iliya Krempeaux

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ImgPrint

func ImgPrint(dstimg draw.Image, x int, y int, value any) error

ImgPrint draw text on an image.

The simplest example of using ImgPrint is:

imgprint.ImgPrint(dstimg, x, y, "Hello world!")

... which would draw the text "Hello world!" onto the image 'dstimg' starting a (x,y).

'dstimg' is the image where the text will be draw. Whomever calls ImagePrint needs to create this image before hand.

'x', 'y' is where the text starts to be drawn.

`value` is what will be drawn. It could just a Go string. Or imgprint.ColoredString() could be used to give a string a color. Or imgprint.StyledString() could be used to give a string a style.

Example (ColoredString)
// Here we define some colors.
//
// We use these in 2 places in this example:
//
// Place №1:
//
// We use this in the palette we use when creating the image.
//
// (Note that we don't have to use a palette when creating an image.
// But, for this example we do.)
//
// Place №2:
//
// We use it to give the image an initial background color.
//
// ...
//
// Note that we have a number of colors here that we do NOT use in this example.
// We included them to make this a more realistic example.
transparent := color.NRGBA{0x00, 0x00, 0x00, 0x00}
black := color.NRGBA{0x1B, 0x2A, 0x34, 0xFF}
blue := color.NRGBA{0x1E, 0x5A, 0xA8, 0xFF}
gray := color.NRGBA{0x8A, 0x92, 0x8D, 0xFF}
magenta := color.NRGBA{0xFF, 0x1D, 0xCE, 0xFF}
red := color.NRGBA{0xB4, 0x00, 0x00, 0xFF}
yellow := color.NRGBA{0xFA, 0xC8, 0x0A, 0xFF}
white := color.NRGBA{0xFF, 0xFF, 0xFF, 0xFF}

// Here we create a palette (of colors).
//
// We use this palette when creating the image.
//
// Note that, in general, a lot of images do NOT have to have a palette.
// But there are times when you might want your image to use a palette.
//
// For example, when an image only has a limited number of colors,
// we can sometimes make the image file smaller (by using a palette).
//
// Also, some people use palettes to get a certain style.
var palette []color.Color = []color.Color{
	transparent,
	black,
	blue,
	gray,
	magenta,
	red,
	yellow,
	white,
}

// In this example the image we created will have the following dimensions:
//
//	1080x566
//
// This gets used later when we create our image.
var bounds = image.Rectangle{
	Min: image.Point{0, 0},
	Max: image.Point{1080, 566},
}

// Initially create the image.
//
// Note that image is just in the computer's memory at this point.
// It is NOT stored in a .GIF, .PNG, .JPEG file, or any other type of image file.
//
// Turning an image into a file is an extra step.
palimg := image.NewPaletted(bounds, palette)

// Give the image a background color.
var background image.Image = &image.Uniform{black}
drawer := draw.Drawer(draw.Src)
drawer.Draw(palimg, palimg.Bounds(), background, image.ZP)

// Print the text on it.
//
// THIS IS THE IMPORTANT PART OF THIS EXAMPLE.
x, y := 10, 50
str := "Hello world!"
err := imgprint.ImgPrint(palimg, x, y, imgprint.ColoredString(blue, str)) // <---------
if nil != err {
	fmt.Println("ERROR:", err)
	return
}

saveImage("example-hello-world_colored.png", palimg)
Output:


FILENAME: example-hello-world_colored.png
Example (Simple)
// Here we define some colors.
//
// We use these in 2 places in this example:
//
// Place №1:
//
// We use this in the palette we use when creating the image.
//
// (Note that we don't have to use a palette when creating an image.
// But, for this example we do.)
//
// Place №2:
//
// We use it to give the image an initial background color.
//
// ...
//
// Note that we have a number of colors here that we do NOT use in this example.
// We included them to make this a more realistic example.
transparent := color.NRGBA{0x00, 0x00, 0x00, 0x00}
black := color.NRGBA{0x1B, 0x2A, 0x34, 0xFF}
blue := color.NRGBA{0x1E, 0x5A, 0xA8, 0xFF}
gray := color.NRGBA{0x8A, 0x92, 0x8D, 0xFF}
magenta := color.NRGBA{0xFF, 0x1D, 0xCE, 0xFF}
red := color.NRGBA{0xB4, 0x00, 0x00, 0xFF}
yellow := color.NRGBA{0xFA, 0xC8, 0x0A, 0xFF}
white := color.NRGBA{0xFF, 0xFF, 0xFF, 0xFF}

// Here we create a palette (of colors).
//
// We use this palette when creating the image.
//
// Note that, in general, a lot of images do NOT have to have a palette.
// But there are times when you might want your image to use a palette.
//
// For example, when an image only has a limited number of colors,
// we can sometimes make the image file smaller (by using a palette).
//
// Also, some people use palettes to get a certain style.
var palette []color.Color = []color.Color{
	transparent,
	black,
	blue,
	gray,
	magenta,
	red,
	yellow,
	white,
}

// In this example the image we created will have the following dimensions:
//
//	1080x566
//
// This gets used later when we create our image.
var bounds = image.Rectangle{
	Min: image.Point{0, 0},
	Max: image.Point{1080, 566},
}

// Initially create the image.
//
// Note that image is just in the computer's memory at this point.
// It is NOT stored in a .GIF, .PNG, .JPEG file, or any other type of image file.
//
// Turning an image into a file is an extra step.
palimg := image.NewPaletted(bounds, palette)

// Give the image a background color.
var background image.Image = &image.Uniform{black}
drawer := draw.Drawer(draw.Src)
drawer.Draw(palimg, palimg.Bounds(), background, image.ZP)

// Print the text on it.
//
// THIS IS THE IMPORTANT PART OF THIS EXAMPLE.
x, y := 10, 50
str := "Hello world!"
err := imgprint.ImgPrint(palimg, x, y, str) // <---------
if nil != err {
	fmt.Println("ERROR:", err)
	return
}

saveImage("example-hello-world.png", palimg)
Output:


FILENAME: example-hello-world.png

Types

type Style

type Style struct {
	ForegroundColor color.Color
	FontFace        font.Face
}

Style represents a single style, that is applied to a text string.

A style includes: foreground-color, font-face.

Style is uses with imgprint.StyledString(), to give a string style. Which in turn is passed to imgprint.ImgPrint().

type StyledStringer

type StyledStringer interface {
	fmt.Stringer
	ForegroundColor() color.Color
	FontFace() font.Face
}

StyledStringer represents a text string that has a (single) style.

func ColoredString

func ColoredString(c color.Color, str string) StyledStringer

ColoredString is used, with imgprint.ImgPrint(), to give a text string a color.

For example:

var red color.NRGBA{0xB4,0x00,0x00, 0xFF}

imgprint.ImgPrint(
	dstimg,
	x, y,
	imgprint.ColoredString(red, "Hello world!"),
)

func StyledString

func StyledString(style Style, str string) StyledStringer

StyledString is used, with imgprint.ImgPrint(), to give a text string a (single) style.

For example:

var blue color.NRGBA{0x1E,0x5A,0xA8, 0xFF}
var fontface font.Face = basicfont.Face7x13

var style imgprint.Style = imgprint.Style{
	FontFace:
	ForegroundColor: blue,
}

imgprint.ImgPrint(
	dstimg,
	x, y,
	imgprint.StyledString(style, "Hello world!"),
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL