func Fuzz(data []byte) int { cfg, err := jpeg.DecodeConfig(bytes.NewReader(data)) if err != nil { return 0 } if cfg.Width*cfg.Height > 1e6 { return 0 } img, err := jpeg.Decode(bytes.NewReader(data)) if err != nil { return 0 } for q := 0; q <= 100; q += 10 { var w bytes.Buffer err = jpeg.Encode(&w, img, &jpeg.Options{q}) if err != nil { panic(err) } img1, err := jpeg.Decode(&w) if err != nil { panic(err) } if !reflect.DeepEqual(img.Bounds(), img1.Bounds()) { panic("bounds changed") } } return 1 }
func (c Creature) CropImage(width, height, max int, ctx appengine.Context) (image.Image, error) { url, _ := aeimg.ServingURL(ctx, c.BlobKey, &aeimg.ServingURLOptions{Size: max, Crop: true}) client := urlfetch.Client(ctx) resp, _ := client.Get(url.String()) // Just in case if resp.StatusCode != 200 { return nil, errors.New("Blob not found") } // Do we need further cropping? if width == height { return jpeg.Decode(resp.Body) } src, err := jpeg.Decode(resp.Body) dest := image.NewRGBA(image.Rect(0, 0, width, height)) if err != nil { return nil, err } graphics.Thumbnail(dest, src) return dest, nil }
// Test the delete function. func TestDelete(t *testing.T) { store := New() // Add some images. addA, _ := jpeg.Decode(base64.NewDecoder(base64.StdEncoding, strings.NewReader(imgA))) addB, _ := jpeg.Decode(base64.NewDecoder(base64.StdEncoding, strings.NewReader(imgB))) hashA, _ := CreateHash(addA) hashB, _ := CreateHash(addB) store.Add("imgA", hashA) store.Add("imgB", hashB) // Delete one. store.Delete("imgA") // Query should only return imgB. query, _ := jpeg.Decode(base64.NewDecoder(base64.StdEncoding, strings.NewReader(imgC))) queryHash, _ := CreateHash(query) matches := store.Query(queryHash) if len(matches) != 1 { t.Errorf("Invalid query result set size, expected 1, is %d", len(matches)) return } if matches[0].ID != "imgB" { t.Errorf("Query found %s but should have found imgB", matches[0].ID) } }
func main() { f, err := os.Open("Lenna50.jpg") if err != nil { fmt.Println(err) return } defer f.Close() i50, err := jpeg.Decode(f) if err != nil { fmt.Println(err) return } if f, err = os.Open("Lenna100.jpg"); err != nil { fmt.Println(err) return } defer f.Close() i100, err := jpeg.Decode(f) if err != nil { fmt.Println(err) return } if i50.ColorModel() != i100.ColorModel() { fmt.Println("different color models") return } b := i50.Bounds() if !b.Eq(i100.Bounds()) { fmt.Println("different image sizes") return } var sum int64 for y := b.Min.Y; y < b.Max.Y; y++ { for x := b.Min.X; x < b.Max.X; x++ { r1, g1, b1, _ := i50.At(x, y).RGBA() r2, g2, b2, _ := i100.At(x, y).RGBA() if r1 > r2 { sum += int64(r1 - r2) } else { sum += int64(r2 - r1) } if g1 > g2 { sum += int64(g1 - g2) } else { sum += int64(g2 - g1) } if b1 > b2 { sum += int64(b1 - b2) } else { sum += int64(b2 - b1) } } } nPixels := (b.Max.X - b.Min.X) * (b.Max.Y - b.Min.Y) fmt.Printf("Image difference: %f%%\n", float64(sum*100)/(float64(nPixels)*0xffff*3)) }
func main() { op := gif.Options{256, nil, nil} gifWriter, err := os.Create("new.gif") if err != nil { fmt.Println("something went wrong") } image1, _ := os.Open("image1.jpeg") image2, _ := os.Open("image2.jpeg") jpeg1, _ := jpeg.Decode(image1) jpeg2, _ := jpeg.Decode(image2) gif.Encode(gifWriter, jpeg1, &op) gif.Encode(gifWriter, jpeg2, &op) }
func loader(inChan chan loaderRequest, outChan chan *Image, errChan chan *Error, stopChan chan int) { for { select { case req := <-inChan: switch strings.ToLower(path.Ext(req.name)) { case ".jpg": if img, err := jpeg.Decode(req.reader); err == nil { outChan <- &Image{Name: req.name, Data: img} } else { errChan <- &Error{Name: req.name, Message: err.Error()} } case ".png": if img, err := png.Decode(req.reader); err == nil { outChan <- &Image{Name: req.name, Data: img} } else { errChan <- &Error{Name: req.name, Message: err.Error()} } } if err := req.reader.Close(); err != nil { fmt.Errorf("%s\n", err) } case <-stopChan: return } } }
// decodeMJPEG turns gstreamer 'jpegenc' output into a stream of images. func decodeMJPEG(input io.Reader) chan image.Image { stream := make(chan image.Image) go func() { in := newReader(bufio.NewReaderSize(input, 64*1024*1024)) //in := newReader(input) for { printStats() tDec.Start() img, err := jpeg.Decode(in) tDec.Stop() //if *flagRec != "" { // record(img) //} if err != nil { if err.Error() == "unexpected EOF" { close(stream) } if err.Error() != "invalid JPEG format: missing SOI marker" { exit(err) } nErrors++ continue } select { default: nDropped++ case stream <- img: nProcessed++ } } }() return stream }
func read(filename string) (image.Image, error) { file, err := os.Open(filename) if err != nil { return nil, err } return jpeg.Decode(file) }
func thumb() image.Image { imageName := "test.jpg" imageFile, err := os.Open(imageName) if err != nil { log.Fatal(err) } var myImage *image.Image switch strings.ToLower(path.Ext(imageName)) { case ".jpg", ".jpeg": img, err := jpeg.Decode(imageFile) if err != nil { log.Fatal(err) } myImage = &img case ".png": img, err := png.Decode(imageFile) if err != nil { log.Fatal(err) } myImage = &img } imageFile.Close() m := resize.Resize(0, 200, *myImage, resize.MitchellNetravali) return m }
// Decode reads a CR2 image from r and returns the embedded JPEG thumbnail as // an image.Image. func Decode(r io.Reader) (image.Image, error) { r, err := NewReader(r) if err != nil { return nil, err } return jpeg.Decode(r) }
func createSmallPic(file io.Reader, fileSmall string, w, h int) error { // decode jpeg into image.Image img, err := jpeg.Decode(file) if err != nil { return err } b := img.Bounds() if w > b.Dx() { w = b.Dx() } if h > b.Dy() { h = b.Dy() } // resize to width 1000 using Lanczos resampling // and preserve aspect ratio m := resize.Resize(uint(w), uint(h), img, resize.Lanczos3) out, err := os.Create(fileSmall) if err != nil { return err } defer out.Close() // write new image to file return jpeg.Encode(out, m, nil) }
func ResizeImage(src, dst string) (err error) { // open "test.jpg" file, err := os.Open(src) if err != nil { // mlog.Error(err) return err } defer file.Close() // decode jpeg into image.Image img, err := jpeg.Decode(file) if err != nil { // mlog.Error(err) return err } // file.Close() // resize to width 1000 using Lanczos resampling // and preserve aspect ratio m := resize.Resize(80, 0, img, resize.Lanczos3) out, err := os.Create(dst) if err != nil { // mlog.Error(err) return err } defer out.Close() // write new image to file return jpeg.Encode(out, m, nil) }
//This private method provides decoding image //Returns decoded image. If during decoding process occurred problem, return error func decodingImage(data []byte) (image.Image, error) { rimg, err := jpeg.Decode(bytes.NewReader(data)) if err != nil { return nil, err } return rimg, nil }
func main() { var source string flag.StringVar(&source, "source", "./test.jpg", "image to read") var dest string flag.StringVar(&dest, "dest", "./out.jpg", "image to output") var sizeStr string flag.StringVar(&sizeStr, "size", "100w", "size to resize to") flag.Parse() file, err := os.Open(source) if err != nil { log.Fatal(err) } defer file.Close() // Decode the image. m, err := jpeg.Decode(file) if err != nil { fmt.Printf("error decoding image\n") log.Fatal(err) } outputImage := resize.Resize(m, sizeStr) fl, err := os.OpenFile(dest, os.O_CREATE|os.O_RDWR, 0644) if err != nil { fmt.Println("couldn't write", err) return } defer fl.Close() jpeg.Encode(fl, outputImage, nil) }
func openImage(t *testing.T, path string) image.Image { fireTruck, err := os.Open(path) assert.NoError(t, err, "error openenig image") img, err := jpeg.Decode(fireTruck) assert.NoError(t, err, "error decoding image") return img }
// Image returns an image object from the JPG. func (r *Rep) Image() (image.Image, error) { var buf bytes.Buffer if c, err := buf.ReadFrom(r); err != nil || c == 0 { return nil, err } return jpeg.Decode(&buf) }
// Resize all images given to it func resizeImages(path string, dim *Dimensions, progress chan int) { file, err := os.Open(path) if err != nil { log.Fatal(err) progress <- 1 return } img, err := jpeg.Decode(file) file.Close() if err != nil { log.Fatal(err) progress <- 1 return } res := resize.Resize(dim.height, dim.width, img, resize.Lanczos3) out, err := os.Create(PutImagePath + strconv.FormatInt(rand.Int63(), 36) + ".jpg") if err != nil { log.Fatal(err) progress <- 1 return } defer out.Close() jpeg.Encode(out, res, nil) progress <- 1 }
func main() { doc := pdf.New() canvas := doc.NewPage(pdf.USLetterWidth, pdf.USLetterHeight) canvas.Translate(pdf.USLetterWidth/2, pdf.USLetterHeight/2) f, err := os.Open("testdata/suzanne.jpg") if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } defer f.Close() img, err := jpeg.Decode(f) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } rect := &pdf.Rectangle{ pdf.Point{-100, -100}, // min pdf.Point{100, 100}} // max // draw an image canvas.DrawImage(img, *rect) canvas.Close() err = doc.Encode(os.Stdout) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }
// Make thumbnail for the selected file func MakeThumb(src string, dst_sane string, dst_thumb string) error { file, err := os.Open(src) defer file.Close() if err != nil { return err } img, err := jpeg.Decode(file) if err != nil { return err } sane := resize.Resize(1080, 0, img, resize.Bilinear) out_sane, err := os.Create(dst_sane) if err != nil { return err } defer out_sane.Close() jpeg.Encode(out_sane, sane, nil) thumb := resize.Thumbnail(200, 200, img, resize.Bilinear) out_thumb, err := os.Create(dst_thumb) if err != nil { return err } defer out_thumb.Close() jpeg.Encode(out_thumb, thumb, nil) return nil }
func (c *appContext) uploadPic(a *multipart.FileHeader) (string, string) { log.Println("In upload pic territory") bucket := c.bucket file, err := a.Open() defer file.Close() if err != nil { panic(err.Error()) } if err != nil { panic(err) } buf, _ := ioutil.ReadAll(file) fn := uuid.New() fname := "places/" + fn + path.Ext(a.Filename) thumbname := "placesthumb/" + fn + path.Ext(a.Filename) log.Println(fname) b := "http://s3-us-west-2.amazonaws.com/" + c.bucket.Name + "/" + fname d := "http://s3-us-west-2.amazonaws.com/" + c.bucket.Name + "/" + thumbname filetype := http.DetectContentType(buf) err = bucket.Put(fname, buf, filetype, s3.PublicRead) if err != nil { log.Println("bucket put error for main image") panic(err.Error()) } log.Print("added a full image") img, err := jpeg.Decode(bytes.NewReader(buf)) if err != nil { log.Println(err.Error()) } m := resize.Resize(200, 200, img, resize.Lanczos2) buf2 := new(bytes.Buffer) err = jpeg.Encode(buf2, m, nil) if err != nil { fmt.Println(err.Error()) } thumb := buf2.Bytes() filetype2 := http.DetectContentType(thumb) err = bucket.Put(thumbname, thumb, filetype2, s3.PublicRead) if err != nil { log.Println("bucket put error for thumbnail") panic(err.Error()) } log.Println("uploaded one thumb image") return b, d }
//funcion resize imagen func (u Util) ImgRZ(s string) { //cargamos archivo de imageb jpg file, err := os.Open("static/images/fotos_ori/" + s) if err != nil { log.Fatal("en este 1", err) } // decode jpeg image.Image img, err := jpeg.Decode(file) if err != nil { log.Fatal("en este 2", err) } file.Close() // resize to width 1000 using Lanczos resampling // and preserve aspect ratio m := resize.Resize(216, 162, img, resize.Lanczos3) out, err := os.Create("static/images/fotos_rz/" + s) if err != nil { log.Fatal(err) } defer out.Close() // write new image to file jpeg.Encode(out, m, nil) return }
func (land Bounds) DoJob(backchan chan bool) { file, err := os.Open(land.filename) if err != nil { panic(err.String()) } defer file.Close() img, err := jpeg.Decode(file) if err != nil { panic(err.String()) } rgb := stripColors(img) land.img = rgb land.ExtractData() land.SaveData() /*f, err := os.Create(land.filename + "_stripped.png") if err != nil { panic(err.String()) } defer f.Close() err = png.Encode(f, land.img) if err != nil { panic(err.String()) } */ fmt.Printf("%s: got %d entries\n", land.outname, len(land.data)) backchan <- true }
func main() { if len(os.Args) != 2 { fmt.Println("Please select one picture to makeascii...") return } fileName := os.Args[1] isJpeg, _ := regexp.MatchString(".+\\.jpg", fileName) isPng, _ := regexp.MatchString(".+\\.png", fileName) var picture image.Image var imageErr error fileIn, errIn := os.Open(fileName) if errIn != nil { fmt.Println(errIn.Error()) return } if isJpeg { picture, imageErr = jpeg.Decode(fileIn) } else if isPng { picture, imageErr = png.Decode(fileIn) } else { fmt.Println("File type is not supported...") return } if imageErr != nil { fmt.Println(imageErr.Error()) return } fmt.Print(MakeAscii(GetImage(picture))) }
func readImageJpeg(filename string) (image []Spectrum, width, height int) { f, err := os.Open(filename) defer f.Close() if err != nil { Error("Error reading JPEG \"%s\"", filename) return nil, 0, 0 } jpegImage, err := jpeg.Decode(f) if err != nil { Error("Error decoding JPEG \"%s\"", filename) return nil, 0, 0 } bounds := jpegImage.Bounds() width = bounds.Dx() height = bounds.Dy() image = make([]Spectrum, width*height, width*height) for y := 0; y < height; y++ { for x := 0; x < width; x++ { rb, gb, bb, _ := jpegImage.At(x, y).RGBA() r := float64(rb) / float64(0xffff) g := float64(gb) / float64(0xffff) b := float64(bb) / float64(0xffff) image[y*width+x] = *NewSpectrumRGB(r, g, b) } } return image, width, height }
func readImage(data []byte, format string) (image.Image, error) { reader := bytes.NewReader(data) if format == "png" { return png.Decode(reader) } return jpeg.Decode(reader) }
//截图生成文件 func resizeImage2file(src_img, dest_img string) { file, err := os.Open(src_img) if err != nil { fmt.Println(err) return } // decode jpeg into image.Image img, err := jpeg.Decode(file) if err != nil { fmt.Println(err) return } file.Close() m := resize.Resize(0, 1000, img, resize.Lanczos2) out, err := os.Create(dest_img) if err != nil { fmt.Println(err) return } defer out.Close() err = jpeg.Encode(out, m, nil) if err != nil { fmt.Println(err) return } }
func main() { // open "1.jpg" file, err := os.Open("3.jpg") if err != nil { log.Fatal(err) } // decode jpeg into image.Image img, err := jpeg.Decode(file) if err != nil { log.Fatal(err) } file.Close() // resize to width 1000 using Lanczos resampling // and preserve aspect ratio m := resize.Resize(1, 0, img, resize.NearestNeighbor) r, g, b, _ := m.At(0, 0).RGBA() fmt.Printf("%X%X%X", r>>8, g>>8, b>>8) // removing 1 byte 9A16->9A out, err := os.Create("test_resized3.jpg") if err != nil { log.Fatal(err) } defer out.Close() // write new image to file jpeg.Encode(out, m, nil) }
func makeThumbnail(iname string) { file, err := os.Open("photos/" + iname) checkErr(err) defer file.Close() img, err := jpeg.Decode(file) checkErr(err) scale := 16 width, height := img.Bounds().Dx()/scale, img.Bounds().Dy()/scale fimg := image.NewRGBA(image.Rect(0, 0, width, height)) for i := 0; i < width; i++ { for j := 0; j < width; j++ { fimg.Set(i, j, img.At(i*scale, j*scale)) } } outf, err := os.Create("thumbnails/thumbnail_" + strings.Split(iname, ".")[0] + ".png") checkErr(err) defer outf.Close() err = png.Encode(outf, fimg) checkErr(err) }
// default image format: jpeg func (this *Avatar) Encode(wr io.Writer, size int) (err error) { var img image.Image decodeImageFile := func(file string) (img image.Image, err error) { fd, err := os.Open(file) if err != nil { return } defer fd.Close() if img, err = jpeg.Decode(fd); err != nil { fd.Seek(0, os.SEEK_SET) img, err = png.Decode(fd) } return } imgPath := this.imagePath if !this.HasCache() { if this.AlterImage == "" { return errors.New("request image failed, and no alt image offered") } imgPath = this.AlterImage } if img, err = decodeImageFile(imgPath); err != nil { return } m := resize.Resize(uint(size), 0, img, resize.NearestNeighbor) return jpeg.Encode(wr, m, nil) }
func main() { f, err := os.Open("../../images/78771293.jpg") if err != nil { log.Fatal(err) } defer f.Close() img, err := jpeg.Decode(f) if err != nil { log.Fatal(err) } r, g, b, a := img.At(0, 0).RGBA() fmt.Printf("%T %T %T %T \n", r, g, b, a) // uint32 uint32 uint32 uint32 fmt.Printf("%v %v %v %v \n", r, g, b, a) // 30884 27783 21662 65535 fmt.Printf("%d %d %d %d \n", r, g, b, a) // 30884 27783 21662 65535 fmt.Printf("%b %b %b %b \n", r, g, b, a) // 111100010100100 110110010000111 101010010011110 1111111111111111 fmt.Printf("%x %x %x %x \n", r, g, b, a) // 78a4 6c87 549e ffff fi, _ := f.Stat() fmt.Println(fi.Name()) fmt.Println(fi.Size()) fmt.Println(img.Bounds().Dx(), img.Bounds().Dy()) }