使用最新版githubcomdisintegrationimaging库给圆形图片加阴影
要使用github.com/disintegration/imaging库给圆形图片加阴影,你可以按照以下步骤进行操作:
- 确保你已经安装了Go语言环境,并在你的项目中导入
github.com/disintegration/imaging库。
import "github.com/disintegration/imaging"
- 读取原始图片文件。
srcImage, err := imaging.Open("input.jpg")
if err != nil {
log.Fatalf("Failed to open image: %v", err)
}
- 创建一个新的画布,大小与原始图片一样。
canvas := imaging.New(srcImage.Bounds().Max.X, srcImage.Bounds().Max.Y, color.Transparent)
- 将原始图片绘制到画布上。
canvas = imaging.PasteCenter(canvas, srcImage)
- 创建一个圆形遮罩。
mask := imaging.NewCanvas(srcImage.Bounds().Max.X, srcImage.Bounds().Max.Y)
mask = imaging.Circle(mask, srcImage.Bounds().Max.X/2, srcImage.Bounds().Max.Y/2, color.White)
- 将遮罩应用到画布上。
canvas = imaging.Overlay(canvas, mask, image.Pt(0, 0), 1)
- 创建一个具有阴影效果的画布。
shadow := imaging.NewCanvas(srcImage.Bounds().Max.X+10, srcImage.Bounds().Max.Y+10, color.Transparent)
shadow = imaging.Overlay(shadow, canvas, image.Pt(5, 5), 1)
- 保存结果图片。
err = imaging.Save(shadow, "output.jpg")
if err != nil {
log.Fatalf("Failed to save image: %v", err)
}
以上代码将读取名为input.jpg的图片文件,创建一个具有圆形遮罩和阴影的图片,并保存为output.jpg。
请注意,如果要使用该库,你需要先使用go get命令进行安装:
go get github.com/disintegration/imaging
``
原文地址: https://www.cveoy.top/t/topic/ine7 著作权归作者所有。请勿转载和采集!