Created
October 26, 2014 08:47
-
-
Save liuzhe0223/adc35dada3e03250e7cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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