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")

}


猜你喜欢

转载自blog.csdn.net/zhuiyunzhugang/article/details/121247068
今日推荐