1
0

example_test.go 1.5 KB

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