img

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	//MakerNotePreserve copies the MakerNote through to the new image. Default
	MakerNotePreserve = metadata.MakerNotePreserve
	//MakerNoteStrip removes the MakerNote from the new image
	MakerNoteStrip = metadata.MakerNoteStrip
	//MakerNoteFail aborts with metadata.ErrMakerNotePresent if a MakerNote is present
	MakerNoteFail = metadata.MakerNoteFail
)

MakerNote policies, aliased from the metadata package

View Source
const (
	//FormatUnknown is anything mimage does not handle
	FormatUnknown = metadata.FormatUnknown
	FormatJpeg    = metadata.FormatJpeg
	FormatPng     = metadata.FormatPng
	FormatGif     = metadata.FormatGif
	FormatTiff    = metadata.FormatTiff
	FormatBmp     = metadata.FormatBmp
	FormatMp4     = metadata.FormatMp4
	FormatMov     = metadata.FormatMov
	FormatAvi     = metadata.FormatAvi
)

Formats, aliased from the metadata package. The video formats are detected but cannot be read or written here, see the video package

Variables

View Source
var (
	//ErrUnsupportedFormat is returned when a source cannot be decoded as a
	//known image format, including a video, or a requested output format is
	//one mimage cannot write. Check with errors.Is
	ErrUnsupportedFormat = errors.New("mimage: unsupported image format")
	//ErrDestHasExtension is returned when a destination base name already
	//carries an image extension. mimage appends the extension for the chosen
	//format, so passing "out.jpg" would produce "out.jpg.jpg". Check with
	//errors.Is
	ErrDestHasExtension = errors.New("mimage: destination already has an image extension")
)

Image errors

Functions

func ConvertFile added in v0.1.0

func ConvertFile(source, destBase string, format Format, opts ConvertOptions) (string, bool, error)

ConvertFile writes source to destBase in the given format, keeping the original dimensions, and returns the path it actually wrote. The canonical extension for format is appended to destBase, which must not already carry an image extension.

The source format is detected from its content, not its filename. Reports false when the source is already in format: nothing is written and the caller should keep using the original file.

An animated gif converts to its first frame only, the rest are discarded, and transparency is lost when the destination is a jpeg

func CropImage added in v0.0.11

func CropImage(img image.Image, crop image.Rectangle) image.Image

CropImage crops an Image.

func Open added in v0.0.11

func Open(fileName string) (image.Image, error)

Open an image for editing

func OpenOpts added in v0.0.11

func OpenOpts(fileName string, autoOrientation bool, srcBytes bool) (image.Image, []byte, error)

OpenOpts opens an Image for editing. Possibly returning the src file as a byte slice

func RotateAndCropFile added in v0.0.11

func RotateAndCropFile(source string, dest string, opts Options) error

RotateAndCropFile rotates and crops an image file and saves the result

func RotateImage added in v0.0.11

func RotateImage(img image.Image, angle int) image.Image

RotateImage rotates an image. Negative angle indicates a counter clockwise rotation

func Save added in v0.0.11

func Save(image image.Image, fileName string) error

Save an image (defaults to Jpeg Quality 90)

func SaveOpts added in v0.0.11

func SaveOpts(image image.Image, fileName string, quality int, srcExif []byte) error

SaveOpts saves an image. If srcExif is != nil it will try to extract and exif information from that and append it to the new file. Any MakerNote in srcExif is preserved, use SaveOptsMakerNote to control that

func SaveOptsMakerNote added in v0.0.19

func SaveOptsMakerNote(image image.Image, fileName string, quality int, srcExif []byte, policy MakerNotePolicy) error

SaveOptsMakerNote is SaveOpts with control over what happens to an exif MakerNote carried in srcExif

func TransformFile

func TransformFile(source string, destinations map[string]Options) error

TransformFile creates versions of source based on destinations, decoding the source once and writing every output from it.

The keys of destinations are base names without an extension: the canonical extension for that destination's Options.Format is appended, so a key of "/out/thumb" with FormatJpeg writes "/out/thumb.jpg". A key that already carries an image extension is an error, see ErrDestHasExtension.

Options.Format is required. The source format is detected from its content rather than its filename. Quality and CopyExif only apply when the destination is a jpeg, and metadata is only carried when the source is a jpeg or a tiff

Example
sourceImg := "../assets/leica.jpg"
homeDir, _ := os.UserHomeDir()
sourceDir := path.Join(homeDir, "transform")
_ = os.Mkdir(sourceDir, 0755)

//for all but the thumb we are copying the original meta information
thumb := NewOptions(ResizeAndCrop, 400, 400, false, FormatJpeg)
landscape := NewOptions(ResizeAndCrop, 1200, 628, true, FormatJpeg)
square := NewOptions(ResizeAndCrop, 1200, 1200, true, FormatJpeg)
portrait := NewOptions(ResizeAndCrop, 1080, 1350, true, FormatJpeg)
resize := NewOptions(Resize, 1200, 0, true, FormatJpeg)

destImgs := map[string]Options{
	path.Join(sourceDir, "thumb"):     thumb,
	path.Join(sourceDir, "landscape"): landscape,
	path.Join(sourceDir, "square"):    square,
	path.Join(sourceDir, "portrait"):  portrait,
	path.Join(sourceDir, "resize"):    resize,
}

_ = TransformFile(sourceImg, destImgs)
fmt.Println("Transformed leica.jpg")
Output:
Transformed leica.jpg

Types

type ConvertOptions added in v0.1.0

type ConvertOptions struct {
	//Quality is the jpeg quality, 1-100. Anything outside that range means 90
	Quality int
	//CopyExif carries the source metadata across. Honoured only when the
	//destination is a jpeg and the source is a jpeg or a tiff
	CopyExif bool
	//MakerNote controls what happens to an exif MakerNote when CopyExif is set
	MakerNote MakerNotePolicy
}

ConvertOptions holds the options for ConvertFile

func NewConvertOptions added in v0.1.0

func NewConvertOptions(copyExif bool) ConvertOptions

NewConvertOptions creates ConvertOptions defaulting to image quality 90

type CropAnchor

type CropAnchor int

CropAnchor specifies where the anchor point should be when cropping an image

const (
	Center CropAnchor = iota
	TopLeft
	Top
	TopRight
	Left
	Right
	BottomLeft
	Bottom
	BottomRight
)

Various Anchors for your crop

type Format added in v0.1.0

type Format = metadata.Format

Format identifies an image format. See metadata.Format

type MakerNotePolicy added in v0.0.19

type MakerNotePolicy = metadata.MakerNotePolicy

MakerNotePolicy controls what happens to an exif MakerNote when metadata is copied to a transformed image. See metadata.MakerNotePolicy

type Options

type Options struct {
	Width     int
	Height    int
	Quality   int
	Anchor    CropAnchor
	Transform TransformType
	Strategy  ResampleStrategy
	X         int
	Y         int
	Angle     int
	//CopyExif carries the source metadata into the destination. Only honoured
	//when the destination is a jpeg and the source is a jpeg or a tiff. For
	//other source formats it is ignored
	CopyExif bool
	//MakerNote controls what happens to an exif MakerNote when CopyExif is set.
	//The zero value preserves it, matching earlier releases
	MakerNote MakerNotePolicy
	//Format of the output. Required: a destination whose Format is
	//FormatUnknown, or a format mimage cannot write such as a video, is an
	//error
	Format Format
}

Options holds all options for a given image transformation job

func NewOptions

func NewOptions(transform TransformType, width, height int, copyExif bool, format Format) Options

NewOptions creates Options for an output in the given format, defaulting to crop center strategy, image quality 90% and Lanczos resampling. The format is a parameter rather than a field to set afterwards because it is required, and forgetting it would only surface at runtime

type ResampleStrategy

type ResampleStrategy int

ResampleStrategy specifies what method to use when resampling an image

const (
	//Lanczos sampling. Default
	Lanczos ResampleStrategy = iota
	//NearestNeighbor sampling
	NearestNeighbor
	//Box sampling
	Box
	//Linear sampling
	Linear
	//Hermite sampling
	Hermite
	//MitchellNetravali sampling
	MitchellNetravali
	//CatmullRom sampling
	CatmullRom
	//BSpline sampling
	BSpline
	//Gaussian sampling
	Gaussian
	//Bartlett sampling
	Bartlett
	//Hann sampling
	Hann
	//Hamming sampling
	Hamming
	//Blackman sampling
	Blackman
	//Welch sampling
	Welch
	//Cosine sampling
	Cosine
)

type TransformType

type TransformType int

TransformType specifies how to transform an image, crop, resize, etc...

const (
	//ResizeAndCrop scales the image to given dimension. To keep aspect ratio the image is cropped
	ResizeAndCrop TransformType = iota
	//Crop cuts a specified region of the image
	Crop
	//Resize scale the image to the specified dimensions. To keep aspect ratio set either width or height to 0
	Resize
	//ResizeAndFit scale the image to fit the maximum specified dimensions.
	ResizeAndFit
)

Jump to

Keyboard shortcuts

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