Golang Program to Read an Image File From Local File Path System and Display it Full Project For Beginners

  • Post author:
  • Post category:Go
  • Post comments:0 Comments

 

 

app.go

 

 

package main

import (
  "fmt"
  "image"
  "image/png"
  "os"
)

func main() {
  // Read image from file that already exists
  existingImageFile, err := os.Open("test.png")
  if err != nil {
    // Handle error
  } 

  defer existingImageFile.Close()

  // Calling the generic image.Decode() will tell give us the data
  // and type of image it is as a string. We expect "png"
  imageData, imageType, err := image.Decode(existingImageFile)
  if err != nil {
    // Handle error
  }
  fmt.Println(imageData)
  fmt.Println(imageType)

  // We only need this because we already read from the file
  // We have to reset the file pointer back to beginning
  existingImageFile.Seek(0, 0)

  // Alternatively, since we know it is a png already
  // we can call png.Decode() directly
  loadedImage, err := png.Decode(existingImageFile)
  if err != nil {
    // Handle error
    }
  fmt.Println(loadedImage)
 }

Leave a Reply