Image to Image Go Examples
Complete Go examples for common Image to Image tasks. See the Go SDK guide for installation and result handling.
You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed. Each Go example can be copied and run independently.
Basic examples
Choose file, in-memory, or output-stream handling for the JPEG result. Stream examples use a binary file stream as their destination.
Convert an image file
Upload an existing logo.png file.
Save to a file
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetOutputFormat("jpg") err := client.ConvertFileToFile("logo.png", "logo.jpg") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Keep in memory
package main import ( "log" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetOutputFormat("jpg") image, err := client.ConvertFile("logo.png") // []byte checkError(err) fmt.Println(len(image)) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Write to a stream
package main import ( "log" "os" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetOutputFormat("jpg") output_stream, err := os.Create("logo.jpg") checkError(err) err = client.ConvertFileToStream("logo.png", output_stream) closeErr := output_stream.Close() checkError(err) checkError(closeErr) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Convert a URL
Fetch an image from its URL.
Save to a file
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetOutputFormat("jpg") err := client.ConvertUrlToFile( "https://your-server.com/logo.png", "logo.jpg", ) checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Keep in memory
package main import ( "log" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetOutputFormat("jpg") image, err := client.ConvertUrl("https://your-server.com/logo.png") // []byte checkError(err) fmt.Println(len(image)) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Write to a stream
package main import ( "log" "os" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetOutputFormat("jpg") output_stream, err := os.Create("logo.jpg") checkError(err) err = client.ConvertUrlToStream( "https://your-server.com/logo.png", output_stream, ) closeErr := output_stream.Close() checkError(err) checkError(closeErr) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Convert raw image bytes
Read logo.png as binary data. In an application, pass the bytes you already hold in memory instead.
Save to a file
package main import ( "log" "os" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") image_data_1, err := os.ReadFile("logo.png") // []byte checkError(err) client.SetOutputFormat("jpg") err = client.ConvertRawDataToFile(image_data_1, "logo.jpg") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Keep in memory
package main import ( "log" "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") image_data_1, err := os.ReadFile("logo.png") // []byte checkError(err) client.SetOutputFormat("jpg") image, err := client.ConvertRawData(image_data_1) // []byte checkError(err) fmt.Println(len(image)) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Write to a stream
package main import ( "log" "os" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") image_data_1, err := os.ReadFile("logo.png") // []byte checkError(err) client.SetOutputFormat("jpg") output_stream, err := os.Create("logo.jpg") checkError(err) err = client.ConvertRawDataToStream(image_data_1, output_stream) closeErr := output_stream.Close() checkError(err) checkError(closeErr) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Read from an input stream
Open logo.png as a binary input stream and save the result to logo.jpg.
package main import ( "log" "os" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetOutputFormat("jpg") input_stream, err := os.Open("logo.png") checkError(err) err = client.ConvertStreamToFile(input_stream, "logo.jpg") closeErr := input_stream.Close() checkError(err) checkError(closeErr) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Transform the image
These examples use scan.png, a 1224 × 1584 pixel image with a chart and a uniform white border. Rotation uses scan-sideways.png; JPEG background replacement uses transparent-logo.png.
Resize an image
Fit scan.png within 400 × 400 pixels while preserving its aspect ratio. The result is a 309 × 400 pixel thumbnail.png. Use a square canvas below when you need an exact 400 × 400 result.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetOutputFormat("png") client.SetResize("400x400") err := client.ConvertFileToFile("scan.png", "thumbnail.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Create a square thumbnail
Fit the whole image onto a 400 × 400 pixel canvas with either a pale blue background or transparent padding. Set 96 DPI so the px canvas dimensions match output pixels. The transparent variant preserves the original image's white background; transparency applies to the added canvas area.
Solid padding
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetCanvasBackgroundColor("EEF2F6") client.SetCanvasHeight("400px") client.SetCanvasWidth("400px") client.SetDpi(96) client.SetOutputFormat("png") client.SetPrintCanvasMode("fit") err := client.ConvertFileToFile("scan.png", "square.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Transparent padding
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetCanvasBackgroundColor("00000000") client.SetCanvasHeight("400px") client.SetCanvasWidth("400px") client.SetDpi(96) client.SetOutputFormat("png") client.SetPrintCanvasMode("fit") err := client.ConvertFileToFile("scan.png", "square-transparent.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Crop or trim an image
Extract the chart from scan.png into chart.png, or remove the uniform outer border and save trimmed.png.
The chart starts 96 pixels from the left and 370 pixels from the top, and measures 1024 × 384 pixels. Set 96 DPI so the px crop coordinates match source image pixels. Adjust the region for your image. Automatic border removal works on solid-color borders; use an explicit crop for uneven scan edges.
Extract a selected region
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetCropAreaHeight("384px") client.SetCropAreaWidth("1024px") client.SetCropAreaX("96px") client.SetCropAreaY("370px") client.SetDpi(96) client.SetOutputFormat("png") err := client.ConvertFileToFile("scan.png", "chart.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Remove a uniform border
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetOutputFormat("png") client.SetRemoveBorders(true) err := client.ConvertFileToFile("scan.png", "trimmed.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Correct image rotation
Rotate scan-sideways.png 90 degrees clockwise to restore the upright orientation, then save upright.png.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetOutputFormat("png") client.SetRotate("90") err := client.ConvertFileToFile("scan-sideways.png", "upright.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Replace transparency when creating a JPEG
Convert transparent-logo.png to logo-background.jpg with a pale blue background. JPEG cannot retain transparency, so choose the background color explicitly.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetCanvasBackgroundColor("EEF2F6") client.SetOutputFormat("jpg") err := client.ConvertFileToFile( "transparent-logo.png", "logo-background.jpg", ) checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Convert an image to WebP
Convert scan.png to scan.webp at its original pixel dimensions. WebP uses lossy compression here, which can reduce file size at the cost of fine detail. Compare the result with your source image before choosing it for web delivery.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewImageToImageClient("demo", "demo") client.SetOutputFormat("webp") err := client.ConvertFileToFile("scan.png", "scan.webp") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }