目标:

为了做图片水印,需要水平拼接多个logo和文字。。。

之前用过imagick,所以继续使用。

第一个版本:实现了图片和文字的拼接,代码如下:

package main

import (
"fmt" "gopkg.in/gographics/imagick.v2/imagick"
) func draw_setfont(mw *imagick.MagickWand,
dw *imagick.DrawingWand,
font string, size float64,
colour string, sx *float64) {
sflag := false if len(font) > {
dw.SetFont(font)
sflag = true
} if len(colour) > {
pw := imagick.NewPixelWand()
pw.SetColor(colour)
dw.SetFillColor(pw)
pw.Destroy()
sflag = true
} if size > {
dw.SetFontSize(size)
} if sflag {
fm := mw.QueryFontMetrics(dw, " ")
*sx = fm.TextWidth
}
} // sx is the width of a space in the current font and fontsize.
// If the font or fontsize is changed a new value for the space
// width must be obtained before calling this again (by calling draw_setfont)
func draw_metrics(mw *imagick.MagickWand, dw *imagick.DrawingWand, dx *float64, dy, sx float64, text string) {
mw.AnnotateImage(dw, *dx, dy, , text)
mw.DrawImage(dw) // get the font metrics
fm := mw.QueryFontMetrics(dw, text)
if fm != nil {
// Adjust the new x coordinate
*dx += fm.TextWidth + sx
}
} func main() {
imagick.Initialize()
defer imagick.Terminate() mw := imagick.NewMagickWand()
ll := imagick.NewMagickWand()
nn := imagick.NewMagickWand()
bb := imagick.NewMagickWand() fmt.Println("read logo...")
ll.ReadImage("../logo3.png")
nn.ReadImage("../logo2.png")
//加上文字
bb.SetSize(, )
bb.ReadImage("xc:none")
dw := imagick.NewDrawingWand()
var dx, dy, sx float64
dx = //set y
dw.SetFontSize()
dw.SetFont("Times-New-Roman")
fm := bb.QueryFontMetrics(dw, "M")
dy = fm.CharacterHeight + fm.Descender + dw.SetTextEncoding("UTF-8")
dw.SetFont("../yahei.ttf")
fmt.Println("start draw.......")
draw_setfont(bb, dw, "", , "#40FF80", &sx)
fmt.Println("curr:", sx, "dy:", dy)
draw_metrics(bb, dw, &dx, dy, sx, "like......你好。。。")
bb.DrawImage(dw)
bb.WriteImage("font.png") mw.AddImage(ll)
mw.AddImage(nn)
mw.AddImage(bb)

total := mw.MontageImage(ndw, "3x1", "", 0, "0")

total.WriteImage("append.png")
}

实现效果:图片和文字,都已经拼接到一行,拼接方式由 MontageImage 第二个参数决定(Nx1,表示都在一行。。。)

但是有个问题,原先透明的logo图片,拼接到一起,居然没有透明效果了。。。。。

各种查资料,然后第二版来了

package main

import (
"fmt" "gopkg.in/gographics/imagick.v2/imagick"
) func draw_setfont(mw *imagick.MagickWand,
dw *imagick.DrawingWand,
font string, size float64,
colour string, sx *float64) {
sflag := false if len(font) > {
dw.SetFont(font)
sflag = true
} if len(colour) > {
pw := imagick.NewPixelWand()
pw.SetColor(colour)
dw.SetFillColor(pw)
pw.Destroy()
sflag = true
} if size > {
dw.SetFontSize(size)
} if sflag {
fm := mw.QueryFontMetrics(dw, " ")
*sx = fm.TextWidth
}
} // sx is the width of a space in the current font and fontsize.
// If the font or fontsize is changed a new value for the space
// width must be obtained before calling this again (by calling draw_setfont) func draw_metrics(mw *imagick.MagickWand, dw *imagick.DrawingWand, dx *float64, dy, sx float64, text string) {
mw.AnnotateImage(dw, *dx, dy, , text)
mw.DrawImage(dw) // get the font metrics
fm := mw.QueryFontMetrics(dw, text)
if fm != nil {
// Adjust the new x coordinate
*dx += fm.TextWidth + sx
}
} func main() {
imagick.Initialize()
defer imagick.Terminate() mw := imagick.NewMagickWand()
ll := imagick.NewMagickWand()
nn := imagick.NewMagickWand()
bb := imagick.NewMagickWand() fmt.Println("read logo...")
ll.ReadImage("../logo3.png")
nn.ReadImage("../logo2.png") //加上文字
bb.SetSize(, )
bb.ReadImage("xc:none")
dw := imagick.NewDrawingWand()
var dx, dy, sx float64
dx = //set y
dw.SetFontSize()
dw.SetFont("Times-New-Roman")
fm := bb.QueryFontMetrics(dw, "M")
dy = fm.CharacterHeight + fm.Descender + dw.SetTextEncoding("UTF-8")
dw.SetFont("../yahei.ttf")
fmt.Println("start draw.......")
draw_setfont(bb, dw, "", , "#40FF80", &sx)
fmt.Println("curr:", sx, "dy:", dy)
draw_metrics(bb, dw, &dx, dy, sx, "like......你好。。。")
bb.DrawImage(dw) mw.AddImage(ll)
mw.AddImage(nn)
mw.AddImage(bb) mw.ResetIterator()
append_img :
= mw.AppendImages(false)
append_img.WriteImage("append.png")
}

标红的两行代码是关键,如果不调用 ResetIterator,则拼接的终效果是最后一张图片,这里是让迭代器指向第一张图片,重新开始。

AppendImages的参数,true或者false表示上下排列(true),还是水平排列(false)。

OK,到这里,就可以完成目标了。

不得不说,imagick很强大,但是API研究起来还是很头疼呀。

另外,如果你想把一张有背景的图片的 背景去掉,变成透明,那么可以用下面的方法。

// Port of http://members.shaw.ca/el.supremo/MagickWand/trans_paint.htm to Go
package main import "gopkg.in/gographics/imagick.v2/imagick" func main() {
imagick.Initialize()
defer imagick.Terminate() mw := imagick.NewMagickWand()
mw.ReadImage("logo:") // A larger fuzz value allows more colours "near" white to be
// modified. A fuzz of zero only allows an exact match with the
// given colour
// Set up the pixelwand containing the colour to be "targeted"
// by transparency
target := imagick.NewPixelWand()
target.SetColor("white")
// Change the transparency of all colours which match target (with
// fuzz applied). In this case they are made completely transparent (0)
// but you can set this to any value from 0 to 1.
mw.TransparentPaintImage(target, 0, 10, false)
mw.WriteImage("logo_white.png")
}

但是这个方法也不是万能的,SetColor用来指定需要去处的背景色,程序会查找相似的颜色,并去掉,然后编程透明。

关于imagic拼接透明背景图片的问题的更多相关文章

  1. 网页中PNG透明背景图片的完美应用

    PNG 图片在网站设计中是不可或缺的部分,最大的特点应该在于 PNG 可以无损压缩,而且还可以设置透明,对于增强网站的图片色彩效果有重要的作用. 但为什么 PNG 图片却没有 GIF 和 JPG 图片 ...

  2. Qt 制作透明背景图片与裁剪图片(很实用)

    这两天想做一个五子棋游戏,想从零开始自己绘制各种图片素材,将经验心得整理如下. 制作透明背景图片: void MyPainter::DrawKit() { QImage image(30, 30, Q ...

  3. vc下打印透明背景图片

    一.前言 刚接到个任务,要把带有透明背景的章子图片打印出来,开始觉得不是很简单吗,直接用vc自动生成的打印功能不就ok了.不过问题却不是想像的那么简单! 二.窗口中显示透明图片 在窗口中显示图片,可以 ...

  4. 【计算机视觉】OPENCV对于有alpha通道的透明背景图片的读取和图片叠加

    这个是我自己做的粗略的螺旋丸的图,导出为png并带有alpha通道. 最后和一只狗合成成这个样子. 效果还是可以的. 为了实现这个效果,首先我们要明白具有透明通道的图片的OpenCV的读取方式.在Op ...

  5. C++ 设置透明背景图片

    背景:            有两个图片,一个是目标背景图片, 一个是带有自身背景色彩的彩色图片            先将这彩色图片绘制到目标背景图片中, 这一步通过BITBLT就可实现.   但实 ...

  6. java 生成透明背景图片

    //开始绘图 graphics2d.setBackground(Color.WHITE); graphics2d.clearRect(0, 0, width, height); graphics2d. ...

  7. php 处理透明背景的图片时的问题

    PHP图象处理之透明背景的gif和png图片的一些问题 1,直接读取有透明背景的PNG格式文件,然后直接输出,背景变成了黑色,gif则没有这种情况.   解决方法:使用 imagesavealpha ...

  8. css透明背景兼容方案

    css透明是一个技术活,因为这里面涉及到了IE8及以下版本不兼容opacity这个css3属性,而filter:alpha(opacity=80)这个值则是不兼容IE6.网上比较流行的透明css是这样 ...

  9. DD_belatedPNG.js解决透明PNG图片背景灰色问题

    <!--[]> <script type="text/javascript" src="http://www.phpddt.com/usr/themes ...

随机推荐

  1. django 快速实现完整登录系统

    django 实现完整登录系统 本操作的环境: =================== Windows 7 64 python3.5 Django 1.10 =================== 创 ...

  2. css内容样式属性

    设置元素的最大高度.最小高度.最大宽度.最小宽度,用max-height.min-height.max-width.min-width. visibility:设置元素是否可见.visible和hid ...

  3. 如何判断js中的数据类型

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  4. a different object with the same identifier value was already associated with the session:错误;

    当出现a different object with the same identifier value was already associated with thesession时,一般是因为在h ...

  5. isMemberOfClass和isKindOfClass的区别

    1.isMemberOfClass:作用:用于判断一个对象是否属于当前这个类   Person *rose = [[Person alloc] init];          if ([rose is ...

  6. CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files 解决方案

    1 设置c:windows\temp 目录访问权限 temp--> 属性-->安全-- > 添加network service -->并赋予其权限为 读 和 写--> 确 ...

  7. VMWare MAC系统调整磁盘

    VMware,打开虚拟文件,调整磁盘大小. 从40G调整到80G 进入虚拟机,打开终端 diskutil list;diskutil resizeVolume disk0s2 80GB http:// ...

  8. Nginx启动报错: could not open error log file: open() &q

    启动nginx报如下错误: nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error ...

  9. IIS 中文文件名下载会出现403访问被拒绝

    IIS 中文文件名下载会出现403访问被拒绝 服务器在安全加固后,出现了IIS 中文文件名下载会出现403访问被拒绝 换成英文的就好了

  10. Error: Cannot open main configuration file '//start' for reading! 解决办法

    当执行service nagios start启动nagios时,报错:Error: Cannot open main configuration file '//start' for reading ...