本文整理汇总了Golang中github.com/disintegration/imaging.Crop函数的典型用法代码示例。如果您正苦于以下问题:Golang Crop函数的具体用法?Golang Crop怎么用?Golang Crop使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Crop函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetCube
// Sets skin.Processed to an isometric render of the head from a top-left angle (showing 3 sides).
func (skin *mcSkin) GetCube(width int) error {
// Crop out the top of the head
topFlat := imaging.Crop(skin.Image, image.Rect(8, 0, 16, 8))
// Resize appropriately, so that it fills the `width` when rotated 45 def.
topFlat = imaging.Resize(topFlat, int(float64(width)*math.Sqrt(2)/3+1), 0, imaging.NearestNeighbor)
// Create the Gift filter
filter := gift.New(
gift.Rotate(45, color.Transparent, gift.LinearInterpolation),
)
bounds := filter.Bounds(topFlat.Bounds())
top := image.NewNRGBA(bounds)
// Draw it on the filter, then smush it!
filter.Draw(top, topFlat)
top = imaging.Resize(top, width+2, width/3, imaging.NearestNeighbor)
// Skew the front and sides at 15 degree angles to match up with the
// head that has been smushed
front := skin.cropHead(skin.Image).(*image.NRGBA)
side := imaging.Crop(skin.Image, image.Rect(0, 8, 8, 16))
front = imaging.Resize(front, width/2, int(float64(width)/1.75), imaging.NearestNeighbor)
side = imaging.Resize(side, width/2, int(float64(width)/1.75), imaging.NearestNeighbor)
front = skewVertical(front, math.Pi/12)
side = skewVertical(imaging.FlipH(side), math.Pi/-12)
// Create a new image to assemble upon
skin.Processed = image.NewNRGBA(image.Rect(0, 0, width, width))
// Draw each side
draw.Draw(skin.Processed.(draw.Image), image.Rect(0, width/6, width/2, width), side, image.Pt(0, 0), draw.Src)
draw.Draw(skin.Processed.(draw.Image), image.Rect(width/2, width/6, width, width), front, image.Pt(0, 0), draw.Src)
// Draw the top we created
draw.Draw(skin.Processed.(draw.Image), image.Rect(-1, 0, width+1, width/3), top, image.Pt(0, 0), draw.Over)
return nil
}
示例2: ProcessImage
func (ipo *ImageProcessorOptions) ProcessImage(source *Image) (*Image, error) {
dest := &Image{format: source.format}
var wip image.Image
if ipo.ScaleX == -1 {
wip = imaging.FlipH(source.image)
} else {
wip = source.image
}
if ipo.X != 0 || ipo.Y != 0 || ipo.Width != source.Width() || ipo.Height != source.Height() {
r := image.Rectangle{image.Point{int(ipo.X), int(ipo.Y)}, image.Point{int(ipo.X + ipo.Width), int(ipo.Y + ipo.Height)}}
wip = imaging.Crop(wip, r)
}
var err error
switch dest.format {
case JPEG:
err = jpeg.Encode(&dest.buffer, wip, &jpeg.Options{int(ipo.Quality)})
case PNG:
err = png.Encode(&dest.buffer, wip)
default:
return nil, errors.New("attempt to encode to unsupported image format")
}
return dest, err
}
示例3: renderLowerBody
// Returns the legs.
func (skin *mcSkin) renderLowerBody() *image.NRGBA {
// This will be the base.
lowerBodyImg := image.NewNRGBA(image.Rect(0, 0, LlWidth+RlWidth, LlHeight))
rlImg := imaging.Crop(skin.Image, image.Rect(RlX, RlY, RlX+RlWidth, RlY+RlHeight))
// If it's an old skin, they don't have a Left Leg, so we'll just flip their right.
var llImg image.Image
if skin.is18Skin() {
llImg = imaging.Crop(skin.Image, image.Rect(LlX, LlY, LlX+LlWidth, LlY+LlHeight))
} else {
llImg = imaging.FlipH(rlImg)
}
return skin.drawLower(lowerBodyImg, rlImg, llImg.(*image.NRGBA))
}
示例4: Extract
func Extract(img image.Image, name string, X, Y, W, H float32, rotated bool) error {
dst := imaging.Crop(img, image.Rect(int(X), int(Y), int(X+W), int(Y+H)))
if rotated {
log.Fatal("图片被旋转过")
}
return imgutil.SaveImg(name, dst)
}
示例5: ResizeHandler
func ResizeHandler(w http.ResponseWriter, r *http.Request) {
// parse url vars
v := mux.Vars(r)
width, _ := v["width"]
height, _ := v["height"]
x, _ := strconv.Atoi(width)
y, _ := strconv.Atoi(height)
imageUrl, _ := v["imageUrl"]
m := getImg(imageUrl)
cropBox, _ := getFillCrop(m, float32(x)/float32(y))
cropRect := image.Rect(cropBox.X, cropBox.Y, cropBox.X+cropBox.Width, cropBox.Y+cropBox.Height)
croppedImg := imaging.Crop(m, cropRect)
// convert to opencv image
//srcImage := opencv.FromImage(m)
//if srcImage == nil {
// fmt.Printf("Couldn't create opencv Image")
//}
//defer srcImage.Release()
//croppedImage := opencv.Crop(srcImage, cropBox.X, cropBox.Y, cropBox.Width, cropBox.Height)
//resizedImage := opencv.Resize(croppedImage, x, y, opencv.CV_INTER_LINEAR)
resizedImage := imaging.Resize(croppedImg, x, y, imaging.CatmullRom)
jpeg.Encode(w, resizedImage, &jpeg.Options{Quality: 90})
}
示例6: cropHelm
// Returns the head of the skin image overlayed with the helm.
func (skin *mcSkin) cropHelm(img image.Image) image.Image {
headImg := skin.cropHead(img)
helmImg := imaging.Crop(img, image.Rect(HelmX, HelmY, HelmX+HeadWidth, HelmY+HeadHeight))
skin.removeAlpha(helmImg)
fastDraw(headImg.(*image.NRGBA), helmImg, 0, 0)
return headImg
}
示例7: renderLowerArmor
// Returns the legs but with any armor which the user has.
func (skin *mcSkin) renderLowerArmor() *image.NRGBA {
// This will be the base.
lowerArmorBodyImg := skin.renderLowerBody()
// If it's an old skin, they don't have armor here.
if skin.is18Skin() {
// Get the armor layers from the skin and remove the Alpha.
ll2Img := imaging.Crop(skin.Image, image.Rect(Ll2X, Ll2Y, Ll2X+LlWidth, Ll2Y+LlHeight))
skin.removeAlpha(ll2Img)
rl2Img := imaging.Crop(skin.Image, image.Rect(Rl2X, Rl2Y, Rl2X+RlWidth, Rl2Y+RlHeight))
skin.removeAlpha(rl2Img)
return skin.drawLower(lowerArmorBodyImg, ll2Img, rl2Img)
}
return lowerArmorBodyImg
}
示例8: renderUpperBody
// Returns the torso and arms.
func (skin *mcSkin) renderUpperBody() *image.NRGBA {
// This will be the base.
upperBodyImg := image.NewNRGBA(image.Rect(0, 0, LaWidth+TorsoWidth+RaWidth, TorsoHeight))
torsoImg := imaging.Crop(skin.Image, image.Rect(TorsoX, TorsoY, TorsoX+TorsoWidth, TorsoY+TorsoHeight))
raImg := imaging.Crop(skin.Image, image.Rect(RaX, RaY, RaX+RaWidth, RaY+TorsoHeight))
// If it's an old skin, they don't have a Left Arm, so we'll just flip their right.
var laImg image.Image
if skin.is18Skin() {
laImg = imaging.Crop(skin.Image, image.Rect(LaX, LaY, LaX+LaWidth, LaY+TorsoHeight))
} else {
laImg = imaging.FlipH(raImg)
}
return skin.drawUpper(upperBodyImg, torsoImg, raImg, laImg.(*image.NRGBA))
}
示例9: resizeThumbnail
func resizeThumbnail(from *bytes.Buffer, spec *ThumbnailSpec) (to io.Reader, w int, h int, err error) {
src, name, err := image.Decode(from)
if err != nil {
return
}
srcB := src.Bounds()
var dst image.Image
if spec.CropToSquare && srcB.Dx() != srcB.Dy() {
var rect image.Rectangle
if srcB.Dx() > srcB.Dy() {
x1 := (srcB.Dx() - srcB.Dy()) / 2
x2 := srcB.Dx() - x1
rect = image.Rect(x1, 0, x2, srcB.Dy())
} else {
rect = image.Rect(0, 0, srcB.Dx(), srcB.Dx())
}
w = spec.Height
if (spec.Height > spec.Width && spec.Width != 0) || spec.Height == 0 {
w = spec.Width
}
h = w
cropedImg := imaging.Crop(src, rect)
rect = cropedImg.Bounds()
dst = resize.Resize(cropedImg, rect, w, h)
} else {
w, h = spec.CalculateRect(srcB, spec.CropToSquare)
if w >= srcB.Dx() || h >= srcB.Dy() {
w, h = srcB.Dx(), srcB.Dy()
}
rect := image.Rect(0, 0, srcB.Dx(), srcB.Dy())
dst = resize.Resize(src, rect, w, h)
}
var buf bytes.Buffer
switch name {
case "jpeg":
jpeg.Encode(&buf, dst, &jpeg.Options{95})
case "png":
png.Encode(&buf, dst)
case "gif":
jpeg.Encode(&buf, dst, &jpeg.Options{95})
case "bmp":
jpeg.Encode(&buf, dst, &jpeg.Options{95})
}
to = &buf
return
}
示例10: renderUpperArmor
// Returns the torso and arms but with any armor which the user has.
func (skin *mcSkin) renderUpperArmor() *image.NRGBA {
// This will be the base.
upperArmorBodyImg := skin.renderUpperBody()
// If it's an old skin, they don't have armor here.
if skin.is18Skin() {
// Get the armor layers from the skin and remove the Alpha.
torso2Img := imaging.Crop(skin.Image, image.Rect(Torso2X, Torso2Y, Torso2X+TorsoWidth, Torso2Y+TorsoHeight))
skin.removeAlpha(torso2Img)
la2Img := imaging.Crop(skin.Image, image.Rect(La2X, La2Y, La2X+LaWidth, La2Y+TorsoHeight))
skin.removeAlpha(la2Img)
ra2Img := imaging.Crop(skin.Image, image.Rect(Ra2X, Ra2Y, Ra2X+RaWidth, Ra2Y+TorsoHeight))
skin.removeAlpha(ra2Img)
return skin.drawUpper(upperArmorBodyImg, torso2Img, ra2Img, la2Img)
}
return upperArmorBodyImg
}
示例11: Handle
func (imageHandler) Handle(media MediaLibrary, file multipart.File, option *Option) error {
if err := media.Store(media.URL("original"), option, file); err == nil {
file.Seek(0, 0)
if img, err := imaging.Decode(file); err == nil {
if format, err := getImageFormat(media.URL()); err == nil {
if cropOption := media.GetCropOption("original"); cropOption != nil {
img = imaging.Crop(img, *cropOption)
}
// Save default image
var buffer bytes.Buffer
imaging.Encode(&buffer, img, *format)
media.Store(media.URL(), option, &buffer)
for key, size := range media.GetSizes() {
newImage := img
if cropOption := media.GetCropOption(key); cropOption != nil {
newImage = imaging.Crop(newImage, *cropOption)
}
dst := imaging.Thumbnail(newImage, size.Width, size.Height, imaging.Lanczos)
var buffer bytes.Buffer
imaging.Encode(&buffer, dst, *format)
media.Store(media.URL(key), option, &buffer)
}
return nil
} else {
return err
}
} else {
return err
}
} else {
return err
}
}
示例12: Create
func Create(c *gin.Context) {
file, fileHeader, err := c.Request.FormFile("file")
if err != nil {
c.Error(err)
return
}
_, h := parseVal("h", c)
_, w := parseVal("w", c)
_, x := parseVal("x", c)
_, y := parseVal("y", c)
img, _, err := image.Decode(file)
if err != nil {
c.Error(err)
return
}
log.Debug("Crop w:%v h:%v x:%v y:%v", w, h, x, y)
rect := convertToRectangle(int(w), int(h), int(x), int(y))
log.Debug("Rect", rect)
croppedImg := imaging.Crop(img, rect)
db := utils.GetDb(c)
gridFile, err := db.GridFS("fs").Create(fileHeader.Filename)
if err != nil {
c.Error(err)
return
}
buffer := new(bytes.Buffer)
jpeg.Encode(buffer, croppedImg, nil)
gridFile.SetName(fileHeader.Filename)
gridFile.SetContentType(fileHeader.Header.Get("Content-Type"))
_, err = io.Copy(gridFile, buffer)
if err != nil {
c.Error(err)
return
}
file.Close()
gridFile.Close()
c.JSON(http.StatusOK, gin.H{"_id": gridFile.Id()})
}
示例13: fitCropScale
func fitCropScale(i image.Image, r image.Rectangle) image.Image {
wantRatio := float64(r.Bounds().Dx()) / float64(r.Bounds().Dy())
haveRatio := float64(i.Bounds().Dx()) / float64(i.Bounds().Dy())
sliceRect := image.Rectangle{}
if haveRatio > wantRatio {
wantwidth := wantRatio * float64(i.Bounds().Dy())
sliceRect = image.Rect(i.Bounds().Dx()/2-int(wantwidth/2), 0, i.Bounds().Dx()/2+int(wantwidth/2), i.Bounds().Dy())
} else {
wantheight := float64(i.Bounds().Dx()) / wantRatio
sliceRect = image.Rect(0, i.Bounds().Dy()/2-int(wantheight/2), i.Bounds().Dx(), i.Bounds().Dy()/2+int(wantheight/2))
}
return imaging.Resize(imaging.Crop(i, sliceRect), r.Dx(), r.Dy(), imaging.Lanczos)
}
示例14: cut
func (i *ImgNet) cut(p image.Point, img *image.Image) []float64 {
rect := image.Rect(p.X, p.Y, p.X+i.w, p.Y+i.h)
croppedImg := imaging.Crop(*img, rect)
rgb := make([]float64, i.w*i.h*3)
for y := 0; y < croppedImg.Bounds().Dy(); y++ {
for x := 0; x < croppedImg.Bounds().Dx(); x++ {
r, g, b, _ := croppedImg.At(x, y).RGBA()
index := x + y*i.w*3
rgb[index] = float64(r) / 65535
rgb[index+1] = float64(g) / 65535
rgb[index+2] = float64(b) / 65535
}
}
return rgb
}
示例15: NewCubemap
func NewCubemap(name string, baseImage image.Image, lod bool) *CubeMap {
cubeMap := new(CubeMap)
x := baseImage.Bounds().Max.X
y := baseImage.Bounds().Max.Y
cubeMap.Name = name
cubeMap.Lod = lod
cubeMap.Right = imaging.Crop(baseImage, image.Rect(x/2, y/3, 3*x/4, 2*y/3))
cubeMap.Left = imaging.Crop(baseImage, image.Rect(0, y/3, x/4, 2*y/3))
cubeMap.Top = imaging.Crop(baseImage, image.Rect(x/4, 0, x/2, y/3))
cubeMap.Bottom = imaging.Crop(baseImage, image.Rect(x/4, 2*y/3, x/2, y))
cubeMap.Back = imaging.Crop(baseImage, image.Rect(3*x/4, y/3, x, 2*y/3))
cubeMap.Front = imaging.Crop(baseImage, image.Rect(x/4, y/3, x/2, 2*y/3))
return cubeMap
}