SHARE
TWEET

Untitled

a guest Jun 15th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "bytes"
  5.     "image"
  6.     "image/color"
  7.     "image/draw"
  8.     "image/jpeg"
  9.     "io/ioutil"
  10.     "log"
  11.     "math"
  12.     "os"
  13. )
  14.  
  15. func main() {
  16.     f, err := os.Open("resources/image.jpg")
  17.     if err != nil {
  18.         log.Fatal(err)
  19.     }
  20.  
  21.     //encoding message is discarded, because OP wanted only jpg, else use encoding in resize function
  22.     img, _, err := image.Decode(f)
  23.     if err != nil {
  24.         log.Fatal(err)
  25.     }
  26.  
  27.     //this is the resized image
  28.     resImg := resize(img, 20, 20)
  29.  
  30.     //this is the resized image []bytes
  31.     imgBytes := imgToBytes(resImg)
  32.  
  33.     //optional written to file
  34.     err = ioutil.WriteFile("resources/test.jpg", imgBytes, 0777)
  35.     if err != nil {
  36.         log.Fatal(err)
  37.     }
  38. }
  39.  
  40. func resize(img image.Image, length int, width int) image.Image {
  41.     //truncate pixel size
  42.     minX := img.Bounds().Min.X
  43.     minY := img.Bounds().Min.Y
  44.     maxX := img.Bounds().Max.X
  45.     maxY := img.Bounds().Max.Y
  46.     for (maxX-minX)%length != 0 {
  47.         maxX--
  48.     }
  49.     for (maxY-minY)%width!= 0 {
  50.         maxY--
  51.     }
  52.     scaleX := (maxX - minX) / length
  53.     scaleY := (maxY - minY) / width
  54.  
  55.     imgRect := image.Rect(0, 0, length, width)
  56.     resImg := image.NewRGBA(imgRect)
  57.     draw.Draw(resImg, resImg.Bounds(), &image.Uniform{C: color.White}, image.ZP, draw.Src)
  58.     for y := 0; y < width; y += 1 {
  59.         for x := 0; x < length; x += 1 {
  60.             averageColor := getAverageColor(img, minX+x*scaleX, minX+(x+1)*scaleX, minY+y*scaleY, minY+(y+1)*scaleY)
  61.             resImg.Set(x, y, averageColor)
  62.         }
  63.     }
  64.     return resImg
  65. }
  66.  
  67. func getAverageColor(img image.Image, minX int, maxX int, minY int, maxY int) color.Color {
  68.     var averageRed float64
  69.     var averageGreen float64
  70.     var averageBlue float64
  71.     var averageAlpha float64
  72.     scale := 1.0 / float64((maxX-minX)*(maxY-minY))
  73.  
  74.     for i := minX; i < maxX; i++ {
  75.         for k := minY; k < maxY; k++ {
  76.             r, g, b, a := img.At(i, k).RGBA()
  77.             averageRed += float64(r) * scale
  78.             averageGreen += float64(g) * scale
  79.             averageBlue += float64(b) * scale
  80.             averageAlpha += float64(a) * scale
  81.         }
  82.     }
  83.  
  84.     averageRed = math.Sqrt(averageRed)
  85.     averageGreen = math.Sqrt(averageGreen)
  86.     averageBlue = math.Sqrt(averageBlue)
  87.     averageAlpha = math.Sqrt(averageAlpha)
  88.  
  89.     averageColor := color.RGBA{
  90.         R: uint8(averageRed),
  91.         G: uint8(averageGreen),
  92.         B: uint8(averageBlue),
  93.         A: uint8(averageAlpha)}
  94.  
  95.     return averageColor
  96. }
  97.  
  98. func imgToBytes(img image.Image) []byte {
  99.     var opt jpeg.Options
  100.     opt.Quality = 80
  101.  
  102.     buff := bytes.NewBuffer(nil)
  103.     err := jpeg.Encode(buff, img, &opt)
  104.     if err != nil {
  105.         log.Fatal(err)
  106.     }
  107.  
  108.     return buff.Bytes()
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top