Documentation ¶
Overview ¶
Package wikimg can pull the latest image URLs from Wikimedia Commons https://commons.wikimedia.org and map image colors to an xterm256 color palette.
Example ¶
package main import ( "fmt" "io" "io/ioutil" "net/http" "os" "github.com/brnstz/routine/wikimg" ) func main() { // Save 10 random images to a directory // Create a pull with max 10 results p := wikimg.NewPuller(10) // Create temp dir for storing the images dir, err := ioutil.TempDir("", "") if err != nil { panic(err) } // Remove dir after test is complete defer os.RemoveAll(dir) for { // Get the next URL imgURL, err := p.Next() if err == wikimg.EndOfResults { // We've reached the end break } else if err != nil { // There's an unexpected error panic(err) } // Call GET on the image URL resp, err := http.Get(imgURL) if err != nil { panic(err) } // Open a temporary file fh, err := ioutil.TempFile(dir, "") if err != nil { // We need to close our HTTP response here too resp.Body.Close() panic(err) } // Copy GET results to file and close stuff _, err = io.Copy(fh, resp.Body) fh.Close() resp.Body.Close() if err != nil { panic(err) } } files, _ := ioutil.ReadDir(dir) fmt.Println(len(files)) }
Output: 10
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // EndOfResults is returned by Next() when no more results are available EndOfResults = errors.New("end of results") // Canceled may be returned by Next() and FirstColor() when the client // closes the Cancel channel on a Puller Canceled = errors.New("wikimg: canceled image processing") )
var XTerm256 = []color.Color{}/* 256 elements not displayed */
XTerm256 is a 256 color palette where the index value is the xterm-256 color id. For example, 0 = #000000 (black) and 1 = #800000 (red) Source: http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html
Functions ¶
This section is empty.
Types ¶
type Puller ¶
type Puller struct { // Cancel is an optional channel. Setting this value on Puller // and closing the channel signals to the Puller that any // in process operations (i.e, retrieving an image or computing // its first color) should be canceled. Any future // calls to Next() or FirstColor() will return a Canceled // error. Cancel <-chan struct{} // contains filtered or unexported fields }
Puller is an image puller that retrieves the most recent image URLs that have been uploaded to Wikimedia Commons https://commons.wikimedia.org
func NewPuller ¶
NewPuller creates a puller that can return at most max images when calls to Next() are made
func (*Puller) FirstColor ¶
FirstColor tries to return the first non-gray color in the image. A gray color is one that, when mapped to an xterm256 palette, has the same value for red, green and blue. We iterate through pixels starting with 0,0 and through each x and y value. In the worst case (a grayscale image), we iterate through every pixel, give up, and return the final pixel color even though it's gray. Both the xtermColor (an integer between 0-255) and a hex string (e.g., "#bb00cc") is returned.