To flip or mirror an image horizontally or vertically in Go, you can use the github.com/disintegration/imaging
library. Here's an example of how to do it:
Install the imaging
library by running the following command in your terminal:
go get -u github.com/disintegration/imaging
Import the imaging
package in your Go file:
import (
"github.com/disintegration/imaging"
)
Load the image from a file using imaging.Open("path/to/image.jpg")
:
img, err := imaging.Open("path/to/image.jpg")
if err != nil {
// handle error
}
To horizontally flip the image, use the imaging.FlipH(img)
function:
flippedHorizontally := imaging.FlipH(img)
To vertically flip the image, use the imaging.FlipV(img)
function:
flippedVertically := imaging.FlipV(img)
Save the flipped images to a file using imaging.Save(flippedImage, "path/to/output.jpg")
:
err = imaging.Save(flippedHorizontally, "path/to/output_h.jpg")
if err != nil {
// handle error
}
err = imaging.Save(flippedVertically, "path/to/output_v.jpg")
if err != nil {
// handle error
}
Make sure to replace "path/to/image.jpg"
, "path/to/output_h.jpg"
, and "path/to/output_v.jpg"
with the appropriate file paths.
That's it! You have successfully flipped or mirrored an image horizontally or vertically in Go using the imaging
library.