Crop an Image to a Circular Area in Go

Via our Image API, dozens of image processing operations are available with just a single Cloudmersive API key for authentication. Easily crop an image into a specified circular area with pixel values as parameters using ready-to-run code in Go (included below for your convenience) or in any of 12 other programming languages available on our Cloudmersive API Console page.

package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/image/edit/crop/circle/%3Cinteger%3E/%3Cinteger%3E/%3Cinteger%3E"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("imageFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

New to Cloudmersive APIs? You’ll need an API key for access, so head to our website (www.cloudmersive.com) and create an account to receive your key. Among our account options, you’ll find a free-tier account is available with an 800 call-per-month limit.

--

--

--

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Transferring Color Attributes in RenderMan for Houdini (and a few other quirks)

Devel HTB Walk Through [Part-01]

How to Pass data to view from Controller ?

Remington Reed Completes AEM Sites Architect Certification

How EMS Simplifies Expense Reporting Process End-to-End?

Grokking Time Travel in Snowflake

How to change the timezone dynamically in laravel

Compact ways to Deploy bots on Raspberry Pi and Push updates.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloudmersive

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

More from Medium

How to Crop an Image to a Rectangular Area: Use the Cloudmersive Image API in Go

Go: my first impressions

Go & Docker Hello World!

Go build go-sqlite3