Documentation
¶
Overview ¶
Package img 提供面向调用者的图像读取、变换、合成与保存封装。
该包支持从本地文件或读取器打开图像,链式完成缩放、裁剪、模糊、灰度、对比度、 亮度、饱和度、旋转、粘贴、叠加、水印与 JPEG 压缩等操作,并支持在图像上绘制 文字;图像变换就地修改内部源图,方法返回自身以支持链式调用。
Index ¶
- Constants
- func CalculatePt(targetSize image.Point, watermark image.Point, anchor imaging.Anchor, ...) image.Point
- func CalculatePt2(targetSize image.Point, watermark image.Point, anchor imaging.Anchor, ...) image.Point
- func NewRGBA(r image.Rectangle) *image.RGBA
- func Save(img *Image, out io.Writer, format imaging.Format, opts ...imaging.EncodeOption) error
- func SaveToFile(img *Image, filename string, opts ...imaging.EncodeOption) error
- type Image
- func NewImage(src image.Image) *Image
- func OpenLocalFile(filename string, opts ...imaging.DecodeOption) (*Image, error)
- func OpenReader(src io.Reader, opts ...imaging.DecodeOption) (*Image, error)
- func Overlay(background Image, img Image, pos image.Point, opacity float64) *Image
- func Paste(background Image, img Image, pos image.Point) *Image
- func (img *Image) AddWaterMark(watermark *Image, anchor imaging.Anchor, marginX int, marginY int, ...) *Image
- func (img *Image) AdjustBrightness(percentage float64) *Image
- func (img *Image) AdjustContrast(percentage float64) *Image
- func (img *Image) AdjustGamma(gamma float64) *Image
- func (img *Image) AdjustSaturation(percentage float64) *Image
- func (img *Image) AdjustSigmoid(midpoint float64, factor float64) *Image
- func (img *Image) Blur(sigma float64) *Image
- func (img *Image) Clone() *Image
- func (img *Image) Compress(quality int) (*Image, error)
- func (img *Image) Convolve3x3(kernel [9]float64) *Image
- func (img *Image) Convolve5x5(kernel [25]float64) *Image
- func (img *Image) Crop(width int, height int, anchor imaging.Anchor) *Image
- func (img *Image) DrawText(content string, fc *fontx.FontConfig, m font.Metrics, anchor imaging.Anchor, ...) (*Image, error)
- func (img *Image) Fill(width int, height int, anchor imaging.Anchor, filter imaging.ResampleFilter) *Image
- func (img *Image) Fit(width int, height int, filter imaging.ResampleFilter) *Image
- func (img *Image) GetSource() image.Image
- func (img *Image) Gray() *Image
- func (img *Image) Height() int
- func (img *Image) Invert() *Image
- func (img *Image) Overlay(top *Image, pos image.Point, opacity float64) *Image
- func (img *Image) Paste(top *Image, pos image.Point) *Image
- func (img *Image) Resize(width int, height int, filter imaging.ResampleFilter) *Image
- func (img *Image) Rotate(angle float64, bgColor color.Color) *Image
- func (img *Image) SetSource(src image.Image) *Image
- func (img *Image) Sharpen(percentage float64) *Image
- func (img *Image) Transverse() *Image
- func (img *Image) Width() int
Constants ¶
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 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 OpenLocalFile ¶
func OpenLocalFile(filename string, opts ...imaging.DecodeOption) (*Image, error)
OpenLocalFile 从本地文件解码图像并返回 Image;仅支持 jpg、png、webp、jpeg 格式, 不支持的格式或读取、解码失败时返回错误。
func OpenReader ¶
OpenReader 从读取器解码图像并返回 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 ¶
AdjustBrightness 按 percentage 调整图像亮度,取值范围 (-100, 100): 0 为原图,-100 为纯黑,100 为纯白;返回自身。
func (*Image) AdjustContrast ¶
AdjustContrast 按 percentage 调整图像对比度,取值范围 (-100, 100): 0 为原图,-100 为纯灰色;返回自身。
func (*Image) AdjustGamma ¶
AdjustGamma 对图像做伽马校正,gamma 必须为正数:1.0 为原图,小于 1.0 变暗, 大于 1.0 变亮;返回自身。
func (*Image) AdjustSaturation ¶
AdjustSaturation 按 percentage 调整图像饱和度,取值范围 (-100, 100): 0 为原图,100 饱和度加倍,-100 为灰度;返回自身。
func (*Image) AdjustSigmoid ¶
AdjustSigmoid 使用 S 形函数做非线性对比度调整,可保留高光与阴影细节。 midpoint 为对比度中点,取值 0 到 1,通常取 0.5;factor 为对比度强弱, 通常在 (-10, 10) 之间,正值增强对比度,负值减弱;返回自身。
func (*Image) Compress ¶
Compress 在不改变图像尺寸的前提下按指定 JPEG 质量重新编码,quality 取值 1 到 100, 数值越高画质越好;编码失败时返回 nil 与错误,成功时返回自身。
func (*Image) Convolve3x3 ¶
Convolve3x3 使用给定的 3×3 卷积核对图像做卷积处理,返回自身。
func (*Image) Convolve5x5 ¶
Convolve5x5 使用给定的 5×5 卷积核对图像做卷积处理,返回自身。
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) Transverse ¶
Transverse 依次对图像执行 imaging.Transverse 与 imaging.Rotate90 变换,返回自身。