本文整理汇总了Golang中github.com/disintegration/imaging.Resize函数的典型用法代码示例。如果您正苦于以下问题:Golang Resize函数的具体用法?Golang Resize怎么用?Golang Resize使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Resize函数的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: transformImage
// transformImage modifies the image m based on the transformations specified
// in opt.
func transformImage(m image.Image, opt Options) image.Image {
// resize if needed
if w, h, resize := resizeParams(m, opt); resize {
if opt.Fit {
m = imaging.Fit(m, w, h, resampleFilter)
} else {
if w == 0 || h == 0 {
m = imaging.Resize(m, w, h, resampleFilter)
} else {
m = imaging.Resize(m, w, h, imaging.Lanczos)
}
}
}
// flip
if opt.FlipVertical {
m = imaging.FlipV(m)
}
if opt.FlipHorizontal {
m = imaging.FlipH(m)
}
// rotate
switch opt.Rotate {
case 90:
m = imaging.Rotate90(m)
case 180:
m = imaging.Rotate180(m)
case 270:
m = imaging.Rotate270(m)
}
return m
}
示例3: generateThumbnailImage
func generateThumbnailImage(img image.Image, thumbnailPath string, width int, height int) {
thumbWidth := float64(utils.Cfg.FileSettings.ThumbnailWidth)
thumbHeight := float64(utils.Cfg.FileSettings.ThumbnailHeight)
imgWidth := float64(width)
imgHeight := float64(height)
var thumbnail image.Image
if imgHeight < thumbHeight && imgWidth < thumbWidth {
thumbnail = img
} else if imgHeight/imgWidth < thumbHeight/thumbWidth {
thumbnail = imaging.Resize(img, 0, utils.Cfg.FileSettings.ThumbnailHeight, imaging.Lanczos)
} else {
thumbnail = imaging.Resize(img, utils.Cfg.FileSettings.ThumbnailWidth, 0, imaging.Lanczos)
}
buf := new(bytes.Buffer)
if err := jpeg.Encode(buf, thumbnail, &jpeg.Options{Quality: 90}); err != nil {
l4g.Error(utils.T("api.file.handle_images_forget.encode_jpeg.error"), thumbnailPath, err)
return
}
if err := WriteFile(buf.Bytes(), thumbnailPath); err != nil {
l4g.Error(utils.T("api.file.handle_images_forget.upload_thumb.error"), thumbnailPath, err)
return
}
}
示例4: ExtractThumbnail
// Extracts thumbnail
func (c *Convertor) ExtractThumbnail(file string, info os.FileInfo) {
c.CurrFile += 1
cover, err := c.GetCoverImage(file, info)
if err != nil {
fmt.Fprintf(os.Stderr, "Error GetCoverImage: %v\n", err.Error())
return
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error Thumbnail: %v\n", err.Error())
return
}
if c.Opts.Width > 0 || c.Opts.Height > 0 {
if c.Opts.Fit {
cover = imaging.Fit(cover, c.Opts.Width, c.Opts.Height, filters[c.Opts.Filter])
} else {
cover = imaging.Resize(cover, c.Opts.Width, c.Opts.Height, filters[c.Opts.Filter])
}
} else {
cover = imaging.Resize(cover, 256, 0, filters[c.Opts.Filter])
}
imagick.Initialize()
mw := imagick.NewMagickWand()
defer mw.Destroy()
b := new(bytes.Buffer)
png.Encode(b, cover)
err = mw.ReadImageBlob(b.Bytes())
if err != nil {
fmt.Fprintf(os.Stderr, "Error ReadImageBlob: %v\n", err.Error())
}
var fileuri string
var filename string
if c.Opts.Outfile == "" {
fileuri = "file://" + file
filename = filepath.Join(c.Opts.Outdir, fmt.Sprintf("%x.png", md5.Sum([]byte(fileuri))))
} else {
abs, _ := filepath.Abs(c.Opts.Outfile)
fileuri = "file://" + abs
filename = abs
}
mw.SetImageFormat("PNG")
mw.SetImageProperty("Software", "CBconvert")
mw.SetImageProperty("Description", "Thumbnail of "+fileuri)
mw.SetImageProperty("Thumb::URI", fileuri)
mw.SetImageProperty("Thumb::MTime", strconv.FormatInt(info.ModTime().Unix(), 10))
mw.SetImageProperty("Thumb::Size", strconv.FormatInt(info.Size(), 10))
mw.SetImageProperty("Thumb::Mimetype", mime.TypeByExtension(filepath.Ext(file)))
mw.WriteImage(filename)
}
示例5: ExtractCover
// Extracts cover
func (c *Convertor) ExtractCover(file string, info os.FileInfo) {
c.CurrFile += 1
cover, err := c.GetCoverImage(file, info)
if err != nil {
fmt.Fprintf(os.Stderr, "Error GetCoverImage: %v\n", err.Error())
return
}
if c.Opts.Width > 0 || c.Opts.Height > 0 {
if c.Opts.Fit {
cover = imaging.Fit(cover, c.Opts.Width, c.Opts.Height, filters[c.Opts.Filter])
} else {
cover = imaging.Resize(cover, c.Opts.Width, c.Opts.Height, filters[c.Opts.Filter])
}
}
filename := filepath.Join(c.Opts.Outdir, fmt.Sprintf("%s.jpg", c.getBasename(file)))
f, err := os.Create(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Error Create: %v\n", err.Error())
return
}
defer f.Close()
jpeg.Encode(f, cover, &jpeg.Options{c.Opts.Quality})
}
示例6: 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})
}
示例7: decodeFunc
func decodeFunc(h []string) func(rec []string) (draw.Image, error) {
if h[0] == "ImageId" {
return decodePGM
}
if h[0] == "left_eye_center_x" {
return func(rec []string) (draw.Image, error) {
src, err := decodePGM(rec)
if err != nil {
return nil, err
}
b := src.Bounds()
img := image.NewRGBA(b)
draw.Draw(img, b, src, b.Min, draw.Src)
for i := 0; i < len(rec)/2; i++ {
/* x, err := strconv.ParseFloat(rec[2*i], 64)
if err != nil {
log.Print(err)
continue
}
y, err := strconv.ParseFloat(rec[2*i + 1], 64)
if err != nil {
log.Print(err)
continue
}
img.Set(int(x + 0.5), int(y + 0.5), color.RGBA{0xff, 0, 0, 0xff})*/
}
// overlayPoints(
i := imaging.Resize(img, 500, 0, imaging.Lanczos)
return i, nil
}
}
return nil
}
示例8: Resized
func Resized(ext string, data []byte, width, height int) (resized []byte, w int, h int) {
if width == 0 && height == 0 {
return data, 0, 0
}
srcImage, _, err := image.Decode(bytes.NewReader(data))
if err == nil {
bounds := srcImage.Bounds()
var dstImage *image.NRGBA
if bounds.Dx() > width && width != 0 || bounds.Dy() > height && height != 0 {
if width == height && bounds.Dx() != bounds.Dy() {
dstImage = imaging.Thumbnail(srcImage, width, height, imaging.Lanczos)
w, h = width, height
} else {
dstImage = imaging.Resize(srcImage, width, height, imaging.Lanczos)
}
} else {
return data, bounds.Dx(), bounds.Dy()
}
var buf bytes.Buffer
switch ext {
case ".png":
png.Encode(&buf, dstImage)
case ".jpg", ".jpeg":
jpeg.Encode(&buf, dstImage, nil)
case ".gif":
gif.Encode(&buf, dstImage, nil)
}
return buf.Bytes(), dstImage.Bounds().Dx(), dstImage.Bounds().Dy()
} else {
glog.Error(err)
}
return data, 0, 0
}
示例9: Resize
func (api *Api) Resize(imgloc string, xsize, ysize int, resizeType string) bool {
dest := setSize(imgloc, xsize, ysize)
if _, err := os.Stat(dest); err == nil {
return true
}
bts, err := ioutil.ReadFile(imgloc)
if err != nil {
fmt.Println(err)
return false
}
rdr := bytes.NewReader(bts)
i, _, err := image.Decode(rdr)
if err != nil {
fmt.Println(err)
return false
}
var fsimg *image.NRGBA
switch resizeType {
case "fit":
fsimg = imaging.Fit(i, xsize, ysize, imaging.Lanczos)
case "thumb":
fsimg = imaging.Thumbnail(i, xsize, ysize, imaging.Lanczos)
default:
fsimg = imaging.Resize(i, xsize, ysize, imaging.Lanczos)
}
out, err := os.Create(dest)
if err != nil {
return false
}
defer out.Close()
jpeg.Encode(out, fsimg, nil)
return true
}
示例10: CenterCrop
func CenterCrop(img *io.Reader, width, height int) ([]byte, error) {
m, _, err := image.Decode(*img)
if err != nil {
return nil, err
}
// TODO: I probably can switch to using imaging.Thumbnail here.
imgW := float64(m.Bounds().Max.X - m.Bounds().Min.X)
imgH := float64(m.Bounds().Max.Y - m.Bounds().Min.Y)
targetW := float64(width)
targetH := float64(height)
var scale float64
if imgW*targetH > targetW*imgH {
scale = targetH / imgH
} else {
scale = targetW / imgW
}
m = imaging.Resize(m, int(imgW*scale), int(imgH*scale), imaging.Lanczos)
m = imaging.CropCenter(m, width, height)
buf := new(bytes.Buffer)
jpeg.Encode(buf, m, &jpeg.Options{Quality: 95})
return buf.Bytes(), nil
}
示例11: resizePreview
func (image *Image) resizePreview(errorChan chan error, srcImage image.Image) {
size := srcImage.Bounds().Size()
ratio := float64(size.Y) / float64(size.X)
targetHeight := int(float64(widthPreview) * ratio)
dstImage := imaging.Resize(srcImage, widthPreview, targetHeight, imaging.Lanczos)
dest := "./data/images/preview/" + image.Location
errorChan <- imaging.Save(dstImage, dest)
}
示例12: resizeImage
func resizeImage(filename string, size int) string {
resizedFile := filename + ".resized.png"
img, err := imaging.Open(filename)
if err != nil {
panic(err)
}
dstimg := imaging.Resize(img, size, 0, imaging.Box)
imaging.Save(dstimg, resizedFile)
return resizedFile
}
示例13: resizeOrCrop
/* resize when there is no height constraint, otherwise crop */
func (result *imageInfo) resizeOrCrop(img image.Image, width int, height int) image.Image {
var imgdata image.Image
if height == 0 {
imgdata = imaging.Resize(img, width, height, imaging.Lanczos)
} else {
imgdata = imaging.CropCenter(img, width, height)
}
result.width = imgdata.Bounds().Max.X
result.height = imgdata.Bounds().Max.Y
return imgdata
}
示例14: GenerateThumbnail
func (be *Backend) GenerateThumbnail(bin string, filename string, width int, height int, crop bool) error {
f, err := be.GetFileMetaData(bin, filename)
if err != nil {
return err
}
if strings.Split(f.MIME, "/")[0] != "image" {
return errors.New("Batch job skipped: " + filename + " is not an image")
}
fpath := filepath.Join(be.filedir, bin, filename)
cachedir := filepath.Join(be.filedir, bin, ".cache")
if !isDir(cachedir) {
if err := os.Mkdir(cachedir, 0700); err != nil {
return err
}
}
dst := filepath.Join(cachedir, strconv.Itoa(width)+"x"+strconv.Itoa(height)+"-"+filename)
// Optimize to skip thumbnail generation if the thumbnail file exists
// and is newer than the file.
fi, err := os.Lstat(dst)
if err == nil {
if f.CreatedAt.After(fi.ModTime()) {
// File newer than thumbnail. Ok to generate.
} else {
// File older than thumbnail. No need to generate.
return nil
}
}
s, err := imaging.Open(fpath)
if err != nil {
return err
}
if crop {
im := imaging.Fill(s, width, height, imaging.Center, imaging.Lanczos)
err = imaging.Save(im, dst)
} else {
im := imaging.Resize(s, width, height, imaging.Lanczos)
err = imaging.Save(im, dst)
}
f.Links = be.GenerateLinks(f.Bin, f.Filename)
be.Lock()
defer be.Unlock()
id := bin + filename
delete(be.files, id)
be.files[id] = f
return err
}
示例15: normalizeInput
func normalizeInput(input image.Image, maxSize int) (image.Image, float64, error) {
var scale float64
if input.Bounds().Dx() > maxSize {
scale = float64(input.Bounds().Dx()) / float64(maxSize)
} else {
scale = float64(input.Bounds().Dy()) / float64(maxSize)
}
log.Printf("Normalizing to %dx%d\n", int(float64(input.Bounds().Dx())/scale), int(float64(input.Bounds().Dy())/scale))
resized := imaging.Resize(input, int(float64(input.Bounds().Dx())/scale), int(float64(input.Bounds().Dy())/scale), imaging.Lanczos)
return resized, scale, nil
}