本文整理汇总了Golang中image.Decode函数的典型用法代码示例。如果您正苦于以下问题:Golang Decode函数的具体用法?Golang Decode怎么用?Golang Decode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Decode函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: waterMark
//水印
func waterMark(picBytes []byte) []byte {
// 打开水印图并解码
img, fileType, _ := image.Decode(bytes.NewBuffer(picBytes))
//读取水印图片
watermark, _ := png.Decode(bytes.NewBuffer(wm))
//原始图界限
origin_size := img.Bounds()
//创建新图层
canvas := image.NewNRGBA(origin_size)
//贴原始图
draw.Draw(canvas, origin_size, img, image.ZP, draw.Src)
//贴水印图
draw.Draw(canvas, watermark.Bounds().Add(image.Pt(origin_size.Dx()-watermark.Bounds().Dx(), origin_size.Dy()-watermark.Bounds().Dy()-4)), watermark, image.ZP, draw.Over)
//生成新图片
buff := bytes.NewBuffer([]byte{})
switch fileType {
case "jpeg":
jpeg.Encode(buff, canvas, &jpeg.Options{95})
default:
png.Encode(buff, canvas)
}
return buff.Bytes()
}
开发者ID:elvizlai,项目名称:MGBlog,代码行数:31,代码来源:file.go
示例2: decode
// decoder reads an image from r and modifies the image as defined by opts.
// swapDimensions indicates the decoded image will be rotated after being
// returned, and when interpreting opts, the post-rotation dimensions should
// be considered.
// The decoded image is returned in im. The registered name of the decoder
// used is returned in format. If the image was not successfully decoded, err
// will be non-nil. If the decoded image was made smaller, needRescale will
// be true.
func decode(r io.Reader, opts *DecodeOpts, swapDimensions bool) (im image.Image, format string, err error, needRescale bool) {
if opts == nil {
// Fall-back to normal decode.
im, format, err = image.Decode(r)
return im, format, err, false
}
var buf bytes.Buffer
tr := io.TeeReader(r, &buf)
ic, format, err := image.DecodeConfig(tr)
if err != nil {
return nil, "", err, false
}
mr := io.MultiReader(&buf, r)
b := image.Rect(0, 0, ic.Width, ic.Height)
sw, sh, needRescale := opts.rescaleDimensions(b, swapDimensions)
if !needRescale {
im, format, err = image.Decode(mr)
return im, format, err, false
}
imageDebug(fmt.Sprintf("Resizing from %dx%d -> %dx%d", ic.Width, ic.Height, sw, sh))
if format == "cr2" {
// Replace mr with an io.Reader to the JPEG thumbnail embedded in a
// CR2 image.
if mr, err = cr2.NewReader(mr); err != nil {
return nil, "", err, false
}
format = "jpeg"
}
if format == "jpeg" && fastjpeg.Available() {
factor := fastjpeg.Factor(ic.Width, ic.Height, sw, sh)
if factor > 1 {
var buf bytes.Buffer
tr := io.TeeReader(mr, &buf)
im, err = fastjpeg.DecodeDownsample(tr, factor)
switch err.(type) {
case fastjpeg.DjpegFailedError:
log.Printf("Retrying with jpeg.Decode, because djpeg failed with: %v", err)
im, err = jpeg.Decode(io.MultiReader(&buf, mr))
case nil:
// fallthrough to rescale() below.
default:
return nil, format, err, false
}
return rescale(im, sw, sh), format, err, true
}
}
// Fall-back to normal decode.
im, format, err = image.Decode(mr)
if err != nil {
return nil, "", err, false
}
return rescale(im, sw, sh), format, err, needRescale
}
开发者ID:rfistman,项目名称:camlistore,代码行数:66,代码来源:images.go
示例3: CheckImage
func CheckImage(t TesterLight, img []byte, sample []byte) {
i1, _, err := image.Decode(bytes.NewReader(img))
if err != nil {
t.Fatal(err)
}
i2, _, err := image.Decode(bytes.NewReader(sample))
if err != nil {
t.Fatal(err)
}
if !i1.Bounds().Eq(i2.Bounds()) {
t.Fatalf("Different image bounds: %v and %v", i1.Bounds(), i2.Bounds())
}
bounds := i1.Bounds()
for x := bounds.Min.X; x < bounds.Max.Y; x++ {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
c1 := i1.At(x, y)
c2 := i2.At(x, y)
if !CmpColors(c1, c2, 30) {
t.Fatalf("Different colors at (%v, %v): %v vs %v", x, y, c1, c2)
}
}
}
}
开发者ID:mikhailborodin,项目名称:gopnik,代码行数:26,代码来源:testtile.go
示例4: TestPHA
func TestPHA(t *testing.T) {
// Load picture from file.
infile, err := os.Open("./testdata/pic_output.png")
if err != nil {
t.Errorf("Load picture: %s.", err)
}
// Decode picture.
srcImg, _, err := image.Decode(infile)
infile.Close()
if err != nil {
t.Errorf("Decode picture: %s.", err)
}
fg := PHA(srcImg)
fmt.Println("Fingerprint:", fg)
// Load picture2 from file.
infile, err = os.Open("./testdata/pic_output2.jpg")
if err != nil {
t.Errorf("Load picture2: %s.", err)
}
// Decode picture.
srcImg, _, err = image.Decode(infile)
infile.Close()
if err != nil {
t.Errorf("Decode picture2: %s.", err)
}
fg2 := PHA(srcImg)
fmt.Println("Fingerprint:", fg2)
fmt.Println("Diff num:", CompareDiff(fg, fg2))
}
开发者ID:jango2015,项目名称:sdc,代码行数:35,代码来源:gopha_test.go
示例5: pixels
func pixels(source, target string) bool {
sourcefile, err := os.Open(source)
checkerror(err)
defer sourcefile.Close()
targetfile, err := os.Open(target)
checkerror(err)
defer targetfile.Close()
sourceimage, _, err := image.Decode(sourcefile)
targetimage, _, err := image.Decode(targetfile)
sourcebounds := sourceimage.Bounds()
targetbounds := targetimage.Bounds()
if (sourcebounds.Min.Y != targetbounds.Min.Y) || (sourcebounds.Min.X != targetbounds.Min.X) || (sourcebounds.Max.Y != targetbounds.Max.Y) || (sourcebounds.Max.X != targetbounds.Max.X) {
return false
// log.Fatalln("Images are not the same size pixel-wise!")
}
for y := sourcebounds.Min.Y; y < sourcebounds.Max.Y; y++ {
for x := sourcebounds.Min.X; x < sourcebounds.Max.X; x++ {
sr, sg, sb, sa := sourceimage.At(x, y).RGBA()
tr, tg, tb, ta := targetimage.At(x, y).RGBA()
if (sr != tr) || (sg != tg) || (sb != tb) || (sa != ta) {
return false
// log.Fatalln("Ah! They are not the same!", x, y)
}
}
}
return true
}
开发者ID:NovemberFoxtrot,项目名称:ratcliff,代码行数:34,代码来源:ratcliff.go
最后编辑: kuteng 文档更新时间: 2021-08-23 19:14 作者:kuteng