go语言实现图像旋转

package main

import (
	"fmt"
	"image"
	"image/png"
	"log"
	"os"
)

func main() {
	file, err := os.Open("C:\\Users\\wg\\go\\src\\学习go语言\\预处理\\家人们_aa.png")
	if err != nil {
		log.Fatal(err)
	}
	dst:="laker.png"
	fOut, _ := os.Create(dst)
	defer fOut.Close()

	// decode jpeg into image.Image
	m, err := png.Decode(file)//jpg和png的格式不一样
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()
	rotate90 := image.NewRGBA(image.Rect(0, 0, m.Bounds().Dy(), m.Bounds().Dx()))
	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-- {
			rotate90.Set(m.Bounds().Max.Y-x, y, m.At(y, x))
		}
	}
	/*
	rotate180 := image.NewRGBA(image.Rect(0, 0, m.Bounds().Dx(), m.Bounds().Dy()))//旋转180度
	for x := m.Bounds().Min.X; x < m.Bounds().Max.X; x++ {
	    for y := m.Bounds().Min.Y; y < m.Bounds().Max.Y; y++ {
	        rotate180.Set(m.Bounds().Max.X-x, m.Bounds().Max.Y-y, m.At(x, y))
	    }
	}
	旋转270度
	rotate270 := image.NewRGBA(image.Rect(0, 0, imgb.Bounds().Dy(), imgb.Bounds().Dx()))
	for x := imgb.Min.Y; x < imgb.Max.Y; x++ {
	    for y := imgb.Max.X - 1; y >= imgb.Min.X; y-- {
	        rotate270.Set(x, imgb.Max.X-y, imgDec.At(y, x))
	    }
	}
	*/
	png.Encode(fOut,rotate90)

	fmt.Println("旋转成功,保存为某某rotate")

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值