当前位置:网站首页>Golang image/png processing image rotation writing
Golang image/png processing image rotation writing
2022-06-29 12:48:00 【Asimov__】
package mint
import (
"github.com/golang/freetype"
"golang.org/x/image/font"
"image"
"image/color"
"image/draw"
"image/png"
"io/ioutil"
"log"
"os"
)
func rotate90(m image.Image) image.Image {
rotate90 := image.NewRGBA(image.Rect(0, 0, m.Bounds().Dy(), m.Bounds().Dx()))
// Matrix rotation
for x := m.Bounds().Min.Y; x < m.Bounds().Max.Y; x++ {
for y := m.Bounds().Max.X - 1; y >= m.Bounds().Min.X; y-- {
// Set the pixels
rotate90.Set(m.Bounds().Max.Y-x, y, m.At(y, x))
}
}
return rotate90
}
// rotate 180 degree
func rotate180(m image.Image) image.Image {
rotate180 := image.NewRGBA(image.Rect(0, 0, m.Bounds().Dx(), m.Bounds().Dy()))
// Matrix rotation
for x := m.Bounds().Min.X; x < m.Bounds().Max.X; x++ {
for y := m.Bounds().Min.Y; y < m.Bounds().Max.Y; y++ {
// Set the pixels
rotate180.Set(m.Bounds().Max.X-x, m.Bounds().Max.Y-y, m.At(x, y))
}
}
return rotate180
}
// rotate 270 degree
func rotate270(m image.Image) image.Image {
rotate270 := image.NewRGBA(image.Rect(0, 0, m.Bounds().Dy(), m.Bounds().Dx()))
// Matrix rotation
for x := m.Bounds().Min.Y; x < m.Bounds().Max.Y; x++ {
for y := m.Bounds().Max.X - 1; y >= m.Bounds().Min.X; y-- {
// Set the pixels
rotate270.Set(x, m.Bounds().Max.X-y, m.At(y, x))
}
}
return rotate270
}
func writeLabel(img image.Image, label string, x, y int, fontColor color.Color, size float64, fontPath string) (image.Image, error) {
bound := img.Bounds()
// Create a new picture
rgba := image.NewRGBA(image.Rect(0, 0, bound.Dx(), bound.Dy()))
// Read the font
fontBytes, err := ioutil.ReadFile(fontPath)
if err != nil {
return rgba, err
}
myFont, err := freetype.ParseFont(fontBytes)
if err != nil {
return rgba, err
}
draw.Draw(rgba, rgba.Bounds(), img, bound.Min, draw.Src)
c := freetype.NewContext()
c.SetDPI(72)
c.SetFont(myFont)
c.SetFontSize(size)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
uni := image.NewUniform(fontColor)
c.SetSrc(uni)
c.SetHinting(font.HintingNone)
// Display... In the specified position
pt := freetype.Pt(x, y+int(c.PointToFixed(size)>>6))
if _, err := c.DrawString(label, pt); err != nil {
return rgba, err
}
return rgba, nil
}
func CreatePassImage(cardColor string, createImg string, label string) {
src, err := os.Open(cardColor)
if err != nil {
log.Println(err)
return
}
img, err := png.Decode(src)
if err != nil {
log.Println(err)
return
}
image := rotate270(img)
writeimage, err := writeLabel(image, label, 56, 870, color.RGBA{255, 255, 255, 255}, 60, "./image/PingFang Heavy.ttf")
if err != nil {
log.Println(err)
return
}
outimage := rotate90(writeimage)
f, err := os.Create(createImg)
if err != nil {
log.Println(err)
return
}
defer f.Close()
err = png.Encode(f, outimage)
if err != nil {
log.Println(err)
return
}
}
边栏推荐
- 求大数的阶乘 ← C语言
- Gbase8s database into table clause
- MIT linear algebra Chinese Notes
- 535. TinyURL 的加密与解密 : 设计一个 URL 简化系统
- 推荐模型复现(三):召回模型YoutubeDNN、DSSM
- [环境配置]PWC-Net
- Gbase8s database into external clause
- Interpolated scatter data
- Quick look | the long-awaited 2022 Guangzhou assistant testing engineer's real problem analysis is finally released
- Gbase8s database sorts standard or raw result tables
猜你喜欢

1. opencv realizes simple color recognition

模糊图片变清晰,一键双色图片,快速整理本地图片...这8个在线图片工具申请加入你的收藏夹!

Go Senior Engineer required course | I sincerely suggest you listen to it. Don't miss it~
![[leetcode] 14. Longest public prefix](/img/3b/3388ce8382ad5caaaf0a42488da2f9.png)
[leetcode] 14. Longest public prefix

How to install oracle19c in Centos8

《高难度谈话》突破谈话瓶颈,实现完美沟通

How to install oracle19c in Centos8

C#通過中序遍曆對二叉樹進行線索化

解决问题:ModuleNotFoundError: No module named ‘pip‘

墨菲安全入选中关村科学城24个重点项目签约
随机推荐
InDesign插件-常规功能开发-JS调试器打开和关闭-js脚本开发-ID插件
C#通过线索二叉树进行中序遍历输出
Gbase8s database select has order by Clause 4
Unexpected ‘debugger‘ statement no-debugger
File contained log poisoning (user agent)
Kyligence Zen, an intelligent indicator driven management and decision-making platform, is newly launched and is in limited internal testing
2022.6.28-----leetcode. three hundred and twenty-four
缓存一致性,删除缓存,写入缓存,缓存击穿,缓存穿透,缓存雪崩
【LeetCode】14、最长公共前缀
Principle and process of MySQL master-slave replication
An interpretable geometric depth learning model for structure based protein binding site prediction
Go Senior Engineer required course | I sincerely suggest you listen to it. Don't miss it~
从Mpx资源构建优化看splitChunks代码分割
墨菲安全入选中关村科学城24个重点项目签约
如何計算win/tai/loss in paired t-test
C#通过中序遍历对二叉树进行线索化
nacos启动报错
ERP Kingdee for preparing BOM
Gbase8s database select has order by Clause 1
ZALSM_EXCEL_TO_INTERNAL_TABLE 导入数据大问题解决