How to convert from type color.Gray to type int

Asked 2 years ago, Updated 2 years ago, 47 views

I look forward to your kind cooperation.

Currently, I would like to use GO language to extract pixel intensity from PNG images, but there is a problem, so I would like to ask you a question.

Here's the code:

package main

import(
            "bytes"
            "io/ioutil"
            "fmt"
            "image/color"
            "image/png"
        )

import "github.com/harrydb/go/img/grayscale"

funcmain(){

  FName_BG: = "test.background.png";
  data_BG,_ —=ioutil.ReadFile(FName_BG)
  img_BG,_ —=png.Decode (bytes.NewReader(data_BG))
  grayImg_BG: = grayscale.Convert(img_BG, grayscale.ToGrayLuminance)

    MaxRowSize, MaxColSize: = img_BG.Bounds().Max.Y, img_BG.Bounds().Max.X;

    fory —=0;y<MaxRowSize;y++{
         for x: = 0; x <MaxColSize; x++ {
             c1_bg:=grayImg_BG.At(x,y).(color.Gray);
             fmt.Println(c1_bg)
         }
     }
}

In the code above, we are trying to convert the PNG image to Grayscale and get pixel intensity.

The problem is that the one shown in fmt.Println(c1_bg) is

{255}
{255}
{255}
{255}

As shown in , it is displayed in color gray.I just want this to be an integer.
(Specifically, I just want to look like {255}->255.)

If anyone knows, please let me know.

Thank you for your cooperation.

go

2022-09-29 22:50

1 Answers

The Definition in color.Gray is as follows, so

typeGray structure{
        Youint8
}

If you just want an integer value, you can go to Y.

fmt.Println(c.Y)

(Sample code on Go Playground

)


2022-09-29 22:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.