/usr/share/pyshared/gamera/plugins/draw.py is in python-gamera 3.3.3-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #
#
# Copyright (C) 2001-2009 Ichiro Fujinaga, Michael Droettboom,
# Karl MacMillan, and Christoph Dalitz
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from gamera.plugin import *
from gamera.util import warn_deprecated
import _draw
try:
from gamera.core import RGBPixel
except:
def RGBPixel(*args):
pass
class draw_marker(PluginFunction):
"""
Draws a marker at a given point.
Coordinates are absolute and *not* relative to the offset of the image.
Therefore, if the image offset is (5, 5), a marker at (5, 5)
will be in the upper left hand corner of the image.
*a*
The position of the marker.
*size*
The size of the marker (number of pixels)
*style*
PLUS + (0)
X + (1)
HOLLOW_SQUARE (2)
FILLED_SQUARE (3)
*value*
The pixel value to set for the line.
"""
self_type = ImageType(ALL)
args = Args([FloatPoint("location"), Int("size", default=5),
Choice("style", "+ x hollow_square filled_square".split(),
default=0),
Pixel("value")])
author = "Michael Droettboom"
def __doc_example1__(images):
from random import randint
from gamera.core import Image, Dim
image = Image((0, 0), Dim(100, 100), RGB, DENSE)
points = [(randint(0, 100), randint(0, 100)) for x in range(4)]
image.draw_bezier(*tuple(list(points) + [RGBPixel(255, 0, 0), 0.1]))
image.draw_marker(points[0], 7, 0, RGBPixel(0, 0, 255))
image.draw_marker(points[1], 7, 1, RGBPixel(0, 255, 0))
image.draw_marker(points[2], 7, 1, RGBPixel(0, 255, 0))
image.draw_marker(points[3], 7, 0, RGBPixel(0, 0, 255))
return image
doc_examples = [__doc_example1__]
class draw_line(PluginFunction):
"""
Draws a straight line between two points.
Coordinates are absolute and *not* relative to the offset of the image.
Therefore, if the image offset is (5, 5), a line at (5, 5) will be
in the upper left hand corner of the image.
*a*:
The start ``FloatPoint``.
*b*:
The end ``FloatPoint``.
*value*:
The pixel value to set for the line.
*thickness* = 1.0:
The thickness of the line (in pixels)
The implementation is based on the classic Bresenham algorithm, as described
in J. E. Bresenham: *Algorithm for computer control of a digital plotter.*
IBM Systems Journal 4, pp. 25-30 (1965).
"""
self_type = ImageType(ALL)
args = Args([FloatPoint("start"), FloatPoint("end"), Pixel("value"), Float("thickness")])
author = "Michael Droettboom and Christoph Dalitz"
def __call__(self, start, end, value, thickness = 1.0):
return _draw.draw_line(self, start, end, value, thickness)
__call__ = staticmethod(__call__)
def __doc_example1__(images):
from random import randint
from gamera.core import Image, Dim
image = Image((0, 0), Dim(100, 100), RGB, DENSE)
for i in range(10):
image.draw_line((randint(0, 100), randint(0, 100)),
(randint(0, 100), randint(0, 100)),
RGBPixel(randint(0, 255), randint(0,255), randint(0, 255)),
float(randint(0, 4)))
return image
doc_examples = [__doc_example1__]
class draw_hollow_rect(PluginFunction):
"""
Draws a hollow rectangle.
Coordinates are absolute and *not* relative to the offset of the image.
Therefore, if the image offset is (5, 5), a rectangle at (5, 5) will be
in the upper left hand corner of the image.
The coordinates can be specified either by two FloatPoints or one
Rect:
**draw_hollow_rect** (FloatPoint(*x1*, *y1*), FloatPoint(*x2*,
*y2*), *value*, *thickness* = 1.0)
**draw_hollow_rect** (Rect *rect*, *value*, *thickness* = 1.0)
*value*:
The pixel value to set for the lines.
*thickness* = 1.0:
The thickness of the outline
"""
self_type = ImageType(ALL)
args = Args([FloatPoint("ul"), FloatPoint("lr"), Pixel("value"), Float("thickness")])
def __call__(self, *args):
from gamera.gameracore import Rect
if len(args) == 4:
return _draw.draw_hollow_rect(self, *args)
elif len(args) == 3:
a, b, c = args
if hasattr(a, 'ul') and hasattr(a, 'lr'):
return _draw.draw_hollow_rect(self, a.ul, a.lr, b, c)
else:
return _draw.draw_hollow_rect(self, a, b, c, 1.0)
elif len(args) == 2:
a, b = args
if hasattr(a, 'ul') and hasattr(a, 'lr'):
return _draw.draw_hollow_rect(self, a.ul, a.lr, b, 1.0)
raise ValueError("Arguments to draw_hollow_rect are incorrect.")
__call__ = staticmethod(__call__)
def __doc_example1__(images):
from random import randint
from gamera.core import Image, Dim
image = Image((0, 0), Dim(100, 100), RGB, DENSE)
for i in range(10):
image.draw_hollow_rect((randint(0, 100), randint(0, 100)),
(randint(0, 100), randint(0, 100)),
RGBPixel(randint(0, 255), randint(0,255), randint(0, 255)))
return image
doc_examples = [__doc_example1__]
class draw_filled_rect(PluginFunction):
"""
Draws a filled rectangle.
Coordinates are absolute and *not* relative to the offset of the image.
Therefore, if the image offset is (5, 5), a rectangle at (5, 5) will
be in the upper left hand corner of the image.
The coordinates can be specified either by four integers, two
FloatPoints, or one Rect:
**draw_filled_rect** (FloatPoint(*x1*, *y1*), FloatPoint(*x2*,
*y2*), *value*)
**draw_filled_rect** (Rect *rect*, *value*)
"""
self_type = ImageType(ALL)
args = Args([FloatPoint("ul"), FloatPoint("lr"), Pixel("value")])
def __call__(self, *args):
if len(args) == 3:
return _draw.draw_filled_rect(self, *args)
elif len(args) == 2:
a, value = args
return _draw.draw_filled_rect(self, a.ul, a.lr, value)
raise ValueError("Arguments to draw_filled_rect are incorrect.")
__call__ = staticmethod(__call__)
def __doc_example1__(images):
from random import randint
from gamera.core import Image, Dim
image = Image((0, 0), Dim(100, 100), RGB, DENSE)
for i in range(10):
image.draw_filled_rect((randint(0, 100), randint(0, 100)),
(randint(0, 100), randint(0, 100)),
RGBPixel(randint(0, 255), randint(0,255), randint(0, 255)))
return image
doc_examples = [__doc_example1__]
class draw_bezier(PluginFunction):
"""
Draws a cubic bezier curve
Coordinates are absolute and *not* relative to the offset of the image.
Therefore, if the image offset is (5, 5), a curve at (5, 5) will be in
the upper left hand corner of the image.
*start*:
The start ``FloatPoint``.
*c1*:
The control point associated with the start point.
*c2*
The control point associated with the end point.
*end*
The end ``FloatPoint``.
*value*:
The pixel value to set for the curve.
*accuracy* = ``0.1``:
The rendering accuracy (in pixels)
"""
self_type = ImageType(ALL)
args = Args([FloatPoint("start"), FloatPoint("c1"), FloatPoint("c2"), FloatPoint("end"),
Pixel("value"), Float("accuracy", default=0.1)])
def __call__(self, start, c1, c2, end, value, accuracy = 0.1):
return _draw.draw_bezier(self, start, c2, c2, end, value, accuracy)
__call__ = staticmethod(__call__)
def __doc_example1__(images):
from random import randint
from gamera.core import Image, Dim
image = Image((0, 0), Dim(100, 100), RGB, DENSE)
for i in range(10):
image.draw_bezier((randint(0, 100), randint(0, 100)),
(randint(0, 100), randint(0, 100)),
(randint(0, 100), randint(0, 100)),
(randint(0, 100), randint(0, 100)),
RGBPixel(randint(0, 255), randint(0,255), randint(0, 255)),
0.1)
return image
doc_examples = [__doc_example1__]
class draw_circle(PluginFunction):
"""
Draws a center centered at *c* with radius *r*.
Coordinates are absolute and *not* relative to the offset of the image.
Therefore, if the image offset is (5, 5), a line at (5, 5) will be
in the upper left hand corner of the image.
*c*:
The center of the circle
*r*:
The radius of the circle
*value*:
The pixel value to set for the line.
*thickness* = 1.0:
The thickness of the circle (in pixels)
*accuracy* = 0.1:
The accuracy of the circle drawing
Based on the "approximating a circle with Bezier curves" approach.
http://www.whizkidtech.redprince.net/bezier/circle/
"""
self_type = ImageType(ALL)
args = Args([FloatPoint("c"), Float("r"), Pixel("value"), Float("thickness"), Float("accuracy")])
author = "Michael Droettboom with the help of Christoph Dalitz"
def __call__(self, c, r, value, thickness = 1.0, accuracy = 0.1):
return _draw.draw_circle(self, c, r, value, thickness, accuracy)
__call__ = staticmethod(__call__)
def __doc_example1__(images):
from random import randint
from gamera.core import Image, Dim
image = Image((0, 0), Dim(100, 100), RGB, DENSE)
for i in range(10):
image.draw_circle((randint(0, 100), randint(0, 100)),
randint(0, 100),
RGBPixel(randint(0, 255), randint(0,255), randint(0, 255)),
1.0, 0.1)
return image
doc_examples = [__doc_example1__]
class draw_text(PluginFunction):
"""
Draws text onto the image. The results are fairly primitive. The
text is not anti-aliased, but should be adequate for marking up the
image for debugging purposes.
This function relies on wxPython for font rendering. Therefore,
wxPython must be installed, and it must be runnable in the current
environment, for example, a connection to a display or X server must
be available. This may not be the case when running over a remote
terminal, for example.
*p*
The position of the text. The *x* component will be either at the
left, center, or right of the text, depending on the value of
*halign*. The *y* component specifies the top of the text.
*text*
The string to be drawn on the image. If this is an 8-bit string the string
should be encoded in the default encoding for your system. If a Unicode
string, the text will be rendered using a Unicode font. This requires
that your wxPython version is compiled with Unicode support.
*color*
The color of the text to be drawn. Note that there are some peculiarities
for **ONEBIT** images:
If *color* is zero, the text will be white. If non-zero, the
text will be black. It is not possible to set different values
for the text.
*family*
The font family to be used. For platform independence, this argument
takes a general typeface style which will be mapped to an appropriate
typeface on each platform.
SERIF (0)
SANS-SERIF (1)
MONOSPACE (2)
*size* = 10
The font size **in pixels** (i.e. not in "points")
*italic* = ``False``
If ``True``, the text will be italic.
*bold* = ``False``
If ``True``, the text will be bold.
*halign*
LEFT JUSTIFIED (0): The text is left-justified to the point *p*.
CENTERED (1): The text is centered horizontally to the point *p*.
RIGHT JUSTIFIED (2): The text is right-justified to the point *p*.
"""
self_type = ImageType([ONEBIT, RGB])
args = Args([FloatPoint("p"), String("text"), Pixel("color"),
Int("size", default=10, range=(3, 512)),
Choice("font_family", ['serif', 'sans-serif', 'monospace']),
Check("italic", default=False),
Check("bold", default=False),
Choice("halign", ["left", "center", "right"])
])
author = "Michael Droettboom"
pure_python = True
def __call__(self, p, text, color, size=10, font_family=0,
italic=False, bold=False, halign=0):
from gamera.core import Dim, RGB, ONEBIT, Image
from gamera.plugins import string_io
try:
import wx
except ImportError:
raise RuntimeError("Drawing text requires wxPython.")
try:
dc = wx.MemoryDC()
except wx._core.PyNoAppError:
app = wx.App()
dc = wx.MemoryDC()
if font_family < 0 or font_family > 2:
raise ValueError("font_family must be in range 0-2.")
font_family = [wx.ROMAN, wx.SWISS, wx.MODERN][font_family]
italic = (italic and wx.ITALIC) or wx.NORMAL
bold = (bold and wx.BOLD) or wx.NORMAL
if type(text) == str:
encoding = wx.FONTENCODING_SYSTEM
elif type(text) == unicode:
encoding = wx.FONTENCODING_UNICODE
else:
raise ValueError("text must be a string or unicode string.")
font = wx.Font(size, font_family, italic, bold,
encoding = encoding)
font.SetPixelSize(wx.Size(size * 2, size * 2))
dc.SetFont(font)
w, h = dc.GetTextExtent(text)
# Do the actual drawing
bmp = wx.EmptyBitmap(w, h, -1)
dc.SelectObject(bmp)
dc.SetPen(wx.TRANSPARENT_PEN)
dc.SetBrush(wx.WHITE_BRUSH)
dc.DrawRectangle(0, 0, w, h)
dc.SetBrush(wx.BLACK_BRUSH)
dc.DrawText(text, 0, 0)
img = bmp.ConvertToImage()
img_str = img.GetData()
text_image = string_io._from_raw_string(
(0, 0), Dim(w, h), RGB, 0, img_str)
text_image = text_image.to_onebit()
if halign == 1:
p = (p[0] - w / 2, p[1])
elif halign == 2:
p = (p[0] - w, p[1])
ul = (max(p[0], self.ul_x), max(p[1], self.ul_y))
lr = (min(p[0] + w, self.lr_x), min(p[1] + h, self.lr_y))
w = lr[0] - ul[0]
h = lr[1] - ul[1]
if w < 0 or h < 0:
return
text_image = text_image.subimage(
(max(self.ul_x - p[0], 0), max(self.ul_y - p[1], 0)),
Dim(w, h))
if self.data.pixel_type == ONEBIT:
subimage = self.subimage(ul, Dim(w, h))
if color:
subimage.or_image(text_image, in_place=True)
else:
text_image.invert()
subimage.and_image(text_image, in_place=True)
elif self.data.pixel_type == RGB:
subimage = Image((max(ul[0] - self.ul_x, p[0]),
max(ul[1] - self.ul_y, p[1])),
Dim(w, h), ONEBIT)
subimage.or_image(text_image, in_place=True)
self.highlight(subimage, color)
__call__ = staticmethod(__call__)
def __doc_example1__(images):
from random import randint
from gamera.core import Image, Dim
image = Image((0, 0), Dim(320, 300), RGB, DENSE)
# These are some various Unicode encoded names of different
# languages (from the wikipedia.org front page). Just a quick way
# to test Unicode support. I use the long encoding here so this
# source file iteself will be portable.
names = ['\xd0\x91\xd1\x8a\xd0\xbb\xd0\xb3\xd0\xb0\xd1\x80\xd1\x81\xd0\xba\xd0\xb8',
'\xd7\xa2\xd7\x91\xd7\xa8\xd7\x99\xd7\xaa',
'\xd8\xa7\xd9\x84\xd8\xb9\xd8\xb1\xd8\xa8\xd9\x8a\xd8\xa9',
'\xc4\x8cesk\xc3\xa1',
'Rom\xc3\xa2n\xc4\x83',
'\xe1\x83\xa5\xe1\x83\x90\xe1\x83\xa0\xe1\x83\x97\xe1\x83\xa3\xe1\x83\x9a\xe1\x83\x98',
'\xd0\x9c\xd0\xb0\xd0\xba\xd0\xb5\xd0\xb4\xd0\xbe\xd0\xbd\xd1\x81\xd0\xba\xd0\xb8',
'\xc3\x8dslenska',
'Lietuvi\xc5\xb3',
'T\xc3\xbcrk\xc3\xa7e']
try:
for i, name in enumerate(names):
image.draw_text((160, i * 30),
name, RGBPixel(randint(0, 255), randint(0,255), randint(0, 255)), 20, halign=1)
except:
pass
return image
if sys.platform != 'darwin':
doc_examples = [__doc_example1__]
class flood_fill(PluginFunction):
"""
Flood fills from the given point using the given color. This is similar
to the "bucket" tool found in many paint programs.
The coordinates can be specified either by two integers or one Point:
*a*:
The start ``Point``.
*color*:
The pixel value to set for the rectangle.
"""
self_type = ImageType([GREYSCALE, FLOAT, ONEBIT, RGB])
args = Args([Point("start"), Pixel("color")])
doc_examples = [(ONEBIT, (10, 58), 0)]
class remove_border(PluginFunction):
"""
This is a special case of the flood_fill algorithm that is designed
to remove dark borders produced by photocopiers or flatbed scanners
around the border of the image.
"""
self_type = ImageType([ONEBIT])
class highlight(PluginFunction):
"""
Highlights all pixels on an image in given color that are black in the
passed connected component. The connected component can be part of a
different image, it must however lie within the image region.
*cc*
A one-bit connected component from the image
*color*
An color value (RGBPixel, 0-255, or [0|1], depending on the image
type) used to color the *cc*.
"""
self_type = ImageType([RGB,GREYSCALE,ONEBIT])
args = Args([ImageType([ONEBIT], "cc"), Pixel("color")])
def __doc_example1__(images):
image = images[ONEBIT]
ccs = image.cc_analysis()
rgb = image.to_rgb()
rgb.highlight(ccs[0], RGBPixel(255, 0, 128))
return rgb
doc_examples = [__doc_example1__]
class DrawModule(PluginModule):
cpp_headers = ["draw.hpp"]
category = "Draw"
functions = [draw_line, draw_bezier, draw_marker,
draw_hollow_rect, draw_filled_rect, flood_fill,
remove_border, highlight, draw_circle,
draw_text]
author = "Michael Droettboom"
url = "http://gamera.sourceforge.net/"
module = DrawModule()
|