要使用github.com/disintegration/imaging给图片加边框,您需要按照以下步骤进行操作:

  1. 在您的项目中,添加github.com/disintegration/imaging作为依赖项。可以使用go get命令来安装该包:

    go get github.com/disintegration/imaging
    
  2. 创建一个Go文件,导入必要的包:

    package main
    
    import (
    	"image"
    	"image/color"
    	"log"
    
    	"github.com/disintegration/imaging"
    )
    
  3. 使用imaging.Open函数打开您要添加边框的图片:

    src, err := imaging.Open("path/to/your/image.jpg")
    if err != nil {
    	log.Fatalf("Failed to open image: %v", err)
    }
    
  4. 使用imaging.New函数创建一个新的画布,该画布大小与原始图像大小相同,并使用所需的边框颜色:

    borderColor := color.RGBA{255, 0, 0, 255} // 使用红色边框
    borderedImage := imaging.New(src.Bounds().Max.X+10, src.Bounds().Max.Y+10, borderColor)
    
  5. 使用imaging.Paste函数将原始图像粘贴到边框画布上:

    borderedImage = imaging.Paste(borderedImage, src, image.Pt(5, 5))
    
  6. 使用imaging.Save函数将结果保存为新的图像文件:

    err = imaging.Save(borderedImage, "path/to/your/bordered_image.jpg")
    if err != nil {
    	log.Fatalf("Failed to save image: %v", err)
    }
    
  7. 运行程序并检查生成的图像文件,您将看到带有边框的图像。

请注意,这只是一个基本示例,您可以根据需要进行更多的自定义操作,例如添加不同大小或样式的边框,调整边框的位置等。请参考github.com/disintegration/imaging的文档以获取更多信息

使用githubcomdisintegrationimaging给图片加边框

原文地址: https://www.cveoy.top/t/topic/imTn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录