Golang如何实现图片水平翻转

Golang如何实现图片水平翻转 我目前找到的资料有:

更新:

// ...
                                // 裁剪图像
				r := image.Rect(0, 0, CropW, CropH)
				croppedImg := image.NewRGBA(r)
				draw.Draw(croppedImg, r, m, image.Point{x * CropW, y * CropH}, draw.Src)

				// 水平翻转裁剪后的图像
				// 使用图像处理并逐像素反转
				// croppedImg.point[x1,y1] > flippedImg.point[CropW - x1, y1]
				flippedImg := image.NewRGBA(r)
				for j := 0; j < croppedImg.Bounds().Dy(); j++ {
					for i := 0; i < croppedImg.Bounds().Dx(); i++ {
						flippedImg.Set(CropW-i, j, croppedImg.At(i, j))
					}
				}
// ...

更多关于Golang如何实现图片水平翻转的实战教程也可以访问 https://www.itying.com/category-94-b0.html

2 回复

您可以尝试使用这个包:https://godoc.org/github.com/disintegration/imaging#Rotate

以下是一个示例:https://play.golang.org/p/i2HyEe-MZBG

package main

import (
	"image"
	"image/color"
	"log"

	"github.com/disintegration/imaging"
)

func main() {
	// 打开原始图像
	src, err := imaging.Open("original.png")
	if err != nil {
		log.Fatalf("打开图像失败: %v", err)
	}

	// 将图像旋转90度
	dst := imaging.Rotate90(src)

	// 保存旋转后的图像
	err = imaging.Save(dst, "rotated.png")
	if err != nil {
		log.Fatalf("保存图像失败: %v", err)
	}
}

更多关于Golang如何实现图片水平翻转的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


在Go语言中实现图片水平翻转,可以通过标准库imagedraw来完成。你提供的代码思路是正确的,但有几个细节可以优化。下面是一个完整的示例:

package main

import (
    "image"
    "image/color"
    "image/draw"
    "image/jpeg"
    "os"
)

// 水平翻转图像
func flipHorizontal(src image.Image) image.Image {
    bounds := src.Bounds()
    dst := image.NewRGBA(bounds)
    
    width := bounds.Dx()
    for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
        for x := bounds.Min.X; x < bounds.Max.X; x++ {
            // 计算翻转后的x坐标
            flippedX := width - 1 - x
            dst.Set(flippedX, y, src.At(x, y))
        }
    }
    return dst
}

// 示例使用
func main() {
    // 读取原始图片
    file, err := os.Open("input.jpg")
    if err != nil {
        panic(err)
    }
    defer file.Close()
    
    img, _, err := image.Decode(file)
    if err != nil {
        panic(err)
    }
    
    // 水平翻转
    flipped := flipHorizontal(img)
    
    // 保存翻转后的图片
    outFile, err := os.Create("output_flipped.jpg")
    if err != nil {
        panic(err)
    }
    defer outFile.Close()
    
    jpeg.Encode(outFile, flipped, &jpeg.Options{Quality: 90})
}

如果你想要更高效的实现,可以使用draw包:

func flipHorizontalOptimized(src image.Image) image.Image {
    bounds := src.Bounds()
    dst := image.NewRGBA(bounds)
    
    width := bounds.Dx()
    for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
        for x := bounds.Min.X; x < bounds.Max.X; x++ {
            flippedX := width - 1 - x + bounds.Min.X
            dst.Set(flippedX, y, src.At(x, y))
        }
    }
    return dst
}

对于你的代码,建议修改为:

// 水平翻转裁剪后的图像
flippedImg := image.NewRGBA(r)
bounds := croppedImg.Bounds()
width := bounds.Dx()

for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
    for x := bounds.Min.X; x < bounds.Max.X; x++ {
        flippedX := width - 1 - x + bounds.Min.X
        flippedImg.Set(flippedX, y, croppedImg.At(x, y))
    }
}

这样处理可以确保坐标计算正确,特别是当图像边界不是从(0,0)开始时。

回到顶部