Search code examples
gojpegoptim

Implement io.Reader from image.RGBA


I need a little hint. I'm creating thumbnails of images in Go and would like to pass them to jpegoptim for crushing.

jpegoptim has the --stdin and --stdout flags, which I would like to use. Now, I don't want to save the generated image to disk first, but convert my *image.RGBA to something that implements io.Reader, so I can pass it to exec.Cmd.Stdin

I'm a little lost on how I could achieve this, would be great if someone can point me in the right direction.

Thanks


Solution

  • In this case, you don't need to implement your own io.Reader. Use io.Pipe and jpeg.Encode, e.g.

    func main() {
        //Prepare image ...
        img := ...
    
        //Prepare output (file etc.) ...
        outFile := ...
    
        //Use pipe to connect JPEG encoder output to cmd's STDIN
        pr, pw := io.Pipe()
    
        //Exec jpegoptim in goroutine
        done := make(chan bool, 1)
        go func() {
            //execute command
            cmdErr := bytes.Buffer{}
            cmd := exec.Command("jpegoptim", "--stdin", "--verbose")
            cmd.Stdin = pr       //image input from PIPE
            cmd.Stderr = &cmdErr //message
            cmd.Stdout = outFile //optimize image output
    
            if err := cmd.Run(); err != nil {
                // handle error
            }
            fmt.Printf("Result: %s\n", cmdErr.String())
            close(done)
        }()
    
        //ENCODE image to JPEG then write to PIPE
        o := jpeg.Options{Quality: 90}
        jpeg.Encode(pw, img, &o)
    
        //When done, close the PIPE
        if err := pw.Close(); err != nil {
            // handle error
        }
    
        //Wait for jpegoptim
        <-done
    }