本文整理汇总了Golang中github.com/disintegration/gift.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: varianceF
func varianceF(img image.Image, disk int) (imageF, imageF) {
m := meanF(img, disk) // gets a grayscale copy of local mean
// create a grayscale version of the original
//g := gift.New( gift.Grayscale() )
//v := image.NewRGBA(g.Bounds(img.Bounds()))
//g.Draw(v, img)
g := gift.New(gift.Grayscale())
dst := image.NewRGBA(g.Bounds(img.Bounds()))
g.Draw(dst, img)
bounds := img.Bounds()
floatData := make([][]float32, bounds.Max.Y-bounds.Min.Y)
for i := range floatData {
floatData[i] = make([]float32, bounds.Max.X-bounds.Min.X)
}
for y := bounds.Min.X; y < bounds.Max.X; y++ {
for x := bounds.Min.Y; x < bounds.Max.Y; x++ {
p1r, p1g, p1b, _ := dst.At(x, y).RGBA()
g1 := 0.2125*float64(p1r) + 0.7154*float64(p1g) + 0.0721*float64(p1b)
g2 := float64(m[x][y])
floatData[x][y] = float32((g1 - g2) * (g1 - g2))
}
}
return m, floatData
}
示例2: makeThumb
func makeThumb(source, destination string) error { // {{{
file, err := os.Open(source)
if err != nil {
return err
}
defer file.Close()
// decode jpeg into image.Image
img, err := jpeg.Decode(file)
if err != nil {
return err
}
g := gift.New(
gift.ResizeToFill(200, 200, gift.LanczosResampling, gift.CenterAnchor),
)
out, err := os.Create(destination)
if err != nil {
return err
}
defer out.Close()
// write new image to file
dst := image.NewRGBA(g.Bounds(img.Bounds()))
g.Draw(dst, img)
return jpeg.Encode(out, dst, &jpeg.Options{100})
} // }}}
示例3: Render
// Render draws rune r front the specified font at the specified dpi and scale. It returns a
// grayscale image that is just large enough to contain the rune.
func Render(font *truetype.Font, r rune, dpi, scale float64) (*image.Gray, error) {
glyph := truetype.NewGlyphBuf()
index := font.Index(r)
glyph.Load(font, font.FUnitsPerEm(), index, truetype.FullHinting)
ctx := freetype.NewContext()
boxer := makeBoundingBoxer()
ctx.SetSrc(image.NewUniform(color.White))
ctx.SetDst(boxer)
ctx.SetClip(boxer.largeBounds)
ctx.SetFontSize(250)
ctx.SetDPI(dpi)
ctx.SetFont(font)
if err := glyph.Load(font, font.FUnitsPerEm(), font.Index(r), truetype.FullHinting); err != nil {
return nil, fmt.Errorf("Unable to load glyph: %v\n", err)
}
var rp raster.Point
rp.X = ctx.PointToFix32(0)
rp.Y = ctx.PointToFix32(100)
ctx.DrawString(string(r), rp)
boxer.complete()
g := gift.New(
gift.Resize(int(float64(boxer.Bounds().Dx())*scale+0.5), int(float64(boxer.Bounds().Dy())*scale+0.5), gift.CubicResampling),
)
dst := image.NewGray(g.Bounds(boxer.Bounds()))
g.Draw(dst, boxer)
return dst, nil
}
示例4: resizeImage
// TODO Too min images should not resized
func resizeImage(src image.Image, n int) image.Image {
srcBounds := src.Bounds()
gi := gift.New(gift.Resize(srcBounds.Max.Y/n, srcBounds.Max.Y/n, gift.LanczosResampling))
dst := image.NewRGBA(gi.Bounds(srcBounds))
gi.Draw(dst, src)
return dst
}
示例5: blur
func blur(img image.Image, howmuch float32) image.Image {
g := gift.New(gift.Grayscale())
g.Add(gift.GaussianBlur(howmuch))
dst := image.NewRGBA(g.Bounds(img.Bounds()))
g.Draw(dst, img)
return (dst)
}
示例6: createThumb
func (fi *FileInfo) createThumb(buffer *bytes.Buffer, c context.Context) {
if imageTypes.MatchString(fi.Type) {
src, _, err := image.Decode(bytes.NewReader(buffer.Bytes()))
check(err)
filter := gift.New(gift.ResizeToFit(
THUMB_MAX_WIDTH,
THUMB_MAX_HEIGHT,
gift.LanczosResampling,
))
dst := image.NewNRGBA(filter.Bounds(src.Bounds()))
filter.Draw(dst, src)
buffer.Reset()
bWriter := bufio.NewWriter(buffer)
switch fi.Type {
case "image/jpeg", "image/pjpeg":
err = jpeg.Encode(bWriter, dst, nil)
case "image/gif":
err = gif.Encode(bWriter, dst, nil)
default:
err = png.Encode(bWriter, dst)
}
check(err)
bWriter.Flush()
thumbnailKey := fi.Key + thumbSuffix + filepath.Ext(fi.Name)
item := &memcache.Item{
Key: thumbnailKey,
Value: buffer.Bytes(),
}
err = memcache.Set(c, item)
check(err)
fi.ThumbnailKey = thumbnailKey
}
}
示例7: mean
func mean(img image.Image, disk int) image.Image {
g := gift.New(gift.Grayscale())
g.Add(gift.Mean(disk, false)) // use square neighborhood
dst := image.NewRGBA(g.Bounds(img.Bounds()))
g.Draw(dst, img)
return (dst)
}
示例8: DrawPicture
// DrawPicture .
func DrawPicture(src image.Image, width int, height int) *image.Gray {
rect := image.Rect(0, 0, width, height)
img := image.NewGray(rect)
rander := rander()
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
var c color.Gray
if IsBlack(src, x, y) {
if y > 1 && IsNotBlack(src, x, y-1) {
c = borderColor(rander)
} else {
c = insideColor(rander)
}
} else {
c = outsideColor(rander)
}
x1, y1 := transformCurve(x, y, width, height)
//c = color.Gray{c.Y + ScaledLuminanceAt(90, src, x, y)}
img.Set(x1, y1, c)
}
}
g := gift.New(
//gift.GaussianBlur(2),
)
img2 := image.NewGray(rect)
g.Draw(img2, img)
return img2
}
示例9: analyzeFile
func analyzeFile(filename string, resize bool) (*Cover, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
if resize {
start := time.Now()
g := gift.New(gift.Resize(500, 0, gift.LanczosResampling))
dst := image.NewRGBA(g.Bounds(img.Bounds()))
g.Draw(dst, img)
img = dst
fmt.Printf("- RESIZE %s took %s\n", path.Base(filename), time.Since(start))
}
start := time.Now()
bg, c1, c2, c3 := colorart.Analyze(img)
fmt.Printf("- ANALYZE %s took %s\n", path.Base(filename), time.Since(start))
return &Cover{filename, bg.String(), c1.String(), c2.String(), c3.String()}, nil
}
示例10: procImage
func procImage(fileName string) (string, error) {
reader, err := os.Open(fileName)
if err != nil {
fmt.Errorf("Failed to open image with error: %s\n", err)
return "", err
}
defer reader.Close()
src, _, err := image.Decode(reader)
if err != nil {
fmt.Errorf("Failed to decode image with error: %s\n", err)
return "", err
}
g := gift.New(
gift.Invert(),
)
dst := image.NewRGBA(g.Bounds(src.Bounds()))
g.Draw(dst, src)
var opt jpeg.Options
opt.Quality = 100
fileName = fileName + "_processed.jpg"
out, _ := os.Create(fileName)
err = jpeg.Encode(out, dst, &opt)
if err != nil {
fmt.Errorf("Failed to encode image with error: %s\n", err)
return "", err
}
return fileName, nil
}
示例11: 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
}
示例12: main
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage:\tgoimger <file>")
os.Exit(1)
}
srcFileName := os.Args[1]
srcFile, _ := os.Open(srcFileName)
src, _, _ := image.Decode(srcFile)
// let's make a new gift
g := gift.New(
gift.Grayscale(),
gift.UnsharpMask(1.0, 0.5, 0.0),
)
// dest - output image
dest := image.NewRGBA(g.Bounds(src.Bounds()))
// draw result
g.Draw(dest, src)
outFileName := srcFileName + "_goimger.jpg"
toimg, _ := os.Create(outFileName)
defer toimg.Close()
jpeg.Encode(toimg, dest, &jpeg.Options{jpeg.DefaultQuality})
}
示例13: main
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage:\tspiffy <file>")
os.Exit(1)
}
srcFileName := os.Args[1]
srcFile, _ := os.Open(srcFileName)
src, _, _ := image.Decode(srcFile)
// 1. Create a new GIFT and add some filters:
g := gift.New(
gift.Grayscale(),
gift.UnsharpMask(1.0, 1.0, 0.0),
)
// 2. Create a new image of the corresponding size.
// dst is a new target image, src is the original image
dst := image.NewRGBA(g.Bounds(src.Bounds()))
// 3. Use Draw func to apply the filters to src and store the result in dst:
g.Draw(dst, src)
outFileName := srcFileName + ".spiffy.jpg"
toimg, _ := os.Create(outFileName)
defer toimg.Close()
jpeg.Encode(toimg, dst, &jpeg.Options{jpeg.DefaultQuality})
}
示例14: CropImage
func CropImage(img image.Image, rect image.Rectangle) (croped image.Image) {
g := gift.New(
gift.Crop(rect),
)
croped_image := image.NewRGBA(g.Bounds(rect))
g.Draw(croped_image, img)
return croped_image
}
示例15: newResizeFuncGift
func newResizeFuncGift(resampling gift.Resampling) resizeFunc {
return func(im image.Image) image.Image {
g := gift.New(gift.Resize(width, height, resampling))
newIm := newImageFunc(g.Bounds(im.Bounds()))
g.Draw(newIm, im)
return newIm
}
}