""" module to work with LTANN62N.dll """ import clr as _clr _clr.AddReference('System.Drawing') import System.Drawing as _drw import ctypes as _ct import os.path as _os_path import ImporterConst as _const #for dll_path in _const.AnnDllFiles: # _clr.AddReferenceToFileAndPath(dll_path) #import Leadtools.Annotations as _ann _ann = None # don't import Leadtools #_lt = _ct.windll.LoadLibrary(_const.LtannDllFile) _lt = None # don't load DLL #_codecs = _ann.AnnCodecs() _codecs = None # don't create _codecs = _ann.AnnCodecs() def _replaceExt(filename, new_ext): result = _os_path.splitext(filename)[0] + new_ext return result def annFileExists(filename): """ for the given filename returns corresponding ANN file, if it exists; else returns empty string """ ann_name = _replaceExt(filename, _const.LtannExt) if _os_path.exists(ann_name): return ann_name else: return '' def _getImageDimensions(filename): with _drw.Image.FromFile(filename) as image: return image.Width, image.Height def convertAnnToVector(ann_filename, src_imagename, dst_imagename): """ converts ANN to other annotation format (WMF); returns true to signal success """ width, height = _getImageDimensions(src_imagename) dst_filename = _replaceExt(dst_imagename, _const.AnnExt) with _ann.AnnContainer() as container: _codecs.Load(ann_filename, container, 1) container.Bounds = _ann.AnnRectangle(0.0, 0.0, float(width), float(height)) _codecs.Save(dst_filename, container, _ann.AnnCodecsFormat.Wmf, 1, _ann.AnnCodecsSavePageMode.Overwrite) return True class _POINT(_ct.Structure): _fields_ = [("x", _ct.c_int), ("y", _ct.c_int)] def __str__(self): return '({}, {})'.format(self.x, self.y) class _RECT(_ct.Structure): _fields_ = [("upperleft", _POINT), ("lowerright", _POINT)] def __str__(self): return '({}, {})'.format(self.upperleft, self.lowerright) class _RECT_DBL(_ct.Structure): _fields_ = [("left", _ct.c_double), ("top", _ct.c_double), ("right", _ct.c_double), ("bottom", _ct.c_double)] def __str__(self): return '({}, {}, {}, {})'.format(self.left, self.top, self.right, self.bottom) def _fit(srcW, srcH, dstW, dstH): if srcW == 0: srcW = dstW if srcH == 0: srcH = dstH ratio = float(min(float(dstW) / float(srcW), float(dstH) / float(srcH))) new_w = float(srcW) * ratio new_h = float(srcH) * ratio return ratio, new_w, new_h def convertAnnImage(srcImageName, srcAnnName, orgWidth, orgHeight, dstImageName, imgType='jpg'): hresult = [] hr = lambda x: hresult.append(x) # weeeeeee with _drw.Image.FromFile(srcImageName) as image, _drw.Graphics.FromImage(image) as g: hdc = 0 ann_handle = _ct.c_uint32(0) null_ptr = _ct.c_uint32(0) img_rect = _RECT((0, 0), (image.Width, image.Height)) ann_rect = _RECT_DBL(0.0, 0.0, 0.0, 0.0) img_ratio, img_w, img_h = _fit(image.Width, image.Height, orgWidth, orgHeight) try: hr(_lt.L_AnnLoad(_ct.c_char_p(srcAnnName), _ct.byref(ann_handle), null_ptr)) hr(_lt.L_AnnGetRect(ann_handle, _ct.byref(ann_rect), null_ptr)) ann_ratio, ann_w, ann_h = _fit(abs(ann_rect.right - ann_rect.left), abs(ann_rect.bottom - ann_rect.top), orgWidth, orgHeight) final_ratio = ann_ratio / img_ratio hr(_lt.L_AnnSetScalarX(ann_handle, _ct.c_double(final_ratio), _ct.c_uint32(0))) # process only the specified object hr(_lt.L_AnnSetScalarY(ann_handle, _ct.c_double(final_ratio), _ct.c_uint32(0))) offsetX = -((ann_w - img_w) / 2.0) offsetY = -((ann_h - img_h) / 2.0) hr(_lt.L_AnnSetOffsetX(ann_handle, _ct.c_double(offsetX), _ct.c_uint32(0))) hr(_lt.L_AnnSetOffsetY(ann_handle, _ct.c_double(offsetY), _ct.c_uint32(0))) hdc = g.GetHdc() hr(_lt.L_AnnDraw(hdc, _ct.byref(img_rect), ann_handle)) finally: if hdc != 0: g.ReleaseHdc(hdc) if ann_handle.value != 0: hr(_lt.L_AnnDestroy(ann_handle, 0)) image.Save(dstImageName, _drw.Imaging.ImageFormat.Jpeg if imgType == 'jpg' else _drw.Imaging.ImageFormat.Png) return (len(hresult) > 0) and all(map(lambda x: bool(x == 1), hresult))