Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The program below takes an input file name and an output file name. It opens the input file, decodes it, converts it to grayscale, then encodes it to the output file.</p> <p>Thie program isn't specific to PNGs, but to support other file formats you'd have to import the correct image package. For example, to add JPEG support you could add to the imports list <code>_ "image/jpeg"</code>.</p> <p>If you <em>only</em> want to support PNG, then you can use <a href="http://golang.org/pkg/image/png/#Decode" rel="noreferrer">image/png.Decode</a> directly instead of <a href="http://golang.org/pkg/image/#Decode" rel="noreferrer">image.Decode</a>.</p> <pre><code>package main import ( "image" "image/png" // register the PNG format with the image package "os" ) func main() { infile, err := os.Open(os.Args[1]) if err != nil { // replace this with real error handling panic(err.String()) } defer infile.Close() // Decode will figure out what type of image is in the file on its own. // We just have to be sure all the image packages we want are imported. src, _, err := image.Decode(infile) if err != nil { // replace this with real error handling panic(err.String()) } // Create a new grayscale image bounds := src.Bounds() w, h := bounds.Max.X, bounds.Max.Y gray := image.NewGray(w, h) for x := 0; x &lt; w; x++ { for y := 0; y &lt; h; y++ { oldColor := src.At(x, y) grayColor := image.GrayColorModel.Convert(oldColor) gray.Set(x, y, grayColor) } } // Encode the grayscale image to the output file outfile, err := os.Create(os.Args[2]) if err != nil { // replace this with real error handling panic(err.String()) } defer outfile.Close() png.Encode(outfile, gray) } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload