本文整理匯總了Golang中image.Bounds函數的典型用法代碼示例。如果您正苦於以下問題:Golang Bounds函數的具體用法?Golang Bounds怎麽用?Golang Bounds使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Bounds函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestResizeImage
func (s *ResizeSuite) TestResizeImage(c *C) {
file, err := ioutil.ReadFile("test/exif_test_img.jpg")
c.Assert(err, IsNil)
for width, height := range sizes {
ctx := &fetch.CacheContext{
Width: width,
}
data := bytes.NewBuffer(file)
orig, _, err := fetch.GetRotatedImage(data)
c.Check(err, IsNil)
buf := new(bytes.Buffer)
jpeg.Encode(buf, orig, nil)
resized, err := fetch.Resize(buf, ctx)
c.Check(err, IsNil)
image, _, err := image.Decode(resized)
c.Check(err, IsNil)
c.Check(image.Bounds().Size().X, Equals, width)
c.Check(image.Bounds().Size().Y, Equals, height)
}
}
示例2: TestResizeImageSquare
func (s *ResizeSuite) TestResizeImageSquare(c *C) {
file, err := ioutil.ReadFile("test/awesome.jpeg")
c.Assert(err, IsNil)
for width, _ := range sizes {
ctx := &fetch.CacheContext{
Width: width,
Crop: true,
}
buf := bytes.NewReader(file)
resized, err := fetch.Resize(buf, ctx)
c.Check(err, IsNil)
image, _, err := image.Decode(resized)
c.Check(err, IsNil)
if width > 768 {
width = 768
}
c.Check(image.Bounds().Size().X, Equals, width)
c.Check(image.Bounds().Size().Y, Equals, width)
}
}
示例3: ReadBinary
func ReadBinary(tbl [][]byte, name string) (r string) {
image := GetPngData(name)
gw := image.Bounds().Max.X
gh := image.Bounds().Max.Y
for y := 12; y < gh-H; y += 22 {
for x := 9; x < gw-W; x += 36 {
s0 := SplitImage(image, W, H, x, y)
if IsOne(s0) {
continue
}
a := FindNum(tbl, s0)
s1 := SplitImage(image, W, H, x+12, y)
b := FindNum(tbl, s1)
if a < 0 || b < 0 {
fmt.Println("bad char", name, x, y, len(r))
fmt.Println("a", a)
PutPattern(s0, W)
fmt.Println("b", b)
PutPattern(s1, W)
os.Exit(1)
}
v := a*16 + b
r = r + fmt.Sprintf("%02x", v)
}
}
if len(r) != 62 {
fmt.Println("bad byte", name, len(r))
fmt.Println(r)
os.Exit(1)
}
return
}
示例4: save
func (s _Sprite) save(spriteImg image.Image, items []_ImageItem, fullWidth, fullHeight int) error {
targetFolder := fmt.Sprintf("public/images/%s", s.entry)
target := path.Join(targetFolder, s.name+".png")
if file, err := os.Create(target); err != nil {
return fmt.Errorf("Cannot create sprite file %s, %v", target, err)
} else {
defer file.Close()
if err := png.Encode(file, spriteImg); err != nil {
return nil
}
target = s.addFingerPrint(targetFolder, s.name+".png")
loggers.Succ("[Sprite][%s] Saved sprite image: %s", s.entry, target)
// generate the stylus file
stylus := "assets/stylesheets/sprites"
if err := os.MkdirAll(stylus, os.ModePerm|os.ModeDir); err != nil {
return fmt.Errorf("Cannot mkdir %s, %v", stylus, err)
}
stylus = fmt.Sprintf("assets/stylesheets/sprites/%s_%s.styl", s.entry, s.name)
if stylusFile, err := os.Create(stylus); err != nil {
return fmt.Errorf("Cannot create the stylus file for sprite %s, %v", stylus, err)
} else {
defer stylusFile.Close()
spriteEntry := SpriteEntry{
Entry: s.entry,
Name: s.name,
Url: fmt.Sprintf("%s/images/%s/%s", s.config.UrlPrefix, s.entry, filepath.Base(target)),
Sprites: make([]SpriteImage, len(items)),
Width: fullWidth / s.pixelRatio,
Height: fullHeight / s.pixelRatio,
}
lastHeight := 0
for i, image := range items {
name := image.name
name = name[:len(name)-len(filepath.Ext(name))]
width, height := image.Bounds().Dx(), image.Bounds().Dy()
if width%s.pixelRatio != 0 || height%s.pixelRatio != 0 {
loggers.Warn("You have images cannot be adjusted by the pixel ratio, %s, bounds=%v, pixelRatio=%d",
image.fullpath, image.Bounds(), s.pixelRatio)
}
spriteEntry.Sprites[i] = SpriteImage{
Name: fmt.Sprintf("%s-%s", s.name, name),
X: 0,
Y: -1 * lastHeight,
Width: width / s.pixelRatio,
Height: height / s.pixelRatio,
}
lastHeight += (height + s.pixelRatio) / s.pixelRatio
}
if err := tmSprites.Execute(stylusFile, spriteEntry); err != nil {
return fmt.Errorf("Cannot generate stylus for sprites %s, %v", spriteEntry, err)
}
}
}
return nil
}
示例5: TestPreviewPdf
func (s *ResizeSuite) TestPreviewPdf(c *C) {
file, err := ioutil.ReadFile("test/trunq_sprint_7-1.pdf")
c.Assert(err, IsNil)
buf := bytes.NewReader(file)
preview, err := getPdfPreview(buf)
c.Check(err, IsNil)
previewBuf := bytes.NewReader(preview)
image, _, err := image.Decode(previewBuf)
c.Check(err, IsNil)
c.Check(image.Bounds().Size().X, Equals, 1650)
c.Check(image.Bounds().Size().Y, Equals, 1275)
}
示例6: AnalyzeImage
func AnalyzeImage(inp string) (color [3]float64, err error) {
reader, err := os.Open(inp)
if err != nil {
return
}
defer reader.Close()
image, _, err := image.Decode(reader)
if err != nil {
return
}
bounds := image.Bounds()
var reds uint32
var greens uint32
var blues uint32
var pixels uint32
for i := 0; i <= bounds.Max.X; i++ {
for j := 0; j <= bounds.Max.Y; j++ {
pixel := image.At(i, j)
red, green, blue, _ := pixel.RGBA()
reds += red
blues += blue
greens += green
pixels += 1
}
}
color = [3]float64{float64(reds) / float64(pixels), float64(blues) / float64(pixels), float64(greens) / float64(pixels)}
return
}
示例7: processFile
func processFile(src io.Reader, mime string, bucket string) (*Uploadable, error) {
if mime == "image/jpeg" || mime == "image/jpg" {
image, format, err := fetch.GetRotatedImage(src)
if err != nil {
return nil, err
}
if format != "jpeg" {
return nil, errors.New("You sent a bad JPEG file.")
}
width := image.Bounds().Size().X
height := image.Bounds().Size().Y
key := fileKey(bucket, width, height)
data := new(bytes.Buffer)
err = jpeg.Encode(data, image, nil)
if err != nil {
return nil, err
}
length := int64(data.Len())
return &Uploadable{data, key, length}, nil
} else {
raw, err := ioutil.ReadAll(src)
if err != nil {
return nil, err
}
data := bytes.NewReader(raw)
length := int64(data.Len())
image, _, err := image.Decode(data)
if err != nil {
return nil, err
}
width := image.Bounds().Size().X
height := image.Bounds().Size().Y
key := fileKey(bucket, width, height)
data.Seek(0, 0)
return &Uploadable{data, key, length}, nil
}
}
示例8: TestResizeOversizedImageSquare
func (s *ResizeSuite) TestResizeOversizedImageSquare(c *C) {
file, err := ioutil.ReadFile("test/awesome-small.jpg")
c.Assert(err, IsNil)
ctx := &fetch.CacheContext{
Width: 400,
Crop: true,
}
buf := bytes.NewReader(file)
resized, err := fetch.Resize(buf, ctx)
c.Check(err, IsNil)
image, _, err := image.Decode(resized)
c.Check(err, IsNil)
c.Check(image.Bounds().Size().X, Equals, 150)
c.Check(image.Bounds().Size().Y, Equals, 150)
}
示例9: TestHandler
func TestHandler(t *testing.T) {
assert, p, w := setup(t)
server := setupUpstreamServer("test/images/gopher.png")
defer server.Close()
url := "https://front.com?url=" + server.URL
req, _ := http.NewRequest("GET", url, nil)
p.handler(w, req)
assert.Equal(w.Code, 200, "status should be ok")
assert.Equal(w.HeaderMap.Get("Content-Type"), "image/png")
assert.NotEmpty(w.Body)
image, _, _ := image.Decode((w.Body))
// Should be original size
assert.Equal(image.Bounds().Dx(), 250, "width should be 250")
assert.Equal(image.Bounds().Dy(), 340, "height should be 340")
}
示例10: TestResizeNoExifImage
func (s *ResizeSuite) TestResizeNoExifImage(c *C) {
file, err := ioutil.ReadFile("test/AWESOME.jpg")
c.Assert(err, IsNil)
for width, height := range noExifSizes {
ctx := &fetch.CacheContext{
Width: width,
}
buf := bytes.NewReader(file)
resized, err := fetch.Resize(buf, ctx)
c.Check(err, IsNil)
image, _, err := image.Decode(resized)
c.Check(err, IsNil)
c.Check(image.Bounds().Size().X, Equals, width)
c.Check(image.Bounds().Size().Y, Equals, height)
}
}
示例11: TestHandlerWithSizeParams
func TestHandlerWithSizeParams(t *testing.T) {
assert, p, w := setup(t)
server := setupUpstreamServer("test/images/gopher.png")
defer server.Close()
url := "https://front.com?width=500&height=100&url=" + server.URL
req, _ := http.NewRequest("GET", url, nil)
p.handler(w, req)
assert.Equal(w.Code, 200, "status should be ok")
assert.Equal(w.HeaderMap.Get("Content-Type"), "image/jpeg")
assert.NotEmpty(w.Body)
image, _, _ := image.Decode((w.Body))
// Should be resized
assert.Equal(image.Bounds().Dx(), 500, "width should be 500")
assert.Equal(image.Bounds().Dy(), 100, "height should be 100")
}
示例12: Build
func (s _Sprite) Build(isProduction bool) error {
targetFolder := fmt.Sprintf("public/images/%s", s.entry)
if err := os.MkdirAll(targetFolder, os.ModePerm|os.ModeDir); err != nil {
return fmt.Errorf("Cannot mkdir %s, %v", targetFolder, err)
}
if imageItems, err := s.getImages(s.fullpath); err != nil {
return err
} else {
files := make([]*os.File, len(imageItems))
images := make([]_ImageItem, len(imageItems))
width, height := 0, 0
for i, imgItem := range imageItems {
if file, err := os.Open(imgItem.fullpath); err != nil {
return fmt.Errorf("Cannot open image file: %s, %v", imgItem.fullpath, err)
} else {
if image, _, err := image.Decode(file); err != nil {
return err
} else {
files[i] = file
images[i] = _ImageItem{imgItem, image}
height += image.Bounds().Dy() + s.pixelRatio
if width < image.Bounds().Dx() {
width = image.Bounds().Dx()
}
}
}
}
spriteImage := image.NewNRGBA(image.Rect(0, 0, width, height))
yOffset := 0
for i := range images {
files[i].Close()
newBounds := image.Rect(0, yOffset, images[i].Bounds().Dx(), yOffset+images[i].Bounds().Dy())
draw.Draw(spriteImage, newBounds, images[i], image.Point{0, 0}, draw.Src)
yOffset += images[i].Bounds().Dy() + s.pixelRatio
}
return s.save(spriteImage, images, width, height)
}
return nil
}
示例13: main
func main() {
if len(os.Args) <= 1 {
fmt.Fprint(os.Stderr, "usage: go-colorweave <filename>")
return
}
reader, err := os.Open(os.Args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
defer reader.Close()
image, _, err := image.Decode(reader)
if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
}
// Resize the image to smaller scale for faster computation
image = resize.Resize(100, 0, image, resize.Bilinear)
bounds := image.Bounds()
ColorCounter := make(map[string]int)
Limit := 5 // Limiting how many colors to be displayed in output
TotalPixels := bounds.Max.X * bounds.Max.Y
for i := 0; i <= bounds.Max.X; i++ {
for j := 0; j <= bounds.Max.Y; j++ {
pixel := image.At(i, j)
red, green, blue, _ := pixel.RGBA()
RGBTuple := []int{int(red / 255), int(green / 255), int(blue / 255)}
ColorName := FindClosestColor(RGBTuple, "css21")
_, present := ColorCounter[ColorName]
if present {
ColorCounter[ColorName] += 1
} else {
ColorCounter[ColorName] = 1
}
}
}
// Sort by the frequency of each color
keys := make([]int, 0, len(ColorCounter))
for _, val := range ColorCounter {
keys = append(keys, val)
}
sort.Sort(sort.Reverse(sort.IntSlice(keys)))
ReverseColorCounter := ReverseMap(ColorCounter)
// Display the top N dominant colors from the image
for _, val := range keys[:Limit] {
fmt.Printf("%s %.2f%%\n", ReverseColorCounter[val], ((float64(val) / float64(TotalPixels)) * 100))
}
}
示例14: ReadImage
// ReadImage loads an image from an image file and generates a vector which represents its black and
// white pixels.
func ReadImage(path string) (*Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
image, _, err := image.Decode(file)
if err != nil {
return nil, err
}
res := &Image{
Vector: make([]float64, image.Bounds().Dx()*image.Bounds().Dy()),
Width: image.Bounds().Dx(),
Height: image.Bounds().Dy(),
}
idx := 0
for y := 0; y < image.Bounds().Dy(); y++ {
for x := 0; x < image.Bounds().Dx(); x++ {
r, g, b, _ := image.At(x, y).RGBA()
black := float64(r+g+b) / float64(0xffff*3)
res.Vector[idx] = black
idx++
}
}
return res, nil
}
示例15: Resize
func Resize(src io.Reader, c *CacheContext) (io.Reader, error) {
raw, err := ioutil.ReadAll(src)
if err != nil {
return nil, err
}
options := vips.Options{
Width: c.Width,
Crop: true,
Extend: vips.EXTEND_WHITE,
Interpolator: vips.BILINEAR,
Gravity: vips.CENTRE,
Quality: 80,
}
if c.Crop {
data := bytes.NewReader(raw)
image, _, err := image.Decode(data)
if err != nil {
return nil, err
}
minDimension := int(math.Min(float64(image.Bounds().Size().X), float64(image.Bounds().Size().Y)))
if minDimension < options.Width || options.Width == 0 {
options.Width = minDimension
}
options.Height = options.Width
}
res, err := vips.Resize(raw, options)
if err != nil {
return nil, err
}
return bytes.NewBuffer(res), err
}