func main() { flag.Parse() if len(*destination) > 0 { err := os.MkdirAll(*destination, os.ModePerm) if err != nil { fmt.Printf("Unable to create destination: %s\n", err) os.Exit(1) } } extMatch := regexp.MustCompile(`\.[a-zA-Z0-9]+$`) filelist := flag.Args() // if len(filelist) == 0 { // filelist = []string{"./*.*"} // } for _, srcArg := range filelist { srcFilenames, err := filepath.Glob(srcArg) if err != nil { log.Printf("Glob error: %s\n", err) continue } for _, srcFilename := range srcFilenames { fmt.Printf("Processing [%s]... ", srcFilename) src, err := imaging.Open(srcFilename) if err != nil { log.Printf("error: %s\n", err) continue } dest := imaging.Grayscale(src) extension := *output if extension == "" { extension = strings.TrimLeft(filepath.Ext(srcFilename), ".") } destFilename := extMatch.ReplaceAllString(srcFilename, "") + fmt.Sprintf("-grayscale.%s", extension) if len(*destination) > 0 { _, file := filepath.Split(destFilename) destFilename = filepath.Join(*destination, file) } err = imaging.Save(dest, destFilename) if err != nil { log.Printf("error: %s\n", err) continue } fmt.Printf("OK; written to %s\n", destFilename) } } }
// GetHash returns a phash string for a JPEG image func GetHash(reader io.Reader) (string, error) { image, err := imaging.Decode(reader) if err != nil { return "", err } image = imaging.Resize(image, 32, 32, imaging.Lanczos) image = imaging.Grayscale(image) imageMatrixData := getImageMatrix(image) dctMatrix := getDCTMatrix(imageMatrixData) smallDctMatrix := reduceMatrix(dctMatrix, 8) dctMeanValue := calculateMeanValue(smallDctMatrix) return buildHash(smallDctMatrix, dctMeanValue), nil }
func cancellationAntiAliasing(img *image.RGBA) *image.NRGBA { if debug { fmt.Println("cancellationAntiAliasing") } gray := imaging.Grayscale(img) //imaging.Save(gray, "./grayscaled.png") w := gray.Rect.Max.X h := gray.Rect.Max.Y for y := 0; y < h; y++ { for x := 0; x < w; x++ { r, _, _, _ := gray.At(x, y).RGBA() if r > 38000 { c := color.RGBA{0xff, 0xff, 0xff, 0xff} gray.Set(x, y, c) } else { c := color.RGBA{0x00, 0x00, 0x00, 0xff} gray.Set(x, y, c) } } } return gray }
// Provides image://cover/ func (c *Comics) CoverProvider(file string, width int, height int) image.Image { c.Conv.Opts = c.GetOptions() stat, err := os.Stat(file) if err != nil { fmt.Fprintf(os.Stderr, "Error Stat CoverProvider: %v\n", err.Error()) return image.NewRGBA(image.Rect(0, 0, width, height)) } cover, err := c.Conv.GetCoverImage(file, stat) if err != nil { fmt.Fprintf(os.Stderr, "Error GetCoverImage: %v\n", err.Error()) return image.NewRGBA(image.Rect(0, 0, width, height)) } cover = c.Conv.TransformImage(cover) if c.Conv.Opts.LevelsInMin != 0 || c.Conv.Opts.LevelsInMax != 255 || c.Conv.Opts.LevelsGamma != 1.00 || c.Conv.Opts.LevelsOutMin != 0 || c.Conv.Opts.LevelsOutMax != 255 { cover = c.Conv.LevelImage(cover) } // imaging is used for preview only if c.Conv.Opts.Grayscale { cover = imaging.Grayscale(cover) } // size preview s := 0 b := new(bytes.Buffer) w := 0 h := 0 switch c.Conv.Opts.Format { case "jpeg": jpeg.Encode(b, cover, &jpeg.Options{c.Conv.Opts.Quality}) s = len(b.Bytes()) cover, _ = jpeg.Decode(bytes.NewReader(b.Bytes())) config, _, _ := image.DecodeConfig(bytes.NewReader(b.Bytes())) w = config.Width h = config.Height case "png": png.Encode(b, cover) s = len(b.Bytes()) cover, _ = png.Decode(bytes.NewReader(b.Bytes())) config, _, _ := image.DecodeConfig(bytes.NewReader(b.Bytes())) w = config.Width h = config.Height case "gif": mw := imagick.NewMagickWand() defer mw.Destroy() mw.ReadImageBlob(c.Conv.GetImageBytes(cover)) mw.SetImageFormat("GIF") blob := mw.GetImageBlob() s = len(blob) cover, _ = gif.Decode(bytes.NewReader(blob)) config, _, _ := image.DecodeConfig(bytes.NewReader(blob)) w = config.Width h = config.Height case "tiff": tiff.Encode(b, cover, &tiff.Options{tiff.Uncompressed, false}) var buf bytes.Buffer gz := gzip.NewWriter(&buf) gz.Write(b.Bytes()) gz.Close() s = buf.Len() cover, _ = tiff.Decode(bytes.NewReader(b.Bytes())) config, _, _ := image.DecodeConfig(bytes.NewReader(b.Bytes())) w = config.Width h = config.Height case "bmp": mw := imagick.NewMagickWand() defer mw.Destroy() bb := c.Conv.GetImageBytes(cover) mw.ReadImageBlob(bb) wand := imagick.NewPixelWand() wand.SetColor("black") defer wand.Destroy() mw.SetImageFormat("BMP3") mw.SetImageBackgroundColor(wand) mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_REMOVE) mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_DEACTIVATE) mw.SetImageMatte(false) mw.SetImageCompression(imagick.COMPRESSION_NO) mw.QuantizeImage(16, mw.GetImageColorspace(), 8, true, true) var buf bytes.Buffer blob := mw.GetImageBlob() gz := gzip.NewWriter(&buf) gz.Write(blob) gz.Close() s = buf.Len() cover, _ = bmp.Decode(bytes.NewReader(blob)) config, _, _ := image.DecodeConfig(bytes.NewReader(bb)) w = config.Width h = config.Height } if cover == nil { return image.NewRGBA(image.Rect(0, 0, width, height)) } human := humanize.IBytes(uint64(s)) c.Root.ObjectByName("sizePreview").Set("text", fmt.Sprintf("%s (%dx%d)", human, w, h)) return cover }