def apply_filter(self, image, _config): highgui.cvQueryFrame(self.camera) im = highgui.cvQueryFrame(self.camera) pilwebcam_image = opencv.adaptors.Ipl2PIL(im) pilwebcam_image.thumbnail( ( int((pilwebcam_image.size[0] / 100.0) * _config['width']), int((pilwebcam_image.size[1] / 100.0) * _config['height']) ) ) ## Border from ImageDraw import Draw drawing = Draw(pilwebcam_image) # top line drawing.line(((0,0), (pilwebcam_image.size[0], 0)), fill = _config['border_colour']) # left line drawing.line(((0,0), (0, pilwebcam_image.size[1])), fill = _config['border_colour']) # right line drawing.line(((pilwebcam_image.size[0]-1,0), (pilwebcam_image.size[0]-1, pilwebcam_image.size[1])), fill = _config['border_colour']) # bottow line drawing.line(((0, pilwebcam_image.size[1]-1), (pilwebcam_image.size[0], pilwebcam_image.size[1]-1)), fill = _config['border_colour']) image.paste(pilwebcam_image, (10,10)) return image
def apply_filter(self, image, _config): highgui.cvQueryFrame(self.camera) im = highgui.cvQueryFrame(self.camera) pilwebcam_image = opencv.adaptors.Ipl2PIL(im) pilwebcam_image.thumbnail( (int((pilwebcam_image.size[0] / 100.0) * _config['width']), int((pilwebcam_image.size[1] / 100.0) * _config['height']))) ## Border from ImageDraw import Draw drawing = Draw(pilwebcam_image) # top line drawing.line(((0, 0), (pilwebcam_image.size[0], 0)), fill=_config['border_colour']) # left line drawing.line(((0, 0), (0, pilwebcam_image.size[1])), fill=_config['border_colour']) # right line drawing.line(((pilwebcam_image.size[0] - 1, 0), (pilwebcam_image.size[0] - 1, pilwebcam_image.size[1])), fill=_config['border_colour']) # bottow line drawing.line(((0, pilwebcam_image.size[1] - 1), (pilwebcam_image.size[0], pilwebcam_image.size[1] - 1)), fill=_config['border_colour']) image.paste(pilwebcam_image, (10, 10)) return image
def roll_date_time(data, out, hour_parts=4, lines=4): bg_color = (255, 255, 255) line_color = (220, 220, 220) color=(32,32,255) def date_value(event): return (event.date() - epoch).days def date_coords(event): time_value = event.hour * hour_parts + event.minute * hour_parts / 60 return (date_value(event) - start_value, height - time_value - 1) epoch = date(1970, 1, 1) # find boundarys start = min(data) end = max(data) # calculate value of boundarys start_value = date_value(start) end_value = date_value(end) # calculate geometry width = end_value - start_value + 1 height = 24 * hour_parts # building the image img = Image.new("RGB", (width, height + 10), bg_color) draw = Draw(img) # drawing horizontal (time) lines to enhance readability for line in xrange(lines): y = (height / lines) * (line + 1) draw.line([(0, y), (width - 1, y)], line_color) # drawing vertical (date) lines and captions for month_start in iter_months(start, end): x, _ = date_coords(month_start) draw.line([(x, 0), (x, height - 1)], line_color) draw.text((x + 3, height), month_start.strftime("%m"), line_color) # plotting actual data for event in data: img.putpixel(date_coords(event), color) img.save(out, 'png')
def apply_filter(self, image, _config): # OpenCV sucks, it keeps all frames cached so when I query them # they are old frames. I delete the camera object and get a new one # so i can query the current frame only. # this is the only solution i could work out to this problem. del self.camera self.camera = cv.CaptureFromCAM(-1) im = cv.QueryFrame(self.camera) # HEY HERE IS SOMETHING REALLY FUNNY # OPENCV USES BGR FOR IT'S INTERNAL IMAGE FORMAT # WOW # SO STANDARD GUYS # FAN-F*****G-TASTIC # I DIDN'T TOTALLY JUST SPEND THREE HOURS WORKING THAT OUT # GREAT STUFF # YOUR OWN EXAMPLE CODE FOR CONVERTING TO A PIL IMAGE DOESN'T TAKE THIS INTO ACCOUNT AT ALL # WTFFFFFFFFFFF pilwebcam_image = Image.fromstring("RGB", cv.GetSize(im), im.tostring()[::-1]).rotate(180) pilwebcam_image = pilwebcam_image.resize( (int((pilwebcam_image.size[0] / 100.0) * _config['width']), int((pilwebcam_image.size[1] / 100.0) * _config['height'])), Image.ANTIALIAS) ## Border drawing = Draw(pilwebcam_image) # top line drawing.line(((0, 0), (pilwebcam_image.size[0], 0)), fill=_config['border_colour']) # left line drawing.line(((0, 0), (0, pilwebcam_image.size[1])), fill=_config['border_colour']) # right line drawing.line(((pilwebcam_image.size[0] - 1, 0), (pilwebcam_image.size[0] - 1, pilwebcam_image.size[1])), fill=_config['border_colour']) # bottow line drawing.line(((0, pilwebcam_image.size[1] - 1), (pilwebcam_image.size[0], pilwebcam_image.size[1] - 1)), fill=_config['border_colour']) image.paste(pilwebcam_image, (10, 10)) return image
def apply_filter(self, image, _config): # OpenCV sucks, it keeps all frames cached so when I query them # they are old frames. I delete the camera object and get a new one # so i can query the current frame only. # this is the only solution i could work out to this problem. del self.camera self.camera = cv.CaptureFromCAM(-1) im = cv.QueryFrame(self.camera) # HEY HERE IS SOMETHING REALLY FUNNY # OPENCV USES BGR FOR IT'S INTERNAL IMAGE FORMAT # WOW # SO STANDARD GUYS # FAN-F*****G-TASTIC # I DIDN'T TOTALLY JUST SPEND THREE HOURS WORKING THAT OUT # GREAT STUFF # YOUR OWN EXAMPLE CODE FOR CONVERTING TO A PIL IMAGE DOESN'T TAKE THIS INTO ACCOUNT AT ALL # WTFFFFFFFFFFF pilwebcam_image = Image.fromstring("RGB", cv.GetSize(im), im.tostring()[::-1]).rotate(180) pilwebcam_image = pilwebcam_image.resize( ( int((pilwebcam_image.size[0] / 100.0) * _config['width']), int((pilwebcam_image.size[1] / 100.0) * _config['height']) ), Image.ANTIALIAS ) ## Border drawing = Draw(pilwebcam_image) # top line drawing.line(((0,0), (pilwebcam_image.size[0], 0)), fill = _config['border_colour']) # left line drawing.line(((0,0), (0, pilwebcam_image.size[1])), fill = _config['border_colour']) # right line drawing.line(((pilwebcam_image.size[0]-1,0), (pilwebcam_image.size[0]-1, pilwebcam_image.size[1])), fill = _config['border_colour']) # bottow line drawing.line(((0, pilwebcam_image.size[1]-1), (pilwebcam_image.size[0], pilwebcam_image.size[1]-1)), fill = _config['border_colour']) image.paste(pilwebcam_image, (10,10)) return image
monthList.remove(sxMin) monthList.remove(sxMax) sxMinX = (sxLength/sxMax) * sxMin sxWidth = 75 # end injection injo1 = injo1.rotate(90, expand=True) injow, injoh = injo1.size out.paste(injo1, (sxRight-(injow/2), sxBottom-injoh-sxWidth/2), mask=injo1) # start injection out.paste(injo1, (sxLeft + sxMinX - (injow/2), sxBottom-injoh-sxWidth/2), mask=injo1) # length of time since diagnosis/symptoms draw.line((sxLeft,sxBottom, sxRight, sxBottom), fill=green, width=sxWidth) text = "0" text_w, text_h = smallFont.getsize(text) draw.text((sxLeft - text_w/2, sxBottom+sxWidth/2), text, fill=cream, font=smallFont) text = str(sxMax) text_w, text_h = smallFont.getsize(text) draw.text((sxRight - text_w/2, sxBottom+sxWidth/2), text, fill=cream, font=smallFont) text = "Months from symptom onset" text_w, text_h = smallFont.getsize(text) draw.text((sxLeft, sxBottom-text_h/2), text, fill=yellow, font=smallFont) smallInjoh = injoh/2 smallInjow = injow/2
def EllipseFitting(self,mode): #img = Image.open(self.filename) frames = [self.image] width, height = self.image.size white = 0 ful=width*height for i in range(height): for j in range (width): rgb = self.image.getpixel((j,i)) if rgb!=0: white=white+1 self.por=int((white/ful)*170) #if self.por<100: # self.por = 200 #elif self.por>400: # self.por=400 print "\t==== all pixels = ", ful, " ====" print "\t==== white pixels = ", white, " ====" print "\t==== threshold sensitivity is = ", self.por, " ====" print "\t==== Begining find clusters... ====" img = self.image.convert("RGB") frames = [img] points_to_track = self.ClaNum k_or_guess = points_to_track X, Y = frames[0].size try: for im, n in zip(frames, count()): points = array([array([p.x, p.y], "f") for p in self.red_points(list(im.getdata()), X, Y)]) if len(points) > 0: codebook, distortion = vq.kmeans(points, k_or_guess) draw = Draw(im) assert points_to_track == len(codebook) delt=int(distortion) res = self.UnionClasters(codebook) self.Clusters = res for p in res: draw.line((p[0]-10, p[1]-10) + (p[0]+10, p[1]+10), fill=(255, 0, 0)) draw.line((p[0]-10, p[1]+10) + (p[0]+10, p[1]-10), fill=(255, 0, 0)) draw.ellipse((p[0]-(int(self.por/2)), p[1]-(int(self.por/2)), p[0]+(int(self.por/2)), p[1]+(int(self.por/2))), None, "blue") print finally: if mode=='s': frames[0].show()
class DesignDraw: def __init__(self, width = 300, height = 300, alpha = 30): # сохраняем высоту и ширину картинки self.width = width self.height = height self.im = Image.new('RGB', (width, height), (255, 255, 255)) # создаем пустое изображение self.draw = Draw(self.im) self.font = ImageFont.truetype(dirname(abspath(__file__)) + '/GOST_A.TTF', 17) self.alpha = alpha # функция преобразующая сантиметры в пиксели def sm2px(self, sm): return (sm * self.alpha) # функция преобразующая координаты из декартовой # в экранную систему координат # координаты в пикселях def dec2screen(self, x, y): return (x, self.height - y) def Show(self): invert_im = ImageOps.invert(self.im) bbox = invert_im.getbbox() self.im = self.im.crop((bbox[0] -5, bbox[1] - 5, bbox[2] + 5, bbox[3] + 5)) self.im.show() def Crop(self): invert_im = ImageOps.invert(self.im) bbox = invert_im.getbbox() self.im = self.im.crop((bbox[0] -5, bbox[1] - 5, bbox[2] + 5, bbox[3] + 5)) def Size(self): """ Возвращает (ширину, высоту) изображения. """ return self.im.size def Resize(self, width = 400, height = 400): self.im = self.im.resize((width, height), Image.ANTIALIAS) # функция рисующая точку # принимаемые координаты в декартовой системе координат # в сантиметрах def Dot(self, x, y, radius = 5): halfr = radius / 2 x = self.sm2px(x) y = self.sm2px(y) (x, y) = self.dec2screen(x, y) self.draw.ellipse((x - halfr, y - halfr, x + halfr, y + halfr), fill=(0, 0, 0, 0)) # функция рисующая прямоугольник # принимаемые координаты в декартовой системе координат # в сантиметрах # x, y - нижний левый угол def Rect(self, x, y, width, height): x = self.sm2px(x) y = self.sm2px(y) width = self.sm2px(width) height = self.sm2px(height) (x, y) = self.dec2screen(x, y) self.draw.rectangle((x, y - height, x + width, y), outline="black") # функция рисующая линию с незакрашенной стрелкой в конце # принимаемые координаты в декартовой системе # в сантиметрах def LineStrokeArrow(self, x1, y1, x2, y2, headlen = 10, headwid = 6): [x1, y1, x2, y2] = [self.sm2px(x1), self.sm2px(y1), self.sm2px(x2), self.sm2px(y2)] [x1, y1] = self.dec2screen(x1, y1) [x2, y2] = self.dec2screen(x2, y2) angle = atan2(y2 - y1, x2 - x1) self.draw.line((x1, y1, x2, y2), fill="black") # координаты левого крыла (lwx, lwy) = (x2 - headlen * cos(angle - pi / headwid), y2 - headlen * sin(angle - pi / headwid)) # координаты правого крыла (rwx, rwy) = (x2 - headlen * cos(angle + pi / headwid), y2 - headlen * sin(angle + pi / headwid)) # рисуем крылья self.draw.line((x2, y2, lwx, lwy), fill="black") self.draw.line((x2, y2, rwx, rwy), fill="black") # функция рисующая линию с закрашенной стрелкой в конце # принимаемые координаты в декартовой системе # в сантиметрах def LineFillArrow(self, x1, y1, x2, y2, headlen = 10, headwid = 6, width = 1): [x1, y1, x2, y2] = [self.sm2px(x1), self.sm2px(y1), self.sm2px(x2), self.sm2px(y2)] [x1, y1] = self.dec2screen(x1, y1) [x2, y2] = self.dec2screen(x2, y2) angle = atan2(y2 - y1, x2 - x1) self.draw.line((x1, y1, x2, y2), fill="black", width = width) # координаты левого крыла (lwx, lwy) = (x2 - headlen * cos(angle - pi / headwid), y2 - headlen * sin(angle - pi / headwid)) # координаты правого крыла (rwx, rwy) = (x2 - headlen * cos(angle + pi / headwid), y2 - headlen * sin(angle + pi / headwid)) lines = [(x2, y2), (lwx, lwy), (rwx, rwy)] self.draw.polygon(lines, fill="black") # функция рисующая линию с закрашенными стрелками на концах # принимаемые координаты в декартовой системе # в сантиметрах def Line2FillArrow(self, x1, y1, x2, y2, headlen = 5, headwid = 5): [x1, y1, x2, y2] = [self.sm2px(x1), self.sm2px(y1), self.sm2px(x2), self.sm2px(y2)] [x1, y1] = self.dec2screen(x1, y1) [x2, y2] = self.dec2screen(x2, y2) angle = atan2(y2 - y1, x2 - x1) self.draw.line((x1, y1, x2, y2), fill="black") # первая стрелка # координаты левого крыла (lwx, lwy) = (x2 - headlen * cos(angle - pi / headwid), y2 - headlen * sin(angle - pi / headwid)) # координаты правого крыла (rwx, rwy) = (x2 - headlen * cos(angle + pi / headwid), y2 - headlen * sin(angle + pi / headwid)) lines = [(x2, y2), (lwx, lwy), (rwx, rwy)] self.draw.polygon(lines, fill="black") # вторая стрелка # координаты левого крыла (lwx, lwy) = (x1 + headlen * cos(angle - pi / headwid), y1 + headlen * sin(angle - pi / headwid)) # координаты правого крыла (rwx, rwy) = (x1 + headlen * cos(angle + pi / headwid), y1 + headlen * sin(angle + pi / headwid)) lines = [(x1, y1), (lwx, lwy), (rwx, rwy)] self.draw.polygon(lines, fill="black") # функция рисующая линию с незакрашенной стрелкой в конце # принимаемые координаты в декартовой системе # в сантиметрах def Line(self, x1, y1, x2, y2): [x1, y1, x2, y2] = [self.sm2px(x1), self.sm2px(y1), self.sm2px(x2), self.sm2px(y2)] [x1, y1] = self.dec2screen(x1, y1) [x2, y2] = self.dec2screen(x2, y2) self.draw.line((x1, y1, x2, y2), fill="black") # функция рисующая текст # принимаемые координаты в декартовой системе # в сантиметрах def Text(self, text, x, y): [x, y] = [self.sm2px(x), self.sm2px(y)] [x, y] = self.dec2screen(x, y) self.draw.text((x + 4, y - 18), text, font=self.font, fill="black") def BottomAlignText(self, text, x, y): [x, y] = [self.sm2px(x), self.sm2px(y)] [x, y] = self.dec2screen(x, y) (textw, texth) = self.font.getsize(text) self.draw.text((x - (textw / 2.0), y), text, font=self.font, fill="black") def LeftAlignText(self, text, x, y): [x, y] = [self.sm2px(x), self.sm2px(y)] [x, y] = self.dec2screen(x, y) (textw, texth) = self.font.getsize(text) self.draw.text((x - textw - 2, y - (texth / 2.0)), text, font=self.font, fill="black") def RightAlignText(self, text, x, y): [x, y] = [self.sm2px(x), self.sm2px(y)] [x, y] = self.dec2screen(x, y) (textw, texth) = self.font.getsize(text) self.draw.text((x + 2, y - (texth / 2.0)), text, font=self.font, fill="black") def TopAlignText(self, text, x, y): [x, y] = [self.sm2px(x), self.sm2px(y)] [x, y] = self.dec2screen(x, y) (textw, texth) = self.font.getsize(text) self.draw.text((x - (textw / 2.0), y - texth), text, font=self.font, fill="black") def Polygon(self, xy): screen_xy = [self.dec2screen(self.sm2px(x), self.sm2px(y)) for x, y in xy] self.draw.polygon(screen_xy, outline="black") def TextSize(self, text): return self.font.getsize(text) def Circle(self, x, y, radius): radius = self.sm2px(radius) halfr = radius / 2 x = self.sm2px(x) y = self.sm2px(y) (x, y) = self.dec2screen(x, y) self.draw.ellipse((x - halfr, y - halfr, x + halfr, y + halfr), fill=None, outline=(0, 0, 0, 0))