/
drawing.py
2131 lines (1742 loc) · 78.2 KB
/
drawing.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
""":mod:`wand.drawing` --- Drawings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The module provides some vector drawing functions.
.. versionadded:: 0.3.0
"""
import collections
import ctypes
import numbers
from . import assertions
from .api import library
from .cdefs.structures import AffineMatrix, PointInfo
from .color import Color
from .compat import abc, binary, string_type, text, text_type, xrange
from .exceptions import WandLibraryVersionError
from .image import COMPOSITE_OPERATORS, BaseImage
from .resource import Resource
from .sequence import SingleImage
__all__ = ('CLIP_PATH_UNITS', 'FILL_RULE_TYPES', 'FONT_METRICS_ATTRIBUTES',
'GRAVITY_TYPES', 'LINE_CAP_TYPES', 'LINE_JOIN_TYPES',
'PAINT_METHOD_TYPES', 'STRETCH_TYPES', 'STYLE_TYPES',
'TEXT_ALIGN_TYPES', 'TEXT_DECORATION_TYPES',
'TEXT_DIRECTION_TYPES', 'Drawing', 'FontMetrics')
#: (:class:`collections.abc.Sequence`) The list of clip path units
#:
#: - ``'undefined_path_units'``
#: - ``'user_space'``
#: - ``'user_space_on_use'``
#: - ``'object_bounding_box'``
CLIP_PATH_UNITS = ('undefined_path_units', 'user_space', 'user_space_on_use',
'object_bounding_box')
#: (:class:`collections.abc.Sequence`) The list of text align types.
#:
#: - ``'undefined'``
#: - ``'left'``
#: - ``'center'``
#: - ``'right'``
TEXT_ALIGN_TYPES = 'undefined', 'left', 'center', 'right'
#: (:class:`collections.abc.Sequence`) The list of text decoration types.
#:
#: - ``'undefined'``
#: - ``'no'``
#: - ``'underline'``
#: - ``'overline'``
#: - ``'line_through'``
TEXT_DECORATION_TYPES = ('undefined', 'no', 'underline', 'overline',
'line_through')
#: (:class:`collections.abc.Sequence`) The list of text direction types.
#:
#: - ``'undefined'``
#: - ``'right_to_left'``
#: - ``'left_to_right'``
TEXT_DIRECTION_TYPES = ('undefined', 'right_to_left', 'left_to_right')
#: (:class:`collections.abc.Sequence`) The list of text gravity types.
#:
#: - ``'forget'``
#: - ``'north_west'``
#: - ``'north'``
#: - ``'north_east'``
#: - ``'west'``
#: - ``'center'``
#: - ``'east'``
#: - ``'south_west'``
#: - ``'south'``
#: - ``'south_east'``
#: - ``'static'``
GRAVITY_TYPES = ('forget', 'north_west', 'north', 'north_east', 'west',
'center', 'east', 'south_west', 'south', 'south_east',
'static')
#: (:class:`collections.abc.Sequence`) The list of fill-rule types.
#:
#: - ``'undefined'``
#: - ``'evenodd'``
#: - ``'nonzero'``
FILL_RULE_TYPES = ('undefined', 'evenodd', 'nonzero')
#: (:class:`collections.abc.Sequence`) The attribute names of font metrics.
FONT_METRICS_ATTRIBUTES = ('character_width', 'character_height', 'ascender',
'descender', 'text_width', 'text_height',
'maximum_horizontal_advance', 'x1', 'y1', 'x2',
'y2', 'x', 'y')
#: (:class:`collections.abc.Sequence`) The list of stretch types for fonts
#:
#: - ``'undefined;``
#: - ``'normal'``
#: - ``'ultra_condensed'``
#: - ``'extra_condensed'``
#: - ``'condensed'``
#: - ``'semi_condensed'``
#: - ``'semi_expanded'``
#: - ``'expanded'``
#: - ``'extra_expanded'``
#: - ``'ultra_expanded'``
#: - ``'any'``
STRETCH_TYPES = ('undefined', 'normal', 'ultra_condensed', 'extra_condensed',
'condensed', 'semi_condensed', 'semi_expanded', 'expanded',
'extra_expanded', 'ultra_expanded', 'any')
#: (:class:`collections.abc.Sequence`) The list of style types for fonts
#:
#: - ``'undefined;``
#: - ``'normal'``
#: - ``'italic'``
#: - ``'oblique'``
#: - ``'any'``
STYLE_TYPES = ('undefined', 'normal', 'italic', 'oblique', 'any')
#: (:class:`collections.abc.Sequence`) The list of LineCap types
#:
#: - ``'undefined;``
#: - ``'butt'``
#: - ``'round'``
#: - ``'square'``
LINE_CAP_TYPES = ('undefined', 'butt', 'round', 'square')
#: (:class:`collections.abc.Sequence`) The list of LineJoin types
#:
#: - ``'undefined'``
#: - ``'miter'``
#: - ``'round'``
#: - ``'bevel'``
LINE_JOIN_TYPES = ('undefined', 'miter', 'round', 'bevel')
#: (:class:`collections.abc.Sequence`) The list of paint method types.
#:
#: - ``'undefined'``
#: - ``'point'``
#: - ``'replace'``
#: - ``'floodfill'``
#: - ``'filltoborder'``
#: - ``'reset'``
PAINT_METHOD_TYPES = ('undefined', 'point', 'replace',
'floodfill', 'filltoborder', 'reset')
class Drawing(Resource):
"""Drawing object. It maintains several vector drawing instructions
and can get drawn into zero or more :class:`~wand.image.Image` objects
by calling it.
For example, the following code draws a diagonal line to the ``image``::
with Drawing() as draw:
draw.line((0, 0), image.size)
draw(image)
:param drawing: an optional drawing object to clone.
use :meth:`clone()` method rather than this parameter
:type drawing: :class:`Drawing`
.. versionadded:: 0.3.0
"""
c_is_resource = library.IsDrawingWand
c_destroy_resource = library.DestroyDrawingWand
c_get_exception = library.DrawGetException
c_clear_exception = library.DrawClearException
def __init__(self, drawing=None):
with self.allocate():
if not drawing:
wand = library.NewDrawingWand()
elif not isinstance(drawing, type(self)):
raise TypeError('drawing must be a wand.drawing.Drawing '
'instance, not ' + repr(drawing))
else:
wand = library.CloneDrawingWand(drawing.resource)
self.resource = wand
@property
def border_color(self):
"""(:class:`~wand.color.Color`) the current border color. It also can
be set. This attribute controls the behavior of
:meth:`~wand.drawing.Drawing.color()` during ``'filltoborder'``
operation.
.. versionadded:: 0.4.0
"""
pixelwand = library.NewPixelWand()
library.DrawGetBorderColor(self.resource, pixelwand)
color = Color.from_pixelwand(pixelwand)
pixelwand = library.DestroyPixelWand(pixelwand)
return color
@border_color.setter
def border_color(self, color):
if isinstance(color, string_type):
color = Color(color)
assertions.assert_color(border_color=color)
with color:
library.DrawSetBorderColor(self.resource, color.resource)
@property
def clip_path(self):
"""(:class:`basestring`) The current clip path. It also can be set.
.. versionadded:: 0.4.0
.. versionchanged: 0.4.1
Safely release allocated memory with
:c:func:`MagickRelinquishMemory` instead of :c:func:`libc.free`.
"""
clip_path_str = None
clip_path_p = library.DrawGetClipPath(self.resource)
if clip_path_p:
clip_path_str = text(ctypes.string_at(clip_path_p))
clip_path_p = library.MagickRelinquishMemory(clip_path_p)
return clip_path_str
@clip_path.setter
def clip_path(self, path):
assertions.assert_string(clip_path=path)
library.DrawSetClipPath(self.resource, binary(path))
@property
def clip_rule(self):
"""(:class:`basestring`) The current clip rule. It also can be set.
It's a string value from :const:`FILL_RULE_TYPES` list.
.. versionadded:: 0.4.0
"""
clip_rule = library.DrawGetClipRule(self.resource)
return FILL_RULE_TYPES[clip_rule]
@clip_rule.setter
def clip_rule(self, clip_rule):
assertions.string_in_list(FILL_RULE_TYPES,
'wand.drawing.FILL_RULE_TYPES',
clip_rule=clip_rule)
library.DrawSetClipRule(self.resource,
FILL_RULE_TYPES.index(clip_rule))
@property
def clip_units(self):
"""(:class:`basestring`) The current clip units. It also can be set.
It's a string value from :const:`CLIP_PATH_UNITS` list.
.. versionadded:: 0.4.0
"""
clip_unit = library.DrawGetClipUnits(self.resource)
return CLIP_PATH_UNITS[clip_unit]
@clip_units.setter
def clip_units(self, clip_unit):
assertions.string_in_list(CLIP_PATH_UNITS,
'wand.drawing.CLIP_PATH_UNITS',
clip_unit=clip_unit)
library.DrawSetClipUnits(self.resource,
CLIP_PATH_UNITS.index(clip_unit))
@property
def fill_color(self):
"""(:class:`~wand.color.Color`) The current color to fill.
It also can be set.
"""
pixel = library.NewPixelWand()
library.DrawGetFillColor(self.resource, pixel)
color = Color.from_pixelwand(pixel)
pixel = library.DestroyPixelWand(pixel)
return color
@fill_color.setter
def fill_color(self, color):
if isinstance(color, string_type):
color = Color(color)
assertions.assert_color(fill_color=color)
with color:
library.DrawSetFillColor(self.resource, color.resource)
@property
def fill_opacity(self):
"""(:class:`~numbers.Real`) The current fill opacity.
It also can be set.
.. versionadded:: 0.4.0
"""
return library.DrawGetFillOpacity(self.resource)
@fill_opacity.setter
def fill_opacity(self, opacity):
assertions.assert_real(fill_opacity=opacity)
library.DrawSetFillOpacity(self.resource, opacity)
@property
def fill_rule(self):
"""(:class:`basestring`) The current fill rule. It can also be set.
It's a string value from :const:`FILL_RULE_TYPES` list.
.. versionadded:: 0.4.0
"""
fill_rule_index = library.DrawGetFillRule(self.resource)
if fill_rule_index not in FILL_RULE_TYPES:
self.raise_exception()
return text(FILL_RULE_TYPES[fill_rule_index])
@fill_rule.setter
def fill_rule(self, fill_rule):
assertions.string_in_list(FILL_RULE_TYPES,
'wand.drawing.FILL_RULE_TYPES',
fill_rule=fill_rule)
library.DrawSetFillRule(self.resource,
FILL_RULE_TYPES.index(fill_rule))
@property
def font(self):
"""(:class:`basestring`) The current font name. It also can be set.
.. versionchanged: 0.4.1
Safely release allocated memory with
:c:func:`MagickRelinquishMemory` instead of :c:func:`libc.free`.
"""
font_str = None
font_p = library.DrawGetFont(self.resource)
if font_p:
font_str = text(ctypes.string_at(font_p))
font_p = library.MagickRelinquishMemory(font_p)
return font_str
@font.setter
def font(self, font):
assertions.assert_string(font=font)
library.DrawSetFont(self.resource, binary(font))
@property
def font_family(self):
"""(:class:`basestring`) The current font family. It also can be set.
.. versionadded:: 0.4.0
.. versionchanged: 0.4.1
Safely release allocated memory with
:c:func:`MagickRelinquishMemory` instead of :c:func:`libc.free`.
"""
font_family_str = None
font_family_p = library.DrawGetFontFamily(self.resource)
if font_family_p:
font_family_str = text(ctypes.string_at(font_family_p))
font_family_p = library.MagickRelinquishMemory(font_family_p)
return font_family_str
@font_family.setter
def font_family(self, family):
assertions.assert_string(font_family=family)
library.DrawSetFontFamily(self.resource, binary(family))
@property
def font_resolution(self):
"""(:class:`~collections.abc.Sequence`) The current font resolution.
It also can be set.
.. versionadded:: 0.4.0
"""
x, y = ctypes.c_double(0.0), ctypes.c_double(0.0)
library.DrawGetFontResolution(self.resource,
ctypes.byref(x),
ctypes.byref(y))
return x.value, y.value
@font_resolution.setter
def font_resolution(self, resolution):
assertions.assert_coordinate(font_resolution=resolution)
library.DrawSetFontResolution(self.resource, *resolution)
@property
def font_size(self):
"""(:class:`numbers.Real`) The font size. It also can be set."""
return library.DrawGetFontSize(self.resource)
@font_size.setter
def font_size(self, size):
assertions.assert_real(font_size=size)
if size < 0.0:
raise ValueError('cannot be less than 0.0, but got ' + repr(size))
library.DrawSetFontSize(self.resource, size)
@property
def font_stretch(self):
"""(:class:`basestring`) The current font stretch variation.
It also can be set, but will only apply if the font-family or encoder
supports the stretch type.
.. versionadded:: 0.4.0
"""
stretch_index = library.DrawGetFontStretch(self.resource)
return text(STRETCH_TYPES[stretch_index])
@font_stretch.setter
def font_stretch(self, stretch):
assertions.string_in_list(STRETCH_TYPES,
'wand.drawing.STRETCH_TYPES',
font_stretch=stretch)
library.DrawSetFontStretch(self.resource,
STRETCH_TYPES.index(stretch))
@property
def font_style(self):
"""(:class:`basestring`) The current font style.
It also can be set, but will only apply if the font-family
supports the style.
.. versionadded:: 0.4.0
"""
style_index = library.DrawGetFontStyle(self.resource)
return text(STYLE_TYPES[style_index])
@font_style.setter
def font_style(self, style):
assertions.string_in_list(STYLE_TYPES,
'wand.drawing.STYLE_TYPES',
font_style=style)
library.DrawSetFontStyle(self.resource,
STYLE_TYPES.index(style))
@property
def font_weight(self):
"""(:class:`~numbers.Integral`) The current font weight.
It also can be set.
.. versionadded:: 0.4.0
"""
return library.DrawGetFontWeight(self.resource)
@font_weight.setter
def font_weight(self, weight):
assertions.assert_integer(font_weight=weight)
library.DrawSetFontWeight(self.resource, weight)
@property
def gravity(self):
"""(:class:`basestring`) The text placement gravity used when
annotating with text. It's a string from :const:`GRAVITY_TYPES`
list. It also can be set.
"""
gravity_index = library.DrawGetGravity(self.resource)
if not gravity_index:
self.raise_exception()
return text(GRAVITY_TYPES[gravity_index])
@gravity.setter
def gravity(self, value):
assertions.string_in_list(GRAVITY_TYPES,
'wand.drawing.GRAVITY_TYPES',
gravity=value)
library.DrawSetGravity(self.resource, GRAVITY_TYPES.index(value))
@property
def opacity(self):
"""(:class:`~numbers.Real`) returns the opacity used when drawing with
the fill or stroke color or texture. Fully opaque is 1.0. This method
only affects vector graphics, and is experimental. To set the opacity
of a drawing, use
:attr:`Drawing.fill_opacity` & :attr:`Drawing.stroke_opacity`
.. versionadded:: 0.4.0
"""
return library.DrawGetOpacity(self.resource)
@opacity.setter
def opacity(self, opaque):
assertions.assert_real(opacity=opaque)
library.DrawSetOpacity(self.resource, opaque)
@property
def stroke_antialias(self):
"""(:class:`bool`) Controls whether stroked outlines are antialiased.
Stroked outlines are antialiased by default. When antialiasing is
disabled stroked pixels are thresholded to determine if the stroke
color or underlying canvas color should be used.
It also can be set.
.. versionadded:: 0.4.0
"""
stroke_antialias = library.DrawGetStrokeAntialias(self.resource)
return bool(stroke_antialias)
@stroke_antialias.setter
def stroke_antialias(self, stroke_antialias):
assertions.assert_bool(stroke_antialias=stroke_antialias)
library.DrawSetStrokeAntialias(self.resource, stroke_antialias)
@property
def stroke_color(self):
"""(:class:`~wand.color.Color`) The current color of stroke.
It also can be set.
.. versionadded:: 0.3.3
"""
pixel = library.NewPixelWand()
library.DrawGetStrokeColor(self.resource, pixel)
color = Color.from_pixelwand(pixel)
pixel = library.DestroyPixelWand(pixel)
return color
@stroke_color.setter
def stroke_color(self, color):
if isinstance(color, string_type):
color = Color(color)
assertions.assert_color(stroke_color=color)
with color:
library.DrawSetStrokeColor(self.resource, color.resource)
@property
def stroke_dash_array(self):
"""(:class:`~collections.abc.Sequence`) - (:class:`numbers.Real`) An
array representing the pattern of dashes & gaps used to stroke paths.
It also can be set.
.. versionadded:: 0.4.0
.. versionchanged: 0.4.1
Safely release allocated memory with
:c:func:`MagickRelinquishMemory` instead of :c:func:`libc.free`.
"""
number_elements = ctypes.c_size_t(0)
dash_array_p = library.DrawGetStrokeDashArray(
self.resource, ctypes.byref(number_elements)
)
dash_array = []
if dash_array_p:
dash_array = [float(dash_array_p[i])
for i in xrange(number_elements.value)]
dash_array_p = library.MagickRelinquishMemory(dash_array_p)
return dash_array
@stroke_dash_array.setter
def stroke_dash_array(self, dash_array):
dash_array_l = len(dash_array)
dash_array_p = (ctypes.c_double * dash_array_l)(*dash_array)
library.DrawSetStrokeDashArray(self.resource,
dash_array_l,
dash_array_p)
@property
def stroke_dash_offset(self):
"""(:class:`numbers.Real`) The stroke dash offset. It also can be set.
.. versionadded:: 0.4.0
"""
return library.DrawGetStrokeDashOffset(self.resource)
@stroke_dash_offset.setter
def stroke_dash_offset(self, offset):
assertions.assert_real(stroke_dash_offset=offset)
library.DrawSetStrokeDashOffset(self.resource, offset)
@property
def stroke_line_cap(self):
"""(:class:`basestring`) The stroke line cap. It also can be set.
.. versionadded:: 0.4.0
"""
line_cap_index = library.DrawGetStrokeLineCap(self.resource)
if line_cap_index not in LINE_CAP_TYPES:
self.raise_exception()
return text(LINE_CAP_TYPES[line_cap_index])
@stroke_line_cap.setter
def stroke_line_cap(self, line_cap):
assertions.string_in_list(LINE_CAP_TYPES,
'wand.drawing.LINE_CAP_TYPES',
stroke_line_cap=line_cap)
library.DrawSetStrokeLineCap(self.resource,
LINE_CAP_TYPES.index(line_cap))
@property
def stroke_line_join(self):
"""(:class:`basestring`) The stroke line join. It also can be set.
.. versionadded:: 0.4.0
"""
line_join_index = library.DrawGetStrokeLineJoin(self.resource)
if line_join_index not in LINE_JOIN_TYPES:
self.raise_exception()
return text(LINE_JOIN_TYPES[line_join_index])
@stroke_line_join.setter
def stroke_line_join(self, line_join):
assertions.string_in_list(LINE_JOIN_TYPES,
'wand.drawing.LINE_JOIN_TYPES',
stroke_line_join=line_join)
library.DrawSetStrokeLineJoin(self.resource,
LINE_JOIN_TYPES.index(line_join))
@property
def stroke_miter_limit(self):
"""(:class:`~numbers.Integral`) The current miter limit.
It also can be set.
.. versionadded:: 0.4.0
"""
return library.DrawGetStrokeMiterLimit(self.resource)
@stroke_miter_limit.setter
def stroke_miter_limit(self, miter_limit):
assertions.assert_integer(stroke_miter_limit=miter_limit)
library.DrawSetStrokeMiterLimit(self.resource, miter_limit)
@property
def stroke_opacity(self):
"""(:class:`~numbers.Real`) The current stroke opacity.
It also can be set.
.. versionadded:: 0.4.0
"""
return library.DrawGetStrokeOpacity(self.resource)
@stroke_opacity.setter
def stroke_opacity(self, opacity):
assertions.assert_real(stroke_opacity=opacity)
library.DrawSetStrokeOpacity(self.resource, opacity)
@property
def stroke_width(self):
"""(:class:`numbers.Real`) The stroke width. It also can be set.
.. versionadded:: 0.3.3
"""
return library.DrawGetStrokeWidth(self.resource)
@stroke_width.setter
def stroke_width(self, width):
assertions.assert_real(stroke_width=width)
if width < 0.0:
raise ValueError('cannot be less than 0.0, but got ' + repr(width))
library.DrawSetStrokeWidth(self.resource, width)
@property
def text_alignment(self):
"""(:class:`basestring`) The current text alignment setting.
It's a string value from :const:`TEXT_ALIGN_TYPES` list.
It also can be set.
"""
text_alignment_index = library.DrawGetTextAlignment(self.resource)
if not text_alignment_index: # pragma: no cover
self.raise_exception()
return text(TEXT_ALIGN_TYPES[text_alignment_index])
@text_alignment.setter
def text_alignment(self, align):
assertions.string_in_list(TEXT_ALIGN_TYPES,
'wand.drawing.TEXT_ALIGN_TYPES',
text_alignment=align)
library.DrawSetTextAlignment(self.resource,
TEXT_ALIGN_TYPES.index(align))
@property
def text_antialias(self):
"""(:class:`bool`) The boolean value which represents whether
antialiasing is used for text rendering. It also can be set to
``True`` or ``False`` to switch the setting.
"""
result = library.DrawGetTextAntialias(self.resource)
return bool(result)
@text_antialias.setter
def text_antialias(self, value):
assertions.assert_bool(text_antialias=value)
library.DrawSetTextAntialias(self.resource, value)
@property
def text_decoration(self):
"""(:class:`basestring`) The text decoration setting, a string
from :const:`TEXT_DECORATION_TYPES` list. It also can be set.
"""
text_decoration_index = library.DrawGetTextDecoration(self.resource)
if not text_decoration_index: # pragma: no cover
self.raise_exception()
return text(TEXT_DECORATION_TYPES[text_decoration_index])
@text_decoration.setter
def text_decoration(self, decoration):
assertions.string_in_list(TEXT_DECORATION_TYPES,
'wand.drawing.TEXT_DECORATION_TYPES',
text_decoration=decoration)
library.DrawSetTextDecoration(self.resource,
TEXT_DECORATION_TYPES.index(decoration))
@property
def text_direction(self):
"""(:class:`basestring`) The text direction setting. a string
from :const:`TEXT_DIRECTION_TYPES` list. It also can be set."""
if library.DrawGetTextDirection is None: # pragma: no cover
raise WandLibraryVersionError(
'the installed version of ImageMagick does not support '
'this feature'
)
text_direction_index = library.DrawGetTextDirection(self.resource)
if not text_direction_index: # pragma: no cover
self.raise_exception()
return text(TEXT_DIRECTION_TYPES[text_direction_index])
@text_direction.setter
def text_direction(self, direction):
if library.DrawGetTextDirection is None: # pragma: no cover
raise WandLibraryVersionError(
'The installed version of ImageMagick does not support '
'this feature'
)
assertions.string_in_list(TEXT_DIRECTION_TYPES,
'wand.drawing.TEXT_DIRECTION_TYPES',
text_direction=direction)
library.DrawSetTextDirection(self.resource,
TEXT_DIRECTION_TYPES.index(direction))
@property
def text_encoding(self):
"""(:class:`basestring`) The internally used text encoding setting.
Although it also can be set, but it's not encouraged.
.. versionchanged: 0.4.1
Safely release allocated memory with
:c:func:`MagickRelinquishMemory` instead of :c:func:`libc.free`.
"""
text_encoding_str = None
text_encoding_p = library.DrawGetTextEncoding(self.resource)
if text_encoding_p:
text_encoding_str = text(ctypes.string_at(text_encoding_p))
text_encoding_p = library.MagickRelinquishMemory(text_encoding_p)
return text_encoding_str
@text_encoding.setter
def text_encoding(self, encoding):
if encoding is None:
# encoding specify an empty string to set text encoding
# to system's default.
encoding = b''
else:
assertions.assert_string(text_encoding=encoding)
encoding = binary(encoding)
library.DrawSetTextEncoding(self.resource, encoding)
@property
def text_interline_spacing(self):
"""(:class:`numbers.Real`) The setting of the text line spacing.
It also can be set.
"""
if library.DrawGetTextInterlineSpacing is None: # pragma: no cover
raise WandLibraryVersionError('The installed version of '
'ImageMagick does not support '
'this feature')
return library.DrawGetTextInterlineSpacing(self.resource)
@text_interline_spacing.setter
def text_interline_spacing(self, spacing):
if library.DrawSetTextInterlineSpacing is None: # pragma: no cover
raise WandLibraryVersionError('The installed version of '
'ImageMagick does not support '
'this feature')
assertions.assert_real(text_interline_spacing=spacing)
library.DrawSetTextInterlineSpacing(self.resource, spacing)
@property
def text_interword_spacing(self):
"""(:class:`numbers.Real`) The setting of the word spacing.
It also can be set.
"""
return library.DrawGetTextInterwordSpacing(self.resource)
@text_interword_spacing.setter
def text_interword_spacing(self, spacing):
assertions.assert_real(text_interword_spacing=spacing)
library.DrawSetTextInterwordSpacing(self.resource, spacing)
@property
def text_kerning(self):
"""(:class:`numbers.Real`) The setting of the text kerning.
It also can be set.
"""
return library.DrawGetTextKerning(self.resource)
@text_kerning.setter
def text_kerning(self, kerning):
assertions.assert_real(text_kerning=kerning)
library.DrawSetTextKerning(self.resource, kerning)
@property
def text_under_color(self):
"""(:class:`~wand.color.Color`) The color of a background rectangle
to place under text annotations. It also can be set.
"""
pixel = library.NewPixelWand()
library.DrawGetTextUnderColor(self.resource, pixel)
color = Color.from_pixelwand(pixel)
pixel = library.DestroyPixelWand(pixel)
return color
@text_under_color.setter
def text_under_color(self, color):
if isinstance(color, string_type):
color = Color(color)
assertions.assert_color(text_under_color=color)
with color:
library.DrawSetTextUnderColor(self.resource, color.resource)
@property
def vector_graphics(self):
"""(:class:`basestring`) The XML text of the Vector Graphics.
It also can be set. The drawing-wand XML is experimental,
and subject to change.
Setting this property to None will reset all vector graphic properties
to the default state.
.. versionadded:: 0.4.0
.. versionchanged: 0.4.1
Safely release allocated memory with
:c:func:`MagickRelinquishMemory` instead of :c:func:`libc.free`.
"""
vg_p = library.DrawGetVectorGraphics(self.resource)
if vg_p:
vg_str = ctypes.string_at(vg_p)
vg_p = library.MagickRelinquishMemory(vg_p)
else:
vg_str = b''
return '<wand>' + text(vg_str) + '</wand>'
@vector_graphics.setter
def vector_graphics(self, vector_graphics):
if vector_graphics is not None and not isinstance(vector_graphics,
string_type):
raise TypeError('expected a string, not ' + repr(vector_graphics))
elif vector_graphics is None:
# Reset all vector graphic properties on drawing wand.
library.DrawResetVectorGraphics(self.resource)
else:
vector_graphics = binary(vector_graphics)
okay = library.DrawSetVectorGraphics(self.resource,
vector_graphics)
if okay == 0: # pragma: no cover
raise ValueError("Vector graphic not understood.")
def affine(self, matrix):
"""Adjusts the current affine transformation matrix with the specified
affine transformation matrix. Note that the current affine transform is
adjusted rather than replaced.
.. sourcecode:: text
| sx rx 0 |
| x', y', 1 | = | x, y, 1 | * | ry sy 0 |
| tx ty 1 |
:param matrix: a list of :class:`~numbers.Real` to define affine
matrix ``[sx, rx, ry, sy, tx, ty]``
:type matrix: :class:`collections.abc.Sequence`
.. versionadded:: 0.4.0
"""
if not isinstance(matrix, abc.Sequence) or len(matrix) != 6:
raise ValueError('matrix must be a list of size Real numbers')
for idx, val in enumerate(matrix):
if not isinstance(val, numbers.Real):
raise TypeError('expecting numbers.Real in position #' +
repr(idx))
amx = AffineMatrix(sx=matrix[0], rx=matrix[1],
ry=matrix[2], sy=matrix[3],
tx=matrix[4], ty=matrix[5])
library.DrawAffine(self.resource, ctypes.byref(amx))
def alpha(self, x=None, y=None, paint_method='undefined'):
"""Paints on the image's opacity channel in order to set effected
pixels to transparent.
To influence the opacity of pixels. The available methods are:
- ``'undefined'``
- ``'point'``
- ``'replace'``
- ``'floodfill'``
- ``'filltoborder'``
- ``'reset'``
.. note::
This method replaces :meth:`matte()` in ImageMagick version 7.
An :class:`AttributeError` will be raised if attempting
to call on a library without ``DrawAlpha`` support.
.. versionadded:: 0.5.0
"""
if library.DrawAlpha is None:
raise AttributeError(
'Method added with ImageMagick version 7. ' +
'Please use `wand.drawing.Drawing.matte()\' instead.'
)
assertions.assert_real(x=x, y=y)
assertions.string_in_list(PAINT_METHOD_TYPES,
'wand.drawing.PAINT_METHOD_TYPES',
paint_method=paint_method)
op = PAINT_METHOD_TYPES.index(paint_method)
library.DrawAlpha(self.resource, x, y, op)
def arc(self, start, end, degree):
"""Draws a arc using the current :attr:`stroke_color`,
:attr:`stroke_width`, and :attr:`fill_color`.
:param start: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents starting x and y of the arc
:type start: :class:`~collections.abc.Sequence`
:param end: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents ending x and y of the arc
:type end: :class:`~collections.abc.Sequence`
:param degree: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents starting degree, and ending degree
:type degree: :class:`~collections.abc.Sequence`
.. versionadded:: 0.4.0
"""
assertions.assert_coordinate(start=start, end=end, degree=degree)
start_x, start_y = start
end_x, end_y = end
degree_start, degree_end = degree
library.DrawArc(self.resource,
float(start_x), float(start_y),
float(end_x), float(end_y),
float(degree_start), float(degree_end))
def bezier(self, points=None):
"""Draws a bezier curve through a set of points on the image, using
the specified array of coordinates.
At least four points should be given to complete a bezier path.
The first & forth point being the start & end point, and the second
& third point controlling the direction & curve.
Example bezier on ``image`` ::
with Drawing() as draw:
points = [(40,10), # Start point
(20,50), # First control
(90,10), # Second control
(70,40)] # End point
draw.stroke_color = Color('#000')
draw.fill_color = Color('#fff')
draw.bezier(points)
draw.draw(image)
:param points: list of x,y tuples
:type points: :class:`list`
.. versionadded:: 0.4.0
"""
(points_l, points_p) = _list_to_point_info(points)
library.DrawBezier(self.resource, points_l,
ctypes.cast(points_p, ctypes.POINTER(PointInfo)))
def circle(self, origin, perimeter):
"""Draws a circle from ``origin`` to ``perimeter``
:param origin: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents origin x and y of circle
:type origin: :class:`collections.abc.Sequence`
:param perimeter: (:class:`~numbers.Real`, :class:`numbers.Real`)
pair which represents perimeter x and y of circle
:type perimeter: :class:`collections.abc.Sequence`
.. versionadded:: 0.4.0
"""
assertions.assert_coordinate(origin=origin, perimeter=perimeter)
origin_x, origin_y = origin