本文整理匯總了Golang中image.Image函數的典型用法代碼示例。如果您正苦於以下問題:Golang Image函數的具體用法?Golang Image怎麽用?Golang Image使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Image函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: RevisitMsg
func (ri *RevisitImage) RevisitMsg() (*RevisitMsg, error) {
buf := bytes.NewBuffer(nil)
switch ri.ImgType {
case "image/jpeg":
err := jpeg.Encode(buf, image.Image(image.Image(&ri.Rgbas[0])), nil)
if err != nil {
return nil, err
}
case "image/png":
err := png.Encode(buf, image.Image(&ri.Rgbas[0]))
if err != nil {
return nil, err
}
case "image/gif":
g := &gif.GIF{
Image: make([]*image.Paletted, 0),
LoopCount: ri.LoopCount,
Delay: make([]int, 0),
}
for index, src := range ri.Rgbas {
b := src.Bounds()
pal := image.NewPaletted(image.Rect(0, 0, b.Dx(), b.Dy()), ri.Palette[index])
draw.Draw(pal, pal.Bounds(), image.Image(&src), b.Min, draw.Src)
g.Image = append(g.Image, pal)
g.Delay = append(g.Delay, ri.Delay[index])
}
buf := bytes.NewBuffer(nil)
err := gif.EncodeAll(buf, g)
if err != nil {
return nil, err
}
dstImgBase64 := base64.StdEncoding.EncodeToString(buf.Bytes())
return &RevisitMsg{
Content: ImageData{
Data: fmt.Sprintf("data:%s;base64,%s", ri.ImgType, dstImgBase64),
},
}, nil
default:
return nil, errors.New("invalid image type")
}
dstImgBase64 := base64.StdEncoding.EncodeToString(buf.Bytes())
return &RevisitMsg{
Content: ImageData{
Data: fmt.Sprintf("data:%s;base64,%s", ri.ImgType, dstImgBase64),
},
}, nil
}
示例2: ImageTransformByProfile
func ImageTransformByProfile(src_image image.Image, src_prof, dst_prof *Profile) (image.Image, error) {
var dst_image image.Image
rect := src_image.Bounds()
width := rect.Dx()
height := rect.Dy()
colorModel := src_image.ColorModel()
// 今のところ RGBA, YCbCr のみ対応
if (colorModel != color.YCbCrModel) && (colorModel != color.RGBAModel) {
return nil, fmt.Errorf("ImageTransformByProfile: Unsupported ColorModel(%d)", colorModel)
}
var src_rgba *image.RGBA
var src_ycbcr *image.YCbCr
if colorModel == color.YCbCrModel {
// YCbCr の場合は RGB に変換する
src_ycbcr = src_image.(*image.YCbCr)
src_rgba = image.NewRGBA(rect)
DrawYCbCr(src_rgba, rect, src_ycbcr, image.Pt(0, 0))
} else {
src_rgba = src_image.(*image.RGBA) // type assertions
}
transform := CreateTransform(src_prof, DATA_RGBA_8, dst_prof, DATA_RGBA_8)
defer transform.DeleteTransform()
if transform == nil {
return nil, fmt.Errorf("ImageTransformByProfile: CreateTransform Failedl(%d)", colorModel)
}
dst_rgba := image.NewRGBA(rect)
src_pix := src_rgba.Pix
dst_pix := dst_rgba.Pix
len_pix := len(src_pix)
transform.DoTransform(src_pix, dst_pix, len_pix)
// YCbCr の場合は RGB から戻す
if colorModel == color.YCbCrModel {
dst_ycbcr := image.NewYCbCr(rect, src_ycbcr.SubsampleRatio)
var x int
var y int
for y = 0; y < height; y++ {
for x = 0; x < width; x++ {
r, g, b, _ := dst_rgba.At(x, y).RGBA()
yy, cb, cr := color.RGBToYCbCr(uint8(r), uint8(g), uint8(b))
yi := dst_ycbcr.YOffset(x, y)
ci := dst_ycbcr.COffset(x, y)
dst_ycbcr.Y[yi] = yy
dst_ycbcr.Cb[ci] = cb
dst_ycbcr.Cr[ci] = cr
}
}
dst_image = image.Image(dst_ycbcr)
} else {
dst_image = image.Image(dst_rgba)
}
return dst_image, nil
}
示例3: Copy
// Copy copies the part of the source image defined by src and sr and writes to
// the part of the destination image defined by dst and the translation of sr
// so that sr.Min translates to dp.
func Copy(dst Image, dp image.Point, src image.Image, sr image.Rectangle, opts *Options) {
mask, mp, op := image.Image(nil), image.Point{}, Over
if opts != nil {
// TODO: set mask, mp and op.
}
dr := sr.Add(dp.Sub(sr.Min))
DrawMask(dst, dr, src, sr.Min, mask, mp, op)
}
示例4: testInterlacedFailWith
func testInterlacedFailWith(t *testing.T, rgb bool) {
src := readImage(t, "testdata/lenna.jpg")
dst := image.Image(image.NewYCbCr(image.Rect(0, 0, 640, 480), image.YCbCrSubsampleRatio420))
if rgb {
src = toRgb(src)
dst = toRgb(dst)
}
convert(t, dst, src, false, true, NewBicubicFilter())
convert(t, dst, src, true, true, NewBicubicFilter())
}
示例5: analyse
func analyse(settings CropSettings, img image.Image, cropWidth, cropHeight, realMinScale float64) (Crop, error) {
o := image.Image(image.NewRGBA(img.Bounds()))
now := time.Now()
edgeDetect(img, o)
log.Println("Time elapsed edge:", time.Since(now))
debugOutput(settings.DebugMode, &o, "edge")
now = time.Now()
if settings.FaceDetection {
err := faceDetect(settings, img, o)
if err != nil {
return Crop{}, err
}
log.Println("Time elapsed face:", time.Since(now))
debugOutput(settings.DebugMode, &o, "face")
} else {
skinDetect(img, o)
log.Println("Time elapsed skin:", time.Since(now))
debugOutput(settings.DebugMode, &o, "skin")
}
now = time.Now()
saturationDetect(img, o)
log.Println("Time elapsed sat:", time.Since(now))
debugOutput(settings.DebugMode, &o, "saturation")
now = time.Now()
var topCrop Crop
topScore := -1.0
cs := crops(o, cropWidth, cropHeight, realMinScale)
log.Println("Time elapsed crops:", time.Since(now), len(cs))
now = time.Now()
for _, crop := range cs {
nowIn := time.Now()
crop.Score = score(&o, &crop)
log.Println("Time elapsed single-score:", time.Since(nowIn))
if crop.Score.Total > topScore {
topCrop = crop
topScore = crop.Score.Total
}
}
log.Println("Time elapsed score:", time.Since(now))
if settings.DebugMode {
drawDebugCrop(&topCrop, &o)
debugOutput(true, &o, "final")
}
return topCrop, nil
}
示例6: drawRGBA
func drawRGBA(dst *image.RGBA, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op) {
x0, x1, dx := r.Min.X, r.Max.X, 1
y0, y1, dy := r.Min.Y, r.Max.Y, 1
if image.Image(dst) == src && r.Overlaps(r.Add(sp.Sub(r.Min))) {
if sp.Y < r.Min.Y || sp.Y == r.Min.Y && sp.X < r.Min.X {
x0, x1, dx = x1-1, x0-1, -1
y0, y1, dy = y1-1, y0-1, -1
}
}
sy := sp.Y + y0 - r.Min.Y
my := mp.Y + y0 - r.Min.Y
sx0 := sp.X + x0 - r.Min.X
mx0 := mp.X + x0 - r.Min.X
sx1 := sx0 + (x1 - x0)
i0 := dst.PixOffset(x0, y0)
di := dx * 4
for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy {
for i, sx, mx := i0, sx0, mx0; sx != sx1; i, sx, mx = i+di, sx+dx, mx+dx {
ma := uint32(m)
if mask != nil {
_, _, _, ma = mask.At(mx, my).RGBA()
}
sr, sg, sb, sa := src.At(sx, sy).RGBA()
if op == Over {
dr := uint32(dst.Pix[i+0])
dg := uint32(dst.Pix[i+1])
db := uint32(dst.Pix[i+2])
da := uint32(dst.Pix[i+3])
// dr, dg, db and da are all 8-bit color at the moment, ranging in [0,255].
// We work in 16-bit color, and so would normally do:
// dr |= dr << 8
// and similarly for dg, db and da, but instead we multiply a
// (which is a 16-bit color, ranging in [0,65535]) by 0x101.
// This yields the same result, but is fewer arithmetic operations.
a := (m - (sa * ma / m)) * 0x101
dst.Pix[i+0] = uint8((dr*a + sr*ma) / m >> 8)
dst.Pix[i+1] = uint8((dg*a + sg*ma) / m >> 8)
dst.Pix[i+2] = uint8((db*a + sb*ma) / m >> 8)
dst.Pix[i+3] = uint8((da*a + sa*ma) / m >> 8)
} else {
dst.Pix[i+0] = uint8(sr * ma / m >> 8)
dst.Pix[i+1] = uint8(sg * ma / m >> 8)
dst.Pix[i+2] = uint8(sb * ma / m >> 8)
dst.Pix[i+3] = uint8(sa * ma / m >> 8)
}
}
i0 += dy * dst.Stride
}
}
示例7: drawRGBA
func drawRGBA(dst *image.RGBA, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op) {
x0, x1, dx := r.Min.X, r.Max.X, 1
y0, y1, dy := r.Min.Y, r.Max.Y, 1
if image.Image(dst) == src && r.Overlaps(r.Add(sp.Sub(r.Min))) {
if sp.Y < r.Min.Y || sp.Y == r.Min.Y && sp.X < r.Min.X {
x0, x1, dx = x1-1, x0-1, -1
y0, y1, dy = y1-1, y0-1, -1
}
}
sy := sp.Y + y0 - r.Min.Y
my := mp.Y + y0 - r.Min.Y
sx0 := sp.X + x0 - r.Min.X
mx0 := mp.X + x0 - r.Min.X
i0 := (y0 - dst.Rect.Min.Y) * dst.Stride
for y := y0; y != y1; y, sy, my = y+dy, sy+dy, my+dy {
dpix := dst.Pix[i0:]
for x, sx, mx := x0, sx0, mx0; x != x1; x, sx, mx = x+dx, sx+dx, mx+dx {
ma := uint32(m)
if mask != nil {
_, _, _, ma = mask.At(mx, my).RGBA()
}
sr, sg, sb, sa := src.At(sx, sy).RGBA()
var dr, dg, db, da uint32
if op == Over {
rgba := dpix[x-dst.Rect.Min.X]
dr = uint32(rgba.R)
dg = uint32(rgba.G)
db = uint32(rgba.B)
da = uint32(rgba.A)
// dr, dg, db and da are all 8-bit color at the moment, ranging in [0,255].
// We work in 16-bit color, and so would normally do:
// dr |= dr << 8
// and similarly for dg, db and da, but instead we multiply a
// (which is a 16-bit color, ranging in [0,65535]) by 0x101.
// This yields the same result, but is fewer arithmetic operations.
a := (m - (sa * ma / m)) * 0x101
dr = (dr*a + sr*ma) / m
dg = (dg*a + sg*ma) / m
db = (db*a + sb*ma) / m
da = (da*a + sa*ma) / m
} else {
dr = sr * ma / m
dg = sg * ma / m
db = sb * ma / m
da = sa * ma / m
}
dpix[x-dst.Rect.Min.X] = image.RGBAColor{uint8(dr >> 8), uint8(dg >> 8), uint8(db >> 8), uint8(da >> 8)}
}
i0 += dy * dst.Stride
}
}
示例8: BenchmarkEdge
func BenchmarkEdge(b *testing.B) {
fname := "24391757.jpg"
fi, _ := os.Open("./samples/" + fname)
defer fi.Close()
img, _, err := image.Decode(fi)
if err != nil {
b.Error(err)
}
o := image.Image(image.NewRGBA(img.Bounds()))
b.ResetTimer()
for i := 0; i < b.N; i++ {
edgeDetect(img, o)
}
}
示例9: analyse
func analyse(img *image.Image, cropWidth, cropHeight, realMinScale float64) Crop {
o := image.Image(image.NewRGBA((*img).Bounds()))
now := time.Now()
edgeDetect(img, &o)
fmt.Println("Time elapsed edge:", time.Since(now))
debugOutput(&o, "edge")
now = time.Now()
if useFaceDetection {
faceDetect(img, &o)
fmt.Println("Time elapsed face:", time.Since(now))
debugOutput(&o, "face")
} else {
skinDetect(img, &o)
fmt.Println("Time elapsed skin:", time.Since(now))
debugOutput(&o, "skin")
}
now = time.Now()
saturationDetect(img, &o)
fmt.Println("Time elapsed sat:", time.Since(now))
debugOutput(&o, "saturation")
now = time.Now()
var topCrop Crop
topScore := -1.0
cs := crops(&o, cropWidth, cropHeight, realMinScale)
fmt.Println("Time elapsed crops:", time.Since(now), len(cs))
now = time.Now()
for _, crop := range cs {
// nowIn := time.Now()
crop.Score = score(&o, &crop)
// fmt.Println("Time elapsed single-score:", time.Since(nowIn))
if crop.Score.Total > topScore {
topCrop = crop
topScore = crop.Score.Total
}
}
fmt.Println("Time elapsed score:", time.Since(now))
if debug {
drawDebugCrop(&topCrop, &o)
}
debugOutput(&o, "final")
return topCrop
}
示例10: analyse
func analyse(img *image.Image, cropWidth, cropHeight, realMinScale float64) Crop {
o := image.Image(image.NewRGBA((*img).Bounds()))
now := time.Now()
edgeDetect(img, &o)
fmt.Println("Time elapsed edge:", time.Since(now))
if debug {
writeImageToPng(&o, "./smartcrop_edge.png")
}
now = time.Now()
skinDetect(img, &o)
fmt.Println("Time elapsed skin:", time.Since(now))
if debug {
writeImageToPng(&o, "./smartcrop_skin.png")
}
now = time.Now()
saturationDetect(img, &o)
fmt.Println("Time elapsed sat:", time.Since(now))
if debug {
writeImageToPng(&o, "./smartcrop_sat.png")
}
now = time.Now()
var topCrop Crop
topScore := -1.0
cs := crops(&o, cropWidth, cropHeight, realMinScale)
fmt.Println("Time elapsed crops:", time.Since(now), len(cs))
now = time.Now()
for _, crop := range cs {
// nowIn := time.Now()
crop.Score = score(&o, &crop)
// fmt.Println("Time elapsed single-score:", time.Since(nowIn))
if crop.Score.Total > topScore {
topCrop = crop
topScore = crop.Score.Total
}
}
fmt.Println("Time elapsed score:", time.Since(now))
if debug {
drawDebugCrop(&topCrop, &o)
writeImageToPng(&o, "./smartcrop_debug.png")
}
return topCrop
}
示例11: Cube
// imgを32x32のブロックの畫像に変換する
func Cube(img *image.Image) image.Image {
layer := image.Image(image.NewNRGBA(image.Rect(0, 0, 32, 32)))
top := Rotate(img, 45)
top = Resize(&top, 28, 15)
left := Slide(img, 2, true, false)
left = Resize(&left, 14, 24)
right := Slide(img, 2, true, true)
right = Resize(&right, 14, 24)
Paste(&layer, 2, 0, &top)
Paste(&layer, 2, 8, &left)
Paste(&layer, 16, 6, &right)
return layer
}
示例12: resizeEmojiGif
func resizeEmojiGif(gifImg *gif.GIF) *gif.GIF {
// Create a new RGBA image to hold the incremental frames.
firstFrame := gifImg.Image[0].Bounds()
b := image.Rect(0, 0, firstFrame.Dx(), firstFrame.Dy())
img := image.NewRGBA(b)
resizedImage := image.Image(nil)
// Resize each frame.
for index, frame := range gifImg.Image {
bounds := frame.Bounds()
draw.Draw(img, bounds, frame, bounds.Min, draw.Over)
resizedImage = resizeEmoji(img, firstFrame.Dx(), firstFrame.Dy())
gifImg.Image[index] = imageToPaletted(resizedImage)
}
// Set new gif width and height
gifImg.Config.Width = resizedImage.Bounds().Dx()
gifImg.Config.Height = resizedImage.Bounds().Dy()
return gifImg
}
示例13: benchGlyph
// benchGlyph benchmarks rasterizing a TrueType glyph.
//
// Note that, compared to the github.com/google/font-go prototype, the height
// here is the height of the bounding box, not the pixels per em used to scale
// a glyph's vectors. A height of 64 corresponds to a ppem greater than 64.
func benchGlyph(b *testing.B, colorModel byte, loose bool, height int, op draw.Op) {
width, data := scaledBenchmarkGlyphData(height)
z := NewRasterizer(width, height)
bounds := z.Bounds()
if loose {
bounds.Max.X++
}
dst, src := draw.Image(nil), image.Image(nil)
switch colorModel {
case 'A':
dst = image.NewAlpha(bounds)
src = image.Opaque
case 'N':
dst = image.NewNRGBA(bounds)
src = image.NewUniform(color.NRGBA{0x40, 0x80, 0xc0, 0xff})
case 'R':
dst = image.NewRGBA(bounds)
src = image.NewUniform(color.RGBA{0x40, 0x80, 0xc0, 0xff})
default:
b.Fatal("unsupported color model")
}
bounds = z.Bounds()
b.ResetTimer()
for i := 0; i < b.N; i++ {
z.Reset(width, height)
z.DrawOp = op
for _, d := range data {
switch d.n {
case 0:
z.MoveTo(d.px, d.py)
case 1:
z.LineTo(d.px, d.py)
case 2:
z.QuadTo(d.px, d.py, d.qx, d.qy)
}
}
z.Draw(dst, bounds, src, image.Point{})
}
}
示例14: analyse
func analyse(img *image.Image) Crop {
o := image.Image(image.NewRGBA((*img).Bounds()))
now := time.Now()
edgeDetect(img, &o)
fmt.Println("Time elapsed edge:", time.Since(now))
// writeImageToJpeg(&o, "/tmp/smartcrop_step1.jpg")
now = time.Now()
skinDetect(img, &o)
fmt.Println("Time elapsed skin:", time.Since(now))
// writeImageToJpeg(&o, "/tmp/smartcrop_step2.jpg")
now = time.Now()
saturationDetect(img, &o)
fmt.Println("Time elapsed sat:", time.Since(now))
// writeImageToJpeg(&o, "/tmp/smartcrop_step3.jpg")
now = time.Now()
var topCrop Crop
topScore := -1.0
cs := crops(&o)
fmt.Println("Time elapsed crops:", time.Since(now), len(cs))
now = time.Now()
for _, crop := range cs {
// nowIn := time.Now()
crop.Score = score(&o, &crop)
// fmt.Println("Time elapsed single-score:", time.Since(nowIn))
if crop.Score.Total > topScore {
topCrop = crop
topScore = crop.Score.Total
}
}
fmt.Println("Time elapsed score:", time.Since(now))
return topCrop
}
示例15: windowOnClickHandler
func windowOnClickHandler(me gxui.MouseEvent) {
y := yMax - yMin // mandelbrot axis size
x := xMax - xMin
xp := float64(me.WindowPoint.X) // point clicked on screen in pixels
yp := float64(me.WindowPoint.Y)
// find point clicked in mandelbrot space
xm := xMin + (xp/1024)*x
ym := yMin + (yp/1024)*y
// scale viewport of mandelbrot space
if me.Button == gxui.MouseButtonLeft {
x = x / 2
y = y / 2
} else {
x = x * 2
y = y * 2
}
yMax = ym + y/2
yMin = ym - y/2
xMax = xm + x/2
xMin = xm - x/2
//fmt.Print(xm, ym)
//fmt.Print(yMax, yMin, xMax, xMin)
source := image.Image(newMandelbrot())
rgba := image.NewRGBA(source.Bounds())
draw.Draw(rgba, source.Bounds(), source, image.ZP, draw.Src)
texture = d.CreateTexture(rgba, 1)
img.SetTexture(texture)
window.Redraw()
}