| 9 | ) |
| 10 | |
| 11 | func ImageCopy(src image.Image, x, y, w, h int) (image.Image, error) { |
| 12 | |
| 13 | var subImg image.Image |
| 14 | |
| 15 | if rgbImg, ok := src.(*image.YCbCr); ok { |
| 16 | subImg = rgbImg.SubImage(image.Rect(x, y, x+w, y+h)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1 |
| 17 | } else if rgbImg, ok := src.(*image.RGBA); ok { |
| 18 | subImg = rgbImg.SubImage(image.Rect(x, y, x+w, y+h)).(*image.RGBA) //图片裁剪x0 y0 x1 y1 |
| 19 | } else if rgbImg, ok := src.(*image.NRGBA); ok { |
| 20 | subImg = rgbImg.SubImage(image.Rect(x, y, x+w, y+h)).(*image.NRGBA) //图片裁剪x0 y0 x1 y1 |
| 21 | } else { |
| 22 | |
| 23 | return subImg, errors.New("图片解码失败") |
| 24 | } |
| 25 | |
| 26 | return subImg, nil |
| 27 | } |
| 28 | |
| 29 | func ImageCopyFromFile(p string, x, y, w, h int) (src image.Image, err error) { |
| 30 | file, err := os.Open(p) |