Created
December 10, 2015 08:18
-
-
Save anolivetree/d59544bbf6c8b8b2b9ca to your computer and use it in GitHub Desktop.
Androidのscreenshotの上部のnotificationエリアと下部のボタンエリアを削除したPNGを作る
This file contains 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/png" | |
_ "image/png" | |
"os" | |
) | |
func main() { | |
for _, name := range os.Args[1:] { | |
f, err := os.Open(name) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
continue | |
} | |
defer f.Close() | |
m, _, err := image.Decode(f) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
continue | |
} | |
fmt.Printf("%v\n", name) | |
// copy image | |
w := m.Bounds().Max.X - m.Bounds().Min.X | |
h := m.Bounds().Max.Y - m.Bounds().Min.Y | |
subImage := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{w, h}}) | |
for y := 0; y < h; y++ { | |
for x := 0; x < w; x++ { | |
subImage.Set(x, y, m.At(x+m.Bounds().Min.X, y+m.Bounds().Min.Y)) | |
} | |
} | |
var trimTop int | |
for y := 0; y < h; y++ { | |
isBlack := isBlackLine(subImage, y) | |
if !isBlack { | |
trimTop = y | |
break | |
} | |
} | |
var trimBottom int | |
for y := h - 1; y >= 0; y-- { | |
isBlack := isBlackLine(subImage, y) | |
if !isBlack { | |
trimBottom = y | |
break | |
} | |
} | |
if trimTop == 0 && trimBottom == h-1 || trimBottom == 0 { | |
fmt.Printf("skip\n") | |
continue | |
} | |
fmt.Printf("trimTop=%v tripBottom=%v\n", trimTop, trimBottom) | |
trimmedImage := subImage.SubImage(image.Rectangle{image.Point{0, trimTop}, image.Point{w, trimBottom + 1}}) | |
fmt.Printf("sub=%v\n", trimmedImage.Bounds()) | |
fout, err := os.OpenFile(name+".trimmed.png", os.O_WRONLY|os.O_CREATE, 0666) | |
if err != nil { | |
panic(err) | |
} | |
defer fout.Close() | |
err = png.Encode(fout, trimmedImage) | |
if err != nil { | |
panic(err) | |
} | |
} | |
} | |
// 横一列にblackが50%以上入っている場合true | |
func isBlackLine(m image.Image, y int) bool { | |
nblack := 0 | |
for x := m.Bounds().Min.X; x < m.Bounds().Max.X; x++ { | |
r, g, b, _ := m.At(x, y).RGBA() | |
if r == 0 && g == 0 && b == 0 { | |
nblack++ | |
} | |
} | |
if nblack >= (m.Bounds().Max.X-m.Bounds().Min.X)/2 { | |
return true | |
} else { | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment