Golang : Generate thumbnails from images
After learning how to resize images and crop images with Golang. We will learn how to generate thumbnails from images with this excellent image package https://github.com/disintegration/imaging.
The codes below are taken from the https://github.com/disintegration/imaging itself without modification. Please visit https://github.com/disintegration/imaging to know more how to use the imaging package.
package main
import (
"image"
"image/color"
"runtime"
"github.com/disintegration/imaging"
)
func main() {
// use all CPU cores for maximum performance
runtime.GOMAXPROCS(runtime.NumCPU())
// input files
files := []string{"01.jpg", "02.jpg", "03.jpg"}
// load images and make 100x100 thumbnails of them
var thumbnails []image.Image
for _, file := range files {
img, err := imaging.Open(file)
if err != nil {
panic(err)
}
thumb := imaging.Thumbnail(img, 100, 100, imaging.CatmullRom)
thumbnails = append(thumbnails, thumb)
}
// create a new blank image
dst := imaging.New(100*len(thumbnails), 100, color.NRGBA{0, 0, 0, 0})
// paste thumbnails into the new image side by side
for i, thumb := range thumbnails {
dst = imaging.Paste(dst, thumb, image.Pt(i*100, 0))
}
// save the combined image to file
err := imaging.Save(dst, "dst.jpg")
if err != nil {
panic(err)
}
}
NOTE : The input images varies in sizes and the thumbnails generation function will handle the different sizes.
01.jpg
02.jpg
03.jpg
dst.jpg
See also : Golang : Resize Image
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.7k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+17.8k Golang : Parse date string and convert to dd-mm-yyyy format
+21.3k Golang : How to get time zone and load different time zone?
+5.8k Golang : Error handling methods
+5k Unix/Linux : secure copying between servers with SCP command examples
+12.3k Golang : Pagination with go-paginator configuration example
+8.2k Golang : Tell color name with OpenCV example
+10.8k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example
+10.6k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+20.9k Android Studio : AlertDialog and EditText to get user string input example
+31.3k Golang : Calculate percentage change of two values
+11.1k Golang : Create S3 bucket with official aws-sdk-go package