當前位置: 首頁>>代碼示例>>Golang>>正文


Golang png.Decode函數代碼示例

本文整理匯總了Golang中image/png.Decode函數的典型用法代碼示例。如果您正苦於以下問題:Golang Decode函數的具體用法?Golang Decode怎麽用?Golang Decode使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Decode函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: main

func main() {

	f, err := os.Open(os.Args[1])
	if err != nil {
		fmt.Println(err)
		return
	}
	defer f.Close()
	i50, err := png.Decode(f)
	if err != nil {
		fmt.Println(err)
		return
	}
	if f, err = os.Open(os.Args[2]); err != nil {
		fmt.Println(err)
		return
	}
	defer f.Close()
	i100, err := png.Decode(f)
	if err != nil {
		fmt.Println(err)
		return
	}
	if i50.ColorModel() != i100.ColorModel() {
		fmt.Println("different color models")
		return
	}
	b := i50.Bounds()
	if !b.Eq(i100.Bounds()) {
		fmt.Println("different image sizes")
		return
	}
	var sum int64
	for y := b.Min.Y; y < b.Max.Y; y++ {
		for x := b.Min.X; x < b.Max.X; x++ {
			r1, g1, b1, _ := i50.At(x, y).RGBA()
			r2, g2, b2, _ := i100.At(x, y).RGBA()
			if r1 > r2 {
				sum += int64(r1 - r2)
			} else {
				sum += int64(r2 - r1)
			}
			if g1 > g2 {
				sum += int64(g1 - g2)
			} else {
				sum += int64(g2 - g1)
			}
			if b1 > b2 {
				sum += int64(b1 - b2)
			} else {
				sum += int64(b2 - b1)
			}
		}
	}
	nPixels := (b.Max.X - b.Min.X) * (b.Max.Y - b.Min.Y)
	fmt.Printf("Image difference: %f%%\n",
		float64(sum*100)/(float64(nPixels)*0xffff*3))
}
開發者ID:engineerball,項目名稱:BDD,代碼行數:58,代碼來源:compareimage.go

示例3: init

func init() {
	var err error

	Terrain, err = png.Decode(bytes.NewReader(TerrainPng))
	if err != nil {
		panic(err)
	}
	TerrainPng = nil

	Actors, err = png.Decode(bytes.NewReader(ActorsPng))
	if err != nil {
		panic(err)
	}
	ActorsPng = nil
}
開發者ID:Nightgunner5,項目名稱:gogame,代碼行數:15,代碼來源:image.go

示例4: fixtureImageObj

func fixtureImageObj(fpath string) image.Image {
	f, _ := os.Open(fpath)
	buf, _ := ioutil.ReadFile(f.Name())
	img, _ := png.Decode(bytes.NewReader(buf))
	// *image.RGBA
	return img
}
開發者ID:rockdean,項目名稱:gosseract,代碼行數:7,代碼來源:servant_test.go

示例5: loadSize

//func loadSize(ctxt *fs.Context, name string, max int) *image.RGBA
func loadSize(name string, max int) *image.RGBA {
	//data, _, err := ctxt.Read("qr/upload/" + name + ".png")
	f1, err := os.Open(name + ".png")
	fmt.Println(name + ".png")
	if err != nil {
		panic(err)
	}
	i, err := png.Decode(f1)
	if err != nil {
		panic(err)
	}
	b := i.Bounds()
	fmt.Printf("%v, %v,max%v", b.Dx(), b.Dy(), max)
	dx, dy := max, max
	if b.Dx() > b.Dy() {
		dy = b.Dy() * dx / b.Dx()
	} else {
		dx = b.Dx() * dy / b.Dy()
	}
	fmt.Printf("%v, %v,", dx, dy)
	var irgba *image.RGBA
	switch i := i.(type) {
	case *image.RGBA:
		irgba = resize.ResizeRGBA(i, i.Bounds(), dx, dy)
	case *image.NRGBA:
		irgba = resize.ResizeNRGBA(i, i.Bounds(), dx, dy)
	default:
		fmt.Println("default")

	}
	fmt.Println("prereturnload")
	fmt.Printf("%v, %v,", irgba.Bounds().Dx(), irgba.Bounds().Dy())
	return irgba
}
開發者ID:GreatJoy1203,項目名稱:QArTag,代碼行數:35,代碼來源:play.go

示例6: main

func main() {
	if len(os.Args) != 2 {
		fmt.Println("Please select one picture to makeascii...")
		return
	}
	fileName := os.Args[1]
	isJpeg, _ := regexp.MatchString(".+\\.jpg", fileName)
	isPng, _ := regexp.MatchString(".+\\.png", fileName)
	var picture image.Image
	var imageErr error
	fileIn, errIn := os.Open(fileName)
	if errIn != nil {
		fmt.Println(errIn.Error())
		return
	}
	if isJpeg {
		picture, imageErr = jpeg.Decode(fileIn)
	} else if isPng {
		picture, imageErr = png.Decode(fileIn)
	} else {
		fmt.Println("File type is not supported...")
		return
	}
	if imageErr != nil {
		fmt.Println(imageErr.Error())
		return
	}
	fmt.Print(MakeAscii(GetImage(picture)))
}
開發者ID:bjh83,項目名稱:makeascii,代碼行數:29,代碼來源:makeascii.go

示例7: createTexture

func createTexture(r io.Reader) (textureId gl.Uint, err error) {
	img, err := png.Decode(r)
	if err != nil {
		return 0, err
	}

	rgbaImg, ok := img.(*image.NRGBA)
	if !ok {
		return 0, errors.New("texture must be an NRGBA image")
	}

	gl.GenTextures(1, &textureId)
	gl.BindTexture(gl.TEXTURE_2D, textureId)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)

	// flip image: first pixel is lower left corner
	imgWidth, imgHeight := img.Bounds().Dx(), img.Bounds().Dy()
	data := make([]byte, imgWidth*imgHeight*4)
	lineLen := imgWidth * 4
	dest := len(data) - lineLen
	for src := 0; src < len(rgbaImg.Pix); src += rgbaImg.Stride {
		copy(data[dest:dest+lineLen], rgbaImg.Pix[src:src+rgbaImg.Stride])
		dest -= lineLen
	}
	gl.TexImage2D(gl.TEXTURE_2D, 0, 4, gl.Sizei(imgWidth), gl.Sizei(imgHeight), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&data[0]))

	return textureId, nil
}
開發者ID:sserafimescu,項目名稱:gogl,代碼行數:29,代碼來源:gopher.go

示例8: TestDescriptor

func TestDescriptor(t *testing.T) {
	square, err := os.Open("../test/images/square.png")
	if err != nil {
		t.Fatal(err)
	}
	img, err := png.Decode(square)

	points := make(chan image.Point, 2)

	go func() {
		points <- image.Point{16, 16}
		close(points)
	}()

	desc := make(chan Descriptor)
	CalcDescriptors(points, img, desc, RGBPointValue)

	i := 0
	for d := range desc {
		if d.base != 16191 {
			t.Logf("Base value of descriptor should be 15, instead it was %v", d.base)
			t.Fail()
		}
		i++
	}
	if i != 1 {
		t.Logf("CalcDescriptor should have returned exactly 1 descriptor, given 1 point")
		t.Fail()
	}
}
開發者ID:jimmiebtlr,項目名稱:cv,代碼行數:30,代碼來源:descriptor_test.go

示例9: readTarget

func readTarget(name string) ([][]int, error) {
	f, err := os.Open(name)
	if err != nil {
		return nil, err
	}
	m, err := png.Decode(f)
	if err != nil {
		return nil, fmt.Errorf("decode %s: %v", name, err)
	}
	rect := m.Bounds()
	target := make([][]int, rect.Dy())
	for i := range target {
		target[i] = make([]int, rect.Dx())
	}
	for y, row := range target {
		for x := range row {
			a := int(color.RGBAModel.Convert(m.At(x, y)).(color.RGBA).A)
			t := int(color.GrayModel.Convert(m.At(x, y)).(color.Gray).Y)
			if a == 0 {
				t = -1
			}
			row[x] = t
		}
	}
	return target, nil
}
開發者ID:TheJumpCloud,項目名稱:rsc,代碼行數:26,代碼來源:play.go

示例10: main

func main() {
	var file *os.File
	var outFile *os.File
	var img image.Image
	var err error

	if file, err = os.Open("pkg.png"); err != nil {
		println("Error", err)
		return
	}
	defer file.Close()

	if img, err = png.Decode(file); err != nil {
		println("Error", err)
		return
	}

	if outFile, err = os.Create("out_pkg.jpeg"); err != nil {
		println("Error", err)
		return
	}

	option := &jpeg.Options{Quality: 100}
	if err = jpeg.Encode(outFile, img, option); err != nil {
		println()
		return
	}
	defer outFile.Close()
}
開發者ID:qt-luigi,項目名稱:golangcafe,代碼行數:29,代碼來源:pngtojpg.go

示例11: CountColor

func CountColor(_png io.Reader) int {
	/* modify here */
	config, err := png.DecodeConfig(_png)
	if err != nil {
		log.Fatal(err)
	}

	width := config.Width * 10
	height := config.Height * 10

	_png = GetPngBinary()

	img, err := png.Decode(_png)
	if err != nil {
		log.Fatal(err)
	}

	var r, g, b, a uint32
	var str string
	m := make(map[string]int)
	for x := 0; x < width; x++ {
		for y := 0; y < height; y++ {

			r, g, b, a = img.At(x, y).RGBA()
			str = strconv.Itoa(int(r)) + strconv.Itoa(int(g)) + strconv.Itoa(int(b)) + strconv.Itoa(int(a))
			m[str] = 1
		}
	}

	return len(m - 1)
}
開發者ID:takkaw,項目名稱:gdd2011_golang,代碼行數:31,代碼來源:png.go

示例12: convertPNGToJPEG

func convertPNGToJPEG(w io.Writer, r io.Reader, quality int) error {
	img, err := png.Decode(r)
	if err != nil {
		return err
	}
	return jpeg.Encode(w, img, &jpeg.Options{Quality: quality})
}
開發者ID:mateuszdyminski,項目名稱:png2jpeg,代碼行數:7,代碼來源:png2jpeg.go

示例13: main

func main() {
	flag.Parse()

	if len(flag.Args()) != 1 {
		fmt.Println("Usage: goicns -o IconSet.icns image.png")
		return
	}

	path := flag.Args()[0]

	f, err := os.OpenFile(path, os.O_RDONLY, 0600)
	if err != nil {
		fmt.Println("Error opening image %s: %s", path, err.Error())
		return
	}

	img, err := png.Decode(f)
	if err != nil {
		fmt.Println("Error decoding image %s: %s", path, err.Error())
		return
	}

	icns := goicns.NewICNS(img)
	if err = icns.Construct(); err != nil {
		fmt.Println("Error encofing ICNS %s: %s", path, err.Error())
		return
	}

	icns.WriteToFile(*flOut, 0666)
}
開發者ID:tonyhb,項目名稱:goicns,代碼行數:30,代碼來源:main.go

示例14: main

func main() {
	pngImgFile, err := os.Open("./2.png")

	if err != nil {
		fmt.Println("2.png file not found!")
		os.Exit(1)
	}

	defer pngImgFile.Close()

	imgSrc, err := png.Decode(pngImgFile)

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	newImg := image.NewRGBA(imgSrc.Bounds())
	draw.Draw(newImg, newImg.Bounds(), &image.Uniform{color.White}, image.Point{}, draw.Src)

	draw.Draw(newImg, newImg.Bounds(), imgSrc, imgSrc.Bounds().Min, draw.Over)

	jpgImgFile, err := os.Create("./draw.jpg")

	if err != nil {
		fmt.Println("Cannot create draw.jpg !")
		fmt.Println(err)
		os.Exit(1)
	}

	defer jpgImgFile.Close()
}
開發者ID:Chubik,項目名稱:testgolang,代碼行數:31,代碼來源:mask.go

示例15: run

func run() error {
	qml.Init(nil)
	engine := qml.NewEngine()
	engine.AddImageProvider("pwd", func(id string, width, height int) image.Image {
		f, err := os.Open(id)
		if err != nil {
			panic(err)
		}
		defer f.Close()
		image, err := png.Decode(f)
		if err != nil {
			panic(err)
		}
		return image
	})

	component, err := engine.LoadFile("imgprovider.qml")
	if err != nil {
		return err
	}

	win := component.CreateWindow(nil)
	win.Show()
	win.Wait()

	return nil
}
開發者ID:RickyS,項目名稱:qml,代碼行數:27,代碼來源:imgprovider.go


注:本文中的image/png.Decode函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。