git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/imaging/example_test.go (about) 1 package imaging_test 2 3 import ( 4 "image" 5 "image/color" 6 "log" 7 8 "git.sr.ht/~pingoo/stdx/imaging" 9 ) 10 11 func Example() { 12 // Open a test image. 13 src, err := imaging.Open("testdata/flowers.png") 14 if err != nil { 15 log.Fatalf("failed to open image: %v", err) 16 } 17 18 // Crop the original image to 300x300px size using the center anchor. 19 src = imaging.CropAnchor(src, 300, 300, imaging.Center) 20 21 // Resize the cropped image to width = 200px preserving the aspect ratio. 22 src = imaging.Resize(src, 200, 0, imaging.Lanczos) 23 24 // Create a blurred version of the image. 25 img1 := imaging.Blur(src, 5) 26 27 // Create a grayscale version of the image with higher contrast and sharpness. 28 img2 := imaging.Grayscale(src) 29 img2 = imaging.AdjustContrast(img2, 20) 30 img2 = imaging.Sharpen(img2, 2) 31 32 // Create an inverted version of the image. 33 img3 := imaging.Invert(src) 34 35 // Create an embossed version of the image using a convolution filter. 36 img4 := imaging.Convolve3x3( 37 src, 38 [9]float64{ 39 -1, -1, 0, 40 -1, 1, 1, 41 0, 1, 1, 42 }, 43 nil, 44 ) 45 46 // Create a new image and paste the four produced images into it. 47 dst := imaging.New(400, 400, color.NRGBA{0, 0, 0, 0}) 48 dst = imaging.Paste(dst, img1, image.Pt(0, 0)) 49 dst = imaging.Paste(dst, img2, image.Pt(0, 200)) 50 dst = imaging.Paste(dst, img3, image.Pt(200, 0)) 51 dst = imaging.Paste(dst, img4, image.Pt(200, 200)) 52 53 // Save the resulting image as JPEG. 54 err = imaging.Save(dst, "testdata/out_example.jpg") 55 if err != nil { 56 log.Fatalf("failed to save image: %v", err) 57 } 58 }