目标:

为了做图片水印,需要水平拼接多个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. git 查看远程分支、本地分支、删除本地分支【转】

    1 查看远程分支 $ git branch -a * br-2.1.2.2 master remotes/origin/HEAD -> origin/master remotes/origin/ ...

  2. CSS常用样式及示例

    CSS常用样式及示例 一.简介      层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集) ...

  3. Android 6.0权限适配

    targetSdkVersion 23以上,必须适配新的权限模式 安卓6.0及之后,权限分为三类  1.不涉及隐私的正常权限,如innernet2.危险权限 3.特殊权限 system_alert_w ...

  4. 记录js学习之this用法

    一直对Javascript中的this都有一种似是而非的感觉,今天突然感觉豁然开朗,特此记录一下. 咱们先看个栗子:    <!DOCTYPE html><html><h ...

  5. delphi 开发者 linux 实务(转)

    Linux Essentials for Delphi Developers   There is currently no way using Delphi to target Linux. Lon ...

  6. 【java基础学习一】int[]、Integer[]、String[] 排序( 正序、倒叙)、去重

    调用: //重复项有9.5.1.2 int[] ints = new int[]{9,4,7,8,2,5,1,6,2,5,9,1}; arrayIntTest(ints); ///////////// ...

  7. HTTP权威协议笔记-7.集成点:网关、隧道及中继

    .8.1 网关 定义:网关类似与翻译器,它抽象出了一种能够到达资源的方法. 实用:网关可以自动将HTTP流量转换为其他协议,这样使用HTTP协议的一方就不需要了解其他协议,也可实现与其他程序或设备交互 ...

  8. 文法 LL1

    <程序>-><声明列表>|<程序><函数> <声明列表>-><声明>|<声明><声明列表> ...

  9. WAMPP安装后mysql无法启动

    上午 10:23:42 [mysql] This may be due to a blocked port, missing dependencies, 上午 10:23:42 [mysql] imp ...

  10. web测试常用的用例及知识

      1.      Web测试中关于登录的测试... 1 2.      搜索功能测试用例设计... 2 3.      翻页功能测试用例... 3 4.      输入框的测试... 5 5.    ...