Created
October 16, 2020 13:30
-
-
Save rikkix/0b9ea466db10d38572ccd33ae33368a0 to your computer and use it in GitHub Desktop.
golang png alpha channel issue
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 hidden_test | |
import ( | |
"image" | |
"image/color" | |
_ "image/jpeg" | |
"image/png" | |
"net/http" | |
"os" | |
"testing" | |
"github.com/chai2010/webp" | |
) | |
func pixCcu(c color.Color) color.Color { | |
rr, gg, bb, _ := c.RGBA() | |
a := (rr + gg + bb) / 3 | |
return color.RGBA64{ | |
R: 65525, | |
G: 65525, | |
B: 65525, | |
A: uint16(a), | |
} | |
} | |
func Transform(img image.Image) (image.Image, error) { | |
XBounds, YBounds := img.Bounds().Max.X, img.Bounds().Max.Y | |
outImg := image.NewRGBA64(image.Rect(0, 0, XBounds, YBounds)) | |
for y := 0; y < YBounds; y++ { | |
for x := 0; x < XBounds; x++ { | |
outImg.Set(x, y, pixCcu(img.At(x, y))) | |
} | |
} | |
return outImg, nil | |
} | |
func TestTransform(t *testing.T) { | |
src, err := http.Get("https://key.visualarts.gr.jp/summer_rb/common/image/gallery_cg_02_l.jpg") | |
if err != nil { | |
t.Error(err) | |
} | |
img, _, err := image.Decode(src.Body) | |
if err != nil { | |
t.Error(err) | |
} | |
outImg, err := Transform(img) | |
if err != nil { | |
t.Error(err) | |
} | |
file, err := os.Create("output.webp") | |
if err != nil { | |
t.Error(err) | |
} | |
err = webp.Encode(file, outImg, &webp.Options{Lossless: true}) | |
if err != nil { | |
t.Error(err) | |
} | |
file1, err := os.Create("output.png") | |
if err != nil { | |
t.Error(err) | |
} | |
err = png.Encode(file1, outImg) | |
if err != nil { | |
t.Error(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment