def __call__(self, size=10): from gamera.core import Image, GREYSCALE, Dim result = Image((0, 0), Dim(self.ncols * size, self.nrows * size), GREYSCALE) half_size = size / 2 for r in xrange(self.nrows): for c in xrange(self.ncols): direction = self.get((c, r)) if self.get((c, r)) != 0: center_r = r * size + (size / 2) size_c = c * size subimage = result.subimage(Point(r*size, c*size), Dim(size, size)) dir = half_size * tan(direction) subimage.draw_line((size_c, center_r - dir), (size_c + size, center_r + dir), 128) return result
def __doc_example1__(images): from random import randint, seed from gamera.core import Image, Dim seed(0) 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 Exception: pass return image
def __call__(array, offset=(0, 0)): from gamera.plugins import _string_io from gamera.core import Dim pixel_type = from_numeric._check_input(array) return _string_io._from_raw_string( offset, Dim(array.shape[1], array.shape[0]), pixel_type, DENSE, array.tostring())
def extend(self, Ex, Ey, img): ul_y = max(0, self.rect.ul_y - Ey) ul_x = max(0, self.rect.ul_x - Ex) lr_y = min(img.lr_y, self.rect.lr_y + Ey) lr_x = min(img.lr_x, self.rect.lr_x + Ex) nrows = lr_y - ul_y + 1 ncols = lr_x - ul_x + 1 self.rect = Rect(Point(ul_x, ul_y), Dim(ncols, nrows))
def __call__(array, offset=None): from gamera.plugins import _string_io from gamera.core import Point, Dim if offset is None: offset = Point(0, 0) pixel_type = from_numarray._check_input(array) return _string_io._from_raw_string( offset, Dim(array.shape[1], array.shape[0]), pixel_type, DENSE, array.tostring())
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
def __doc_example1__(images): from random import randint, seed from gamera.core import Image, Dim seed(0) 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
def Fudge(o, amount=FUDGE_AMOUNT): # For rectangles, just return a new rectangle that is slightly larger if isinstance(o, Rect): return Rect(Point(int(o.ul_x - amount), int(o.ul_y - amount)), Dim(int(o.ncols + amount * 2), int(o.nrows + amount * 2))) # For integers, return one of our "fudge number proxies" elif isinstance(o, int): return FudgeInt(o, amount) elif isinstance(o, float): return FudgeFloat(o, amount)
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
def __call__(image, offset=None): from gamera.plugins import _string_io from gamera.core import Dim, Point, RGBPixel if offset is None: offset = Point(0, 0) if isinstance(image, cv.cvmat): imgcv = cv.CloneMat(image) cv.CvtColor(image, imgcv, cv.CV_BGR2RGB) return _string_io._from_raw_string(offset, Dim(imgcv.cols, imgcv.rows), RGB, DENSE, imgcv.tostring()) elif isinstance(image, cv.iplimage): imgcv = cv.CreateImage(cv.GetSize(image), image.depth, image.channels) cv.CvtColor(image, imgcv, cv.CV_BGR2RGB) return _string_io._from_raw_string(offset, Dim(imgcv.width, imgcv.height), RGB, DENSE, imgcv.tostring()) else: raise TypeError("Image must be of type cv.cvmat or cv.iplimage")
def __call__(image, offset=None): from gamera.plugins import _string_io from gamera.core import Dim, Point typecode = image.mode if offset is None: offset = Point(0, 0) if typecode in _inverse_modes: pixel_type = _inverse_modes[typecode] else: raise ValueError( "Only RGB and 8-bit Greyscale 'L' PIL image modes are supported." ) try: return _string_io._from_raw_string( offset, Dim(image.size[0], image.size[1]), pixel_type, DENSE, image.tobytes()) except Exception: # for compatibility with pil 1.1.7 and earlier return _string_io._from_raw_string( offset, Dim(image.size[0], image.size[1]), pixel_type, DENSE, image.tostring())
def __call__(image, offset=None): from gamera.plugins import _string_io from gamera.core import Dim, Point typecode = image.mode if offset is None: offset = Point(0, 0) if _inverse_modes.has_key(typecode): pixel_type = _inverse_modes[typecode] else: raise ValueError( "Only RGB and 8-bit Greyscale 'L' PIL image modes are supported." ) return _string_io._from_raw_string( offset, Dim(image.size[0], image.size[1]), pixel_type, DENSE, image.tostring())
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)
def expand_ccs(image,ccs,Ex,Ey): # two helper functions for merging rectangles def find_intersecting_rects(glyphs, index): g = glyphs[index] inter = [] for i in range(len(glyphs)): if i == index: continue if g.intersects(glyphs[i]): inter.append(i) return inter def list_union_rects(big_rects): current = 0 rects = big_rects while(1): inter = find_intersecting_rects(rects, current) if len(inter): g = rects[current] new_rects = [g] for i in range(len(rects)): if i == current: continue if i in inter: g.union(rects[i]) else: new_rects.append(rects[i]) rects = new_rects current = 0 else: current += 1 if(current >= len(rects)): break return rects # the actual plugin from gamera.core import Dim, Rect, Point, Cc from gamera.plugins.image_utilities import union_images page = image # extend CC bounding boxes big_rects = [] for c in ccs: ul_y = max(0, c.ul_y - Ey) ul_x = max(0, c.ul_x - Ex) lr_y = min(page.lr_y, c.lr_y + Ey) lr_x = min(page.lr_x, c.lr_x + Ex) nrows = lr_y - ul_y + 1 ncols = lr_x - ul_x + 1 big_rects.append(Rect(Point(ul_x, ul_y), Dim(ncols, nrows))) extended_segs = list_union_rects(big_rects) # build new merged CCs tmplist = ccs[:] dellist = [] seg_ccs = [] seg_cc = [] if(len(extended_segs) > 0): label = 1 for seg in extended_segs: label += 1 for cc in tmplist: if(seg.intersects(cc)): # mark original image with segment label #self.highlight(cc, label) seg_cc.append(cc) dellist.append(cc) if len(seg_cc) == 0: continue seg_rect = seg_cc[0].union_rects(seg_cc) new_seg = Cc(image, label, seg_rect.ul, seg_rect.lr) seg_cc = [] for item in dellist: tmplist.remove(item) dellist = [] seg_ccs.append(new_seg) return seg_ccs
def get(self): from gamera.core import Dim return Dim(int(self.control_x.GetValue()), int(self.control_y.GetValue()))
def __doc_example1__(images): from gamera.core import Dim onebit = images[ONEBIT].subimage((0, 0), Dim(70, 68)) greyscale = images[GREYSCALE].to_onebit().subimage((0, 0), Dim(70, 68)) return [onebit, greyscale, onebit.and_image(greyscale, False)]