Skip to content

More efficient method for obtaining an image width and height #695

@ajstarks

Description

@ajstarks
The current method for obtaining the width and height of a png or jpg is inefficient if
only the
the dimensions are required

What steps will reproduce the problem?

Given the program and  two images:


/Users/ajstarks/Images/yankee-stadium.jpg 8708 2086
/Users/ajstarks/Images/ys.jpg 8648 1872

real    0m3.554s
user    0m3.269s
sys 0m0.271s


by comparison,  the python script:
import sys, Image
for f in sys.argv[1:]:
    i=Image.open(f)
    print f, i.size[0], i.size[1]

/Users/ajstarks/Images/yankee-stadium.jpg 8708 2086
/Users/ajstarks/Images/ys.jpg 8648 1872

real    0m0.039s
user    0m0.020s
sys 0m0.018s


package main

import (
    "image/png"
    "image/jpeg"
    "strings"
    "fmt"
    "os"
)

func main() {
    for i := 1; i < len(os.Args); i++ {
        f, fok := os.Open(os.Args[i], os.O_RDONLY, 0)
        defer f.Close()

        if fok == nil {
            if strings.HasSuffix(os.Args[i], ".jpg") {
                j, ok := jpeg.Decode(f)
                if ok == nil {
                    fmt.Printf("%s %d %d\n", os.Args[i], j.Width(), j.Height())
                } else {
                    fmt.Printf("%v: %v\n", os.Args[i], ok)
                }
            } else {
                p, ok := png.Decode(f)
                if ok == nil {
                    fmt.Printf("%s %d %d\n", os.Args[i], p.Width(), p.Height())
                } else {
                    fmt.Printf("%v: %v\n", os.Args[i], ok)
                }
            }
        } else {
            fmt.Printf("Cannot open " + os.Args[i])
        }
    }
}



What is the expected output? What do you see instead?


the output is correct, yet slower than needed.  Time is dependent on image size


What is your $GOOS?  $GOARCH?

darwin amd64

Which revision are you using?  (hg identify)

9482fde11a02 release.2010-03-22/release

Please provide any additional information below.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions