Пример #1
0
func Convert(imagePath, replaceText string, width float64) (string, error) {
	var buffer string

	imtext := NewImageText(replaceText)

	imagick.Initialize()
	defer imagick.Terminate()

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

	err := originalImage.ReadImage(imagePath)
	if err != nil {
		return "", fmt.Errorf("invalid parameter not image file: %s", imagePath)
	}

	originWidth := float64(originalImage.GetImageWidth())
	originHeight := float64(originalImage.GetImageHeight())

	height := math.Floor(width / originWidth * originHeight)

	// resize
	if err := originalImage.ScaleImage(uint(width), uint(height)); err != nil {
		return "", fmt.Errorf("invalid parameter width: %s", width)
	}

	iterator := originalImage.NewPixelIterator()
	defer iterator.Destroy()

	for y := 0; y < int(height); y++ {

		pixels := iterator.GetNextIteratorRow()
		if len(pixels) == 0 {
			break
		}

		for _, pixel := range pixels {
			if !pixel.IsVerified() {
				return "", fmt.Errorf("convert error: %s", "unverified pixel")
			}
			r := (uint8)(255 * pixel.GetRed())
			g := (uint8)(255 * pixel.GetGreen())
			b := (uint8)(255 * pixel.GetBlue())
			short := RGBToShort(r, g, b)

			if replaceText == "" {
				buffer += fmt.Sprintf("\x1b[48;05;%sm  \x1b[0m", short)
			} else {
				buffer += fmt.Sprintf("\x1b[38;05;%sm%s\x1b[0m", short, imtext.GetText())
			}
		}
		buffer += "\n"

		if err := iterator.SyncIterator(); err != nil {
			return "", fmt.Errorf("convert error: %s", err)
		}
	}

	return buffer, nil
}
Пример #2
0
// adding text to image copied from example
func (cl *cell) imgtext(width, height int) *imagick.MagickWand {
	mw := imagick.NewMagickWand()
	//defer mw.Destroy()
	dw := imagick.NewDrawingWand()
	defer dw.Destroy()
	pw := imagick.NewPixelWand()
	defer pw.Destroy()
	pw.SetColor("none")

	// Create a new transparent image
	mw.NewImage(uint(width), uint(height), pw)

	// Set up a 72 point white font
	r, g, b, _ := cl.font.color.RGBA()
	pw.SetColor(fmt.Sprintf("rgb(%d,%d,%d)", r, g, b))
	dw.SetFillColor(pw)
	if (cl.font.name != "") || (cl.font.name != "none") {
		dw.SetFont(cl.font.name)
	}
	dw.SetFontSize(cl.font.size)

	otlne := "none"
	// Add a black outline to the text
	r, g, b, _ = cl.font.outlineColor.RGBA()
	if cl.font.outline {
		otlne = fmt.Sprintf("rgb(%d,%d,%d)", r, g, b)
	}

	pw.SetColor(otlne)
	dw.SetStrokeColor(pw)
	dw.SetStrokeWidth(cl.font.outlineSize)

	// Turn antialias on - not sure this makes a difference
	//dw.SetTextAntialias(true)

	// Now draw the text
	dw.Annotation(cl.font.x, cl.font.y, cl.text)

	// Draw the image on to the mw
	mw.DrawImage(dw)

	// equivalent to the command line +repage
	mw.ResetImagePage("")

	// Make a copy of the text image
	cw := mw.Clone()

	// Set the background colour to blue for the shadow
	pw.SetColor("black")
	mw.SetImageBackgroundColor(pw)

	// Opacity is a real number indicating (apparently) percentage
	mw.ShadowImage(70, 4, 5, 5)

	// Composite the text on top of the shadow
	mw.CompositeImage(cw, imagick.COMPOSITE_OP_OVER, 5, 5)
	cw.Destroy()
	return mw
}
Пример #3
0
/*resizeImage() mw fullsize image
newwidth, newheight = size to be resized to
keepSpecSize = return image with exactly the size specified or just the size of the resized image
center = senter the image
*/
func resizeImage(mw *imagick.MagickWand, newWidth, newHeight int, keepSpecSize, center bool) (resmw *imagick.MagickWand) {
	var (
		width, height, origHeight, origWidth int
	)
	origHeight = int(mw.GetImageHeight())
	origWidth = int(mw.GetImageWidth())

	//check if requested size is the same as current size
	if (origHeight != newHeight) || (origWidth != newWidth) {
		// width / height * newheight = newwidth
		if (round((float64(origWidth) / float64(origHeight)) * float64(newHeight))) <= newWidth {
			width = round((float64(origWidth) / float64(origHeight)) * float64(newHeight))
			height = newHeight
		} else {
			// height / width * newwidth = newheight
			height = round((float64(origHeight) / float64(origWidth)) * float64(newWidth))
			width = newWidth
		}
	} else {
		height = newHeight
		width = newWidth
	}

	//new magickwand for resized image
	resmw = imagick.NewMagickWand()

	if !keepSpecSize {
		resmw.NewImage(uint(width), uint(height), imagick.NewPixelWand())
		center = false
	} else {
		//blank image
		resmw.NewImage(uint(newWidth), uint(newHeight), imagick.NewPixelWand())
		if center {
			err = mw.ResizeImage(uint(width), uint(height), imagick.FILTER_LANCZOS, 1)
			if err != nil {
				panic(err)
			}
			//centers image
			resmw.CompositeImage(mw, imagick.COMPOSITE_OP_SRC_OVER, round(float64(newWidth-width)/float64(2)), round(float64(newHeight-height)/float64(2)))
		} else {
			resmw.CompositeImage(mw, imagick.COMPOSITE_OP_SRC_OVER, 0, 0)
		}
	}
	mw.Destroy()
	return resmw

}
Пример #4
0
func (cl *cell) Init() {
	cl.text = "hello this is text\nhaha\nhdsjfklfhaskjd"
	cl.index = -1
	cl.font.color, cl.font.outlineColor = color.RGBA{0, 0, 0, 1}, color.RGBA{1, 1, 1, 1}
	cl.font.name = "none"
	cl.font.outline = false
	cl.font.outlineSize = 1
	cl.font.size = 35
	cl.font.x, cl.font.y = 10, 30

	cl.qmlcell = cellQml.Create(engine.Context())
	cl.qmlimg = qimg.Create(engine.Context())

	//load image
	cl.img = imagick.NewMagickWand()
	cl.img.ReadImage("logo:")

}
Пример #5
0
Файл: image.go Проект: phzfi/RIC
func NewImage() Image {
	return Image{imagick.NewMagickWand()}
}