Ejemplo n.º 1
0
// Applies a Photoshop-like levels operation on an image
func (c *Convertor) LevelImage(img image.Image) image.Image {
	imagick.Initialize()

	mw := imagick.NewMagickWand()
	defer mw.Destroy()

	err := mw.ReadImageBlob(c.GetImageBytes(img))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error ReadImageBlob: %v\n", err.Error())
		return img
	}

	_, qrange := imagick.GetQuantumRange()
	quantumRange := float64(qrange)

	inmin := (quantumRange * c.Opts.LevelsInMin) / 255
	inmax := (quantumRange * c.Opts.LevelsInMax) / 255
	outmin := (quantumRange * c.Opts.LevelsOutMin) / 255
	outmax := (quantumRange * c.Opts.LevelsOutMax) / 255

	err = mw.LevelImage(inmin, c.Opts.LevelsGamma, inmax)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error LevelImageChannel Input: %v\n", err.Error())
		return img
	}

	err = mw.LevelImage(-outmin, 1.0, quantumRange+(quantumRange-outmax))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error LevelImageChannel Output: %v\n", err.Error())
		return img
	}

	blob := mw.GetImageBlob()
	i, err := c.decodeImage(bytes.NewReader(blob), "levels")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error decodeImage: %v\n", err.Error())
		return img
	}

	return i
}
Ejemplo n.º 2
0
func main() {
	// Note that the colours are stored as separate *normalized* RGB components
	arglist := []float64{
		0, 0,
		// RGB black
		0, 0, 0,
		// The y coordinate is filled in later
		0, -1,
		// RGB white
		1, 1, 1,
	}
	// arguments for MagickFunctionImage
	funclist := []float64{4, -4, 1}
	imagick.Initialize()
	defer imagick.Terminate()
	mw := imagick.NewMagickWand()
	defer mw.Destroy()
	if err := mw.ReadImage("beijing_md.jpg"); err != nil {
		panic(err)
	}
	// fill in the Y coordinate now that we can get the image dimensions
	arglist[6] = float64(mw.GetImageHeight() - 1)
	_, quant := imagick.GetQuantumRange()
	mw.SigmoidalContrastImage(true, 15, float64(quant)*30/100)
	cw := mw.Clone()
	defer cw.Destroy()
	cw.SparseColorImage(imagick.CHANNELS_RGB, imagick.INTERPOLATE_BARYCENTRIC_COLOR, arglist)
	// Do the polynomial function
	cw.FunctionImage(imagick.FUNCTION_POLYNOMIAL, funclist)
	// -set option:compose:args 15
	if err := cw.SetImageArtifact("compose:args", "15"); err != nil {
		panic(err)
	}
	mw.CompositeImage(cw, imagick.COMPOSITE_OP_BLUR, 0, 0)
	mw.WriteImage("beijing_model.jpg")
}
Ejemplo n.º 3
0
func main() {
	imagick.Initialize()
	defer imagick.Terminate()

	/* First step is to create the gel shape:
	convert -size 100x60 xc:none \
	          -fill red -draw 'circle    25,30  10,30' \
	                    -draw 'circle    75,30  90,30' \
	                    -draw 'rectangle 25,15  75,45' \
	          gel_shape.png
	*/

	/* Create a wand */
	mw := imagick.NewMagickWand()
	pw := imagick.NewPixelWand()
	dw := imagick.NewDrawingWand()

	mw.SetSize(100, 60)
	mw.ReadImage("xc:none")

	pw.SetColor("red")
	dw.SetFillColor(pw)
	dw.Circle(25, 30, 10, 30)
	dw.Circle(75, 30, 90, 30)
	dw.Rectangle(25, 15, 75, 45)

	// Now we draw the Drawing wand on to the Magick Wand
	mw.DrawImage(dw)
	mw.WriteImage("gel_shape.png")

	dw.Destroy()
	pw.Destroy()
	mw.Destroy()

	/* Next step is to create the gel highlight:
	convert gel_shape.png \
	            \( +clone -fx A +matte  -blur 0x12  -shade 110x0 -normalize \
	               -sigmoidal-contrast 16,60% -evaluate multiply .5 \
	               -roll +5+10 +clone -compose Screen -composite \) \
	            -compose In  -composite  gel_highlight.png
	*/

	mw = imagick.NewMagickWand()
	mw.ReadImage("gel_shape.png")

	mwc := mw.Clone()
	mwf, err := mwc.FxImage("A")
	if err != nil {
		panic(err)
	}

	mwf.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_DEACTIVATE)
	mwf.BlurImage(0, 12)
	mwf.ShadeImage(true, 110, 0)
	mwf.NormalizeImage()

	// The last argument is specified as a percentage on the command line
	// but is specified to the function as a percentage of the QuantumRange
	_, quant := imagick.GetQuantumRange()
	mwf.SigmoidalContrastImage(true, 16, 0.6*float64(quant))
	mwf.EvaluateImage(imagick.EVAL_OP_MULTIPLY, 0.5)
	mwf.RollImage(5, 10)

	mwc.Destroy()
	// The +clone operation copies the original but only so that
	// it can be used in the following composite operation, so we don't
	// actually need to do a clone, just reference the original image.
	mwf.CompositeImage(mw, imagick.COMPOSITE_OP_SCREEN, 0, 0)

	mw.CompositeImage(mwf, imagick.COMPOSITE_OP_IN, 0, 0)
	mw.WriteImage("gel_highlight.png")

	mw.Destroy()
	mwc.Destroy()
	mwf.Destroy()

	// Now create the gel border
	/*
		convert gel_highlight.png \
		            \( +clone -fx A  +matte -blur 0x2  -shade 0x90 -normalize \
		               -blur 0x2  -negate -evaluate multiply .4 -negate -roll -.5-1 \
		               +clone  -compose Multiply -composite \) \
		            -compose In  -composite  gel_border.png

	*/

	mw = imagick.NewMagickWand()
	mw.ReadImage("gel_highlight.png")
	mwc = mw.Clone()
	mwf, err = mwc.FxImage("A")
	if err != nil {
		panic(err)
	}
	mwf.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_DEACTIVATE)
	mwf.BlurImage(0, 2)
	mwf.ShadeImage(true, 0, 90)
	mwf.NormalizeImage()
	mwf.BlurImage(0, 2)
	mwf.NegateImage(false)
	mwf.EvaluateImage(imagick.EVAL_OP_MULTIPLY, 0.4)
	mwf.NegateImage(false)
	mwf.RollImage(-1, -1)
	mwf.CompositeImage(mw, imagick.COMPOSITE_OP_MULTIPLY, 0, 0)
	mw.CompositeImage(mwf, imagick.COMPOSITE_OP_IN, 0, 0)
	mw.WriteImage("gel_border.png")

	mw.Destroy()
	mwc.Destroy()
	mwf.Destroy()

	// and finally the text and shadow effect
	/*
		convert gel_border.png \
		            -font Candice  -pointsize 24  -fill white  -stroke black \
		            -gravity Center  -annotate 0 "Gel"  -trim -repage 0x0+4+4 \
		            \( +clone -background navy -shadow 80x4+4+4 \) +swap \
		            -background none  -flatten    gel_button.png
	*/

	mw = imagick.NewMagickWand()
	dw = imagick.NewDrawingWand()
	pw = imagick.NewPixelWand()

	mw.ReadImage("gel_border.png")
	dw.SetFont("Lucida-Handwriting-Italic")
	dw.SetFontSize(24)
	pw.SetColor("white")
	dw.SetFillColor(pw)
	pw.SetColor("black")
	dw.SetStrokeColor(pw)
	dw.SetGravity(imagick.GRAVITY_CENTER)
	// It is important to notice here that MagickAnnotateImage renders the text on
	// to the MagickWand, NOT the DrawingWand. It only uses the DrawingWand for font
	// and colour information etc.
	mw.AnnotateImage(dw, 0, 0, 0, "Gel")
	mw.TrimImage(0)
	mw.ResetImagePage("0x0+4+4")
	mwc = mw.Clone()
	pw.SetColor("navy")
	mwc.SetImageBackgroundColor(pw)
	mwc.ShadowImage(80, 4, 4, 4)
	mwf = imagick.NewMagickWand()
	mwf.AddImage(mwc)
	mwf.AddImage(mw)

	mw.Destroy()
	mwc.Destroy()

	pw.SetColor("none")
	mwf.SetImageBackgroundColor(pw)
	mw = mwf.MergeImageLayers(imagick.IMAGE_LAYER_FLATTEN)
	mw.WriteImage("gel_button.png")

	mw.Destroy()
	mwc.Destroy()
	mwf.Destroy()
	dw.Destroy()
	pw.Destroy()
}