Ejemplo n.º 1
0
func MakeThumbnail(source []byte, num int, c chan string) {
	start := time.Now()
	output := fmt.Sprintf("tmp/out_%d.jpg", num)
	log.Printf("Working with %s", output)
	image, err := magick.NewFromBlob(source, "png")
	defer image.Destroy()
	defer runtime.GC()
	if err != nil {
		log.Printf("Error reading from file %s", err.Error())
		os.Exit(1)
	}
	err = image.Resize("100x100")
	if err != nil {
		log.Print("Problem with transforming")
		os.Exit(1)
	}
	err = image.Shadow("#000", 75, 5, 2, 2)
	if err != nil {
		log.Print("Problem with transforming")
		os.Exit(1)
	}
	err = image.FillBackgroundColor("#333")
	if err != nil {
		log.Print("Problem setting background")
		os.Exit(1)
	}
	_, err = image.ToBlob(output)
	if err != nil {
		log.Printf("Error outputing file: %s", err.Error())
		//	os.Exit(1)
	}
	end := time.Now()
	log.Printf("done with %s. took %v", output, end.Sub(start))
	c <- output
}
Ejemplo n.º 2
0
func main() {
	if len(os.Args) != 3 {
		log.Print("Please supply an input and output filename e.g. ./examples foo.jpg bar.jpg")
		os.Exit(1)
	}
	input := os.Args[1]
	output := os.Args[2]
	log.Printf("Reading from file %s, writing to file %s", input, output)

	start := time.Now()

	source, _ := ioutil.ReadFile(input)
	image, err := magick.NewFromBlob(source, "jpg")
	log.Printf("Loading image took %v\n", time.Now().Sub(start))
	start = time.Now()
	if err != nil {
		log.Printf("Error reading from file %s", err.Error())
		os.Exit(1)
	}

	image.Quality(50)
	image.Strip()
	image.Progressive()
	image.SetProperty("jpeg:sampling-factor", "4:4:4")
	log.Print("Transforming")
	log.Printf("size: %d %d", image.Width(), image.Height())
	err = image.Resize("2000x2000!")
	log.Printf("Transforming image took %v\n", time.Now().Sub(start))
	start = time.Now()
	if err != nil {
		log.Print("Problem with transforming")
		os.Exit(1)
	}

	log.Printf("Writing to %s", output)
	err = image.ToFile(output)
	if err != nil {
		log.Printf("Problem with writing %v", err)
		os.Exit(1)
	}
	log.Printf("Writing image took %v\n", time.Now().Sub(start))
	log.Printf("Wrote to %s", output)

	image.Destroy()
}
Ejemplo n.º 3
0
Archivo: reddy.go Proyecto: RobWC/reddy
func (pp *PicturePoacher) FetchSubmissionImages() []ImageData {
	var resizeAll = "256x256"
	var cropAll = ""

	images := make([]ImageData, 0)

	submissions, _ := pp.Session.SubredditSubmissions(pp.Subreddit)

	for sub := range submissions {
		if submissions[sub].Domain == "imgur.com" || submissions[sub].Domain == "i.imgur.com" {
			_, err := url.Parse(submissions[sub].URL)
			imgType := ""
			if err != nil {
				fmt.Println("Unable to parse URL")
			} else {
				resp, err := http.Get(submissions[sub].URL)
				if err != nil {
					fmt.Println("Failed to fetch image")
				} else {
					if resp.StatusCode == 200 {
						//Check file type
						contentType := strings.Split(resp.Header["Content-Type"][0], "/")
						if contentType[0] == "image" {
							data, err := ioutil.ReadAll(resp.Body)
							if err != nil {
								fmt.Println("Unable to read body")
							} else {
								if strings.ToLower(contentType[1]) == "jpg" || strings.ToLower(contentType[1]) == "jpeg" {
									newImage, err := magick.NewFromBlob(data, "jpg")
									if err != nil {
										fmt.Println(err)
									} else {
										imgType = "jpg"
										newImage.Resize(resizeAll)
										newImage.Crop(cropAll)
										newImage.Strip()
										imageBlob, err := newImage.ToBlob("jpg")
										if err != nil {
											fmt.Println(err)
										} else {
											fontBytes, err := ioutil.ReadFile("./FreeMonoBold.ttf")
											font, err := freetype.ParseFont(fontBytes)
											finalImage, err := jpeg.Decode(bytes.NewReader(imageBlob))
											rgba := image.NewRGBA(image.Rect(0, 0, finalImage.Bounds().Max.X, finalImage.Bounds().Max.Y))
											draw.Draw(rgba, rgba.Bounds(), finalImage, image.ZP, draw.Src)
											textContext := freetype.NewContext()
											textContext.SetFontSize(32)
											textContext.SetFont(font)
											textContext.SetClip(finalImage.Bounds())
											textContext.SetDst(rgba)
											textContext.SetSrc(image.White)
											pt := freetype.Pt(32, 32+int(textContext.PointToFix32(8)>>8))
											text := strconv.FormatInt(int64(submissions[sub].Score), 10)
											textContext.DrawString(text, pt)

											if err != nil {

											} else {
												jpeg.Encode(bufio.NewWriter(bytes.NewBuffer([]byte{})), rgba, nil)
												images = append(images, ImageData{Image: rgba, Score: submissions[sub].Score})
											}
										}
									}
								} else if strings.ToLower(contentType[1]) == "png" {
									newImage, err := magick.NewFromBlob(data, "png")
									if err != nil {
										fmt.Println(err)
									} else {
										imgType = "png"
										newImage.Resize(resizeAll)
										newImage.Crop(cropAll)
										newImage.Strip()
										imageBlob, err := newImage.ToBlob("png")
										if err != nil {
											fmt.Println(err)
										} else {
											finalImage, err := png.Decode(bytes.NewReader(imageBlob))
											if err != nil {

											} else {
												images = append(images, ImageData{Image: finalImage, Score: submissions[sub].Score})
											}
										}
									}
								} else if strings.ToLower(contentType[1]) == "gif" {
									newImage, err := magick.NewFromBlob(data, "gif")
									if err != nil {
										fmt.Println(err)
									} else {
										imgType = "gif"
										newImage.Resize(resizeAll)
										newImage.Crop(cropAll)
										newImage.Strip()
										imageBlob, err := newImage.ToBlob("png")
										if err != nil {
											fmt.Println(err)
										} else {
											finalImage, err := gif.Decode(bytes.NewReader(imageBlob))
											if err != nil {

											} else {
												images = append(images, ImageData{Image: finalImage, Score: submissions[sub].Score})
											}
										}
									}
								}
							}
						}

					} else {
						fmt.Println("Failed to fetch image of type", imgType)
					}

				}
			}

		} else {
			fmt.Println("No image for ID", submissions[sub].ID)
		}
	}
	return images
}