{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import (\n", " \"bytes\"\n", " \"image\"\n", " \"image/color\"\n", " \"image/draw\"\n", " \"image/png\"\n", " \"log\"\n", " \"math/rand\"\n", ")\n", "\n", "func newImage() image.Image {\n", " sz := 50\n", " // create the whole image canvas\n", " canvas := image.Rect(0, 0, 2*sz, 2*sz)\n", " img := image.NewRGBA(canvas)\n", " \n", " \n", " // draw a gray background\n", " //bkg := color.RGBA{0x66, 0x66, 0x66, 0xff}\n", " bkg := color.RGBA{102, 102, 102, 255}\n", " draw.Draw(img, canvas, image.NewUniform(bkg), image.ZP, draw.Src)\n", " \n", " // create a randomly sized, randomly centered, red square\n", " x1 := rand.Intn(sz)\n", " y1 := rand.Intn(sz)\n", " x2 := rand.Intn(sz) + sz\n", " y2 := rand.Intn(sz) + sz\n", " \n", " // red foreground\n", " red := color.RGBA{255,0,0,255}\n", " draw.Draw(\n", " img, \n", " image.Rect(x1, y1, x2, y2),\n", " image.NewUniform(red), \n", " image.ZP, draw.Src,\n", " )\n", " return img\n", "}\n", "\n", "func getRaw(img image.Image) []byte {\n", " buf := bytes.Buffer{}\n", " err := png.Encode(&buf, img)\n", " if err != nil {\n", " log.Printf(\"could not encode to PNG: %v\", err)\n", " return nil\n", " }\n", " return buf.Bytes()\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "img := newImage()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(img)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(getRaw(img))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import \"encoding/hex\"\n", "print(hex.Dump(getRaw(img)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "img" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "getRaw(img)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(1+2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "newImage()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "func get() (int, int) { return 42, 666 }" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "get()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import \"encoding/hex\"\n", "hex.Dump(getRaw(newImage()))" ] } ], "metadata": { "kernelspec": { "display_name": "Go", "language": "go", "name": "go" }, "language_info": { "codemirror_mode": "go", "file_extension": ".go", "mimetype": "", "name": "go", "nbconvert_exporter": "", "pygments_lexer": "go", "version": "unreleased" } }, "nbformat": 4, "nbformat_minor": 2 }