img

package
v3.8.56 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package img 提供面向调用者的图像读取、变换、合成与保存封装。

该包支持从本地文件或读取器打开图像,链式完成缩放、裁剪、模糊、灰度、对比度、 亮度、饱和度、旋转、粘贴、叠加、水印与 JPEG 压缩等操作,并支持在图像上绘制 文字;图像变换就地修改内部源图,方法返回自身以支持链式调用。

Index

Constants

View Source
const (
	// DefaultDPI 为文字绘制使用的默认 DPI,固定为 72。
	DefaultDPI = 72
)

Variables

This section is empty.

Functions

func CalculatePt

func CalculatePt(targetSize image.Point,
	watermark image.Point,
	anchor imaging.Anchor,
	marginX int, marginY int) image.Point

CalculatePt 按锚点 anchor 与边距 marginX、marginY,依据目标图与水印图尺寸计算水印落点坐标。

func CalculatePt2

func CalculatePt2(targetSize image.Point,
	watermark image.Point,
	anchor imaging.Anchor,
	marginX int, marginY int) image.Point

CalculatePt2 按锚点 anchor 与边距 marginX、marginY 计算文字绘制落点坐标, 居中锚点下边距会叠加到居中位置上。

func NewRGBA

func NewRGBA(r image.Rectangle) *image.RGBA

NewRGBA 按矩形区域新建一张 image.RGBA 图像。

func Save

func Save(img *Image, out io.Writer, format imaging.Format, opts ...imaging.EncodeOption) error

Save 按指定格式将图像编码写入 out;编码失败时返回错误。

func SaveToFile

func SaveToFile(img *Image, filename string, opts ...imaging.EncodeOption) error

SaveToFile 按文件扩展名推断编码格式,将图像写入 filename 指定的文件; 格式识别、创建文件或编码失败时返回错误;编码成功后若文件关闭失败,返回关闭错误。

Types

type Image

type Image struct {
	// contains filtered or unexported fields
}

Image 包装一张待处理的源图,图像变换方法就地修改内部源图并返回自身,支持链式调用。

零值内部源图为 nil,须通过 OpenLocalFile、OpenReader 或 NewImage 构造; 方法间不保证并发安全。

func NewImage

func NewImage(src image.Image) *Image

NewImage 将给定 image.Image 包装为 Image。

func OpenLocalFile

func OpenLocalFile(filename string, opts ...imaging.DecodeOption) (*Image, error)

OpenLocalFile 从本地文件解码图像并返回 Image;仅支持 jpg、png、webp、jpeg 格式, 不支持的格式或读取、解码失败时返回错误。

func OpenReader

func OpenReader(src io.Reader, opts ...imaging.DecodeOption) (*Image, error)

OpenReader 从读取器解码图像并返回 Image;解码失败时返回错误。

func Overlay

func Overlay(background Image, img Image, pos image.Point, opacity float64) *Image

Overlay 在 pos 位置以 opacity(取值 0.0 到 1.0)将 img 叠加到 background, 返回合成后的新 Image。

func Paste

func Paste(background Image, img Image, pos image.Point) *Image

Paste 将 img 粘贴到 background 的 pos 位置,返回合成后的新 Image。

func (*Image) AddWaterMark

func (img *Image) AddWaterMark(watermark *Image, anchor imaging.Anchor, marginX int, marginY int, opacity float64) *Image

AddWaterMark 按锚点与边距计算水印位置,并以 opacity(0.0 到 1.0)叠加水印图,返回自身。

func (*Image) AdjustBrightness

func (img *Image) AdjustBrightness(percentage float64) *Image

AdjustBrightness 按 percentage 调整图像亮度,取值范围 (-100, 100): 0 为原图,-100 为纯黑,100 为纯白;返回自身。

func (*Image) AdjustContrast

func (img *Image) AdjustContrast(percentage float64) *Image

AdjustContrast 按 percentage 调整图像对比度,取值范围 (-100, 100): 0 为原图,-100 为纯灰色;返回自身。

func (*Image) AdjustGamma

func (img *Image) AdjustGamma(gamma float64) *Image

AdjustGamma 对图像做伽马校正,gamma 必须为正数:1.0 为原图,小于 1.0 变暗, 大于 1.0 变亮;返回自身。

func (*Image) AdjustSaturation

func (img *Image) AdjustSaturation(percentage float64) *Image

AdjustSaturation 按 percentage 调整图像饱和度,取值范围 (-100, 100): 0 为原图,100 饱和度加倍,-100 为灰度;返回自身。

func (*Image) AdjustSigmoid

func (img *Image) AdjustSigmoid(midpoint float64, factor float64) *Image

AdjustSigmoid 使用 S 形函数做非线性对比度调整,可保留高光与阴影细节。 midpoint 为对比度中点,取值 0 到 1,通常取 0.5;factor 为对比度强弱, 通常在 (-10, 10) 之间,正值增强对比度,负值减弱;返回自身。

func (*Image) Blur

func (img *Image) Blur(sigma float64) *Image

Blur 使用高斯模糊处理图像,sigma 必须为正数,数值越大模糊程度越高;返回自身。

func (*Image) Clone

func (img *Image) Clone() *Image

Clone 返回包装当前源图的新 Image;两个实例共享同一份底层图像数据。

func (*Image) Compress

func (img *Image) Compress(quality int) (*Image, error)

Compress 在不改变图像尺寸的前提下按指定 JPEG 质量重新编码,quality 取值 1 到 100, 数值越高画质越好;编码失败时返回 nil 与错误,成功时返回自身。

func (*Image) Convolve3x3

func (img *Image) Convolve3x3(kernel [9]float64) *Image

Convolve3x3 使用给定的 3×3 卷积核对图像做卷积处理,返回自身。

func (*Image) Convolve5x5

func (img *Image) Convolve5x5(kernel [25]float64) *Image

Convolve5x5 使用给定的 5×5 卷积核对图像做卷积处理,返回自身。

func (*Image) Crop

func (img *Image) Crop(width int, height int, anchor imaging.Anchor) *Image

Crop 按指定锚点从图像中裁出 width×height 的矩形区域,变换就地作用于内部源图并返回自身。

func (*Image) DrawText

func (img *Image) DrawText(content string, fc *fontx.FontConfig, m font.Metrics, anchor imaging.Anchor, marginX int, marginY int) (*Image, error)

DrawText 在当前图像上绘制文字 content。

fc 提供字体、字号与颜色,m 为对应字号的字体度量;anchor 决定文字对齐方式, marginX、marginY 为距最近边框的边距,对齐方式为 Center 等居中锚点时边距 参照左侧或上侧边框。绘制失败时返回图像自身与错误。

func (*Image) Fill

func (img *Image) Fill(width int, height int, anchor imaging.Anchor, filter imaging.ResampleFilter) *Image

Fill 将源图像按锚点缩放并裁剪填充到指定宽高,过程中保持比例不变形,返回自身。

func (*Image) Fit

func (img *Image) Fit(width int, height int, filter imaging.ResampleFilter) *Image

Fit 使用指定重采样滤镜等比缩放图像,使整体不超过给定的最大宽高,返回自身。

func (*Image) GetSource

func (img *Image) GetSource() image.Image

GetSource 返回内部持有的底层 image.Image。

func (*Image) Gray

func (img *Image) Gray() *Image

Gray 将图像转换为灰度图,返回自身。

func (*Image) Height

func (img *Image) Height() int

Height 返回图像像素高度。

func (*Image) Invert

func (img *Image) Invert() *Image

Invert 对图像做反色(负片)处理,返回自身。

func (*Image) Overlay

func (img *Image) Overlay(top *Image, pos image.Point, opacity float64) *Image

Overlay 在 pos 位置以 opacity(取值 0.0 到 1.0)将 top 叠加到当前图像,返回自身。

func (*Image) Paste

func (img *Image) Paste(top *Image, pos image.Point) *Image

Paste 将 top 粘贴到当前图像的 pos 位置,返回自身。

func (*Image) Resize

func (img *Image) Resize(width int, height int, filter imaging.ResampleFilter) *Image

Resize 使用指定重采样滤镜将图像缩放到给定宽高;宽或高为 0 时保持原图宽高比。 变换就地作用于内部源图并返回自身。

func (*Image) Rotate

func (img *Image) Rotate(angle float64, bgColor color.Color) *Image

Rotate 将图像按 angle 角度(单位:度)逆时针旋转,bgColor 填充旋转后露出的空白区域,返回自身。

func (*Image) SetSource

func (img *Image) SetSource(src image.Image) *Image

SetSource 将内部源图替换为 src,并返回自身以支持链式调用。

func (*Image) Sharpen

func (img *Image) Sharpen(percentage float64) *Image

Sharpen 按给定参数生成图像的锐化版本,参数语义与底层 imaging.Sharpen 一致,返回自身。

func (*Image) Transverse

func (img *Image) Transverse() *Image

Transverse 依次对图像执行 imaging.Transverse 与 imaging.Rotate90 变换,返回自身。

func (*Image) Width

func (img *Image) Width() int

Width 返回图像像素宽度。

Directories

Path Synopsis
Package fontx 提供 TrueType 字体加载与字体度量信息封装。
Package fontx 提供 TrueType 字体加载与字体度量信息封装。

Jump to

Keyboard shortcuts

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