Skip to content

Instantly share code, notes, and snippets.

@liuzhe0223
Created October 26, 2014 08:47
Show Gist options
  • Save liuzhe0223/adc35dada3e03250e7cc to your computer and use it in GitHub Desktop.
Save liuzhe0223/adc35dada3e03250e7cc to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"image"
"image/jpeg"
"os"
)
func main() {
f, err := os.Open("test.jpg")
if err != nil {
fmt.Println(err)
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
fmt.Println(err)
}
imgY := img.(*image.YCbCr)
imgBounds := imgY.Bounds()
mX := imgBounds.Max.X
mY := imgBounds.Max.Y
for x := 0; x < mX; x += 256 {
for y := 0; y < mY; y += 256 {
subImg := imgY.SubImage(image.Rect(x, y, x+256, y+256)).(*image.YCbCr)
fmt.Println(subImg.Bounds())
imgName := fmt.Sprintf("img%dx%d.jpg", x+256, y+256)
fo, err := os.Create(imgName)
if err != nil {
fmt.Println(err)
}
defer fo.Close()
jpeg.Encode(fo, subImg, nil)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment