forked from gographics/imagick
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
166 lines (140 loc) · 4.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Port of http://members.shaw.ca/el.supremo/MagickWand/gel.htm to Go
package main
import (
"gopkg.in/gographics/imagick.v3/imagick"
)
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()
}