-2

So I have a line of code like this:

func TestImage(){
    img, _ := imgio.Open(`input.jpg`)
    inverted := effect.Invert(img)
    f, _ := os.Create("output.jpg")
    defer f.Close()
    Encoder := imgio.JPEGEncoder(80)
    Encoder(f, inverted)
}

All it does is just invert an image, which is simple enough. However, it can only do so to local file. So, say, if I have an image on site A that needs to be download, modify and upload back, I would have to

  • Download the image to a local storage
  • Load it
  • Modify using that function
  • Save it to the local storage
  • Load it again to a POST function for uploading or something

I was wondering if there is anyway to achieve such task with out reading to save it and load it from a local storage? Like save the image as a "fake" file, which is a byte buffer or something.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
CSDD
  • 339
  • 2
  • 14
  • 1
    You're not showing the function signatures for `imgio.*`, `effect.*`, and `Encoder`. If they take `io.Reader` and `io.Writer` interfaces (which they should), they can take a byte buffer. But we can't tell. – Marc Jul 29 '20 at 07:42

1 Answers1

1

Package imgio provides basic image file input/output.

If you don't want to read from and write to files, don't use this package. Use the image package in the standard library directly.

package main

import (
    "bytes"
    "image"
    "image/jpeg"

    "github.com/anthonynsimon/bild/imgio"
)

func TestImage() {
    input := new(bytes.Buffer)
    img, err := image.Decode(input)
    // TODO: handle error

    output := new(bytes.Buffer)
    err = jpeg.Encode(output, img, &jpeg.Options{Quality: 80})
}
Peter
  • 29,454
  • 5
  • 48
  • 60