目标:

为了做图片水印,需要水平拼接多个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. The PHP Package 之 monolog[转]

    Monolog 发送你的日志到文件.到sockets.到邮箱.到数据库或(和)者其他网路存储服务(云).Monolog可以做到同时保存到一个或多个存储介质(后面的栈冒泡处理). 安装   $ comp ...

  2. C#中如何定义全局变量及在各窗体中使用全局变量

    using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; us ...

  3. dede文章调用时过滤调 body里面的style属性和值

    dede 发布文章的时候会在里面的标签中添加一些style 属性,现在改网站想去掉这些属性和里面的值,因为文章太多所以就用下面的方法 \include\arc.listview.class.php 在 ...

  4. About Mysql 5.7 Installation

    After version of mysql 5.7, mysql increase its security level. CMD run as adminstratot cd c:/mysql/b ...

  5. 关于int类型的赋值语句正确的有

    A.char a=65;          对 B.int a=12.0; C.int a=12.0f; D.int a=(int)12.0     对

  6. Postgresql-xl 调研

    Postgresql-xl 调研 来历 这个项目的背后是一家叫做stormDB的公司.整个代买基于postgres-xc.开源版本应该是stormdb的一个分支. In 2010, NTT's Ope ...

  7. Sublim Text3快捷键大全

    Ctrl+Shift+P:打开命令面板Ctrl+P:搜索项目中的文件Ctrl+G:跳转到第几行Ctrl+W:关闭当前打开文件Ctrl+Shift+W:关闭所有打开文件Ctrl+Shift+V:粘贴并格 ...

  8. How-to: Enable User Authentication and Authorization in Apache HBase

    With the default Apache HBase configuration, everyone is allowed to read from and write to all table ...

  9. java修改图片大小

    import java.awt.Image; import java.awt.image.BufferedImage; import java.io.BufferedInputStream; impo ...

  10. C++ 内存的分配方式 (摘选自网络)

    在c++中有三种分配内存的方式: 在许多大大公司在面试的时候很可能考到的内容.在这里和大家分享了!1. 静态存储区,是在程序编译时就已经分配好的,在整个运行期间都存在,如全局变量.常量.2. 栈上分配 ...