Пример #1
0
def saveImage(data, nodeAllocations, opts):
  global WIDTH
  global HEIGHT
  global PAGESIZE

  # Collapse "step" rows that are completely empty.
  step = 4
  collapser = numpy.zeros(WIDTH*HEIGHT, numpy.uint32)
  for begin, end in ((WIDTH*y, WIDTH*y + WIDTH*step) 
                     for y in xrange(0, HEIGHT, step)):
    if any(data[begin:end]):
      collapser[begin:end] = 1
  data = data.compress(collapser)
  
  # Create the output false colors picture. Rescale the values in the
  # data array so that they are normalised to 245 and convert the array
  # to 8 bit so that it can be easily used with the paletted mode.
  # Notice that we leave the last ten colors (245 - 255) to highlight
  # fragmented memory allocations coming from a unique stacktrace node.
  # Determine the 10 most fragmented allocations coming from the same symbol
  # and highlight the pages they touch.
  # Reshape the array to be bidimensional rather than linear.
  data *= 245
  data /= PAGESIZE
  
  # Calculate an highlight mask to hightlight top MEM_LIVE allocations.
  if topTen[0]:
    highlightNode, highlighNodeName = topTen[4][0]
    for (pos, size) in nodeAllocations[highlightNode]:
      paintAllocation(data, pos, size, 246)
  
  WIDTH = int(sqrt(len(data)))
  data = numpy.reshape(data, (len(data)/WIDTH, WIDTH))
  HEIGHT, WIDTH = data.shape
  
  pilImage = fromarray(numpy.array(data, dtype=numpy.uint8), 'P')
  lut = [0,0,0]
  for x in (int(i / 245. * 255.) for i in range(245)):
    lut.extend([x*0.8, x*0.8 , x*0.8])
  # Extra colors are all purple.
  for x in range(10):
    lut.extend([186., 7., 17.])
  
  pilImage.putpalette(lut)
  
  d = ImageDraw(pilImage)
  for c in xrange(256):
    w, h = WIDTH*0.8 / 256, HEIGHT*0.01
    x, y = c * w+WIDTH*0.1, HEIGHT*0.9 - h
    d.rectangle((x, y, x+w, y+h), fill=c)
  pilImage = pilImage.resize((WIDTH*2, HEIGHT*2))
  pilImage.save(opts.output)
Пример #2
0
def saveImage(data, nodeAllocations, opts):
    global WIDTH
    global HEIGHT
    global PAGESIZE

    # Collapse "step" rows that are completely empty.
    step = 4
    collapser = numpy.zeros(WIDTH * HEIGHT, numpy.uint32)
    for begin, end in ((WIDTH * y, WIDTH * y + WIDTH * step)
                       for y in xrange(0, HEIGHT, step)):
        if any(data[begin:end]):
            collapser[begin:end] = 1
    data = data.compress(collapser)

    # Create the output false colors picture. Rescale the values in the
    # data array so that they are normalised to 245 and convert the array
    # to 8 bit so that it can be easily used with the paletted mode.
    # Notice that we leave the last ten colors (245 - 255) to highlight
    # fragmented memory allocations coming from a unique stacktrace node.
    # Determine the 10 most fragmented allocations coming from the same symbol
    # and highlight the pages they touch.
    # Reshape the array to be bidimensional rather than linear.
    data *= 245
    data /= PAGESIZE

    # Calculate an highlight mask to hightlight top MEM_LIVE allocations.
    if topTen[0]:
        highlightNode, highlighNodeName = topTen[4][0]
        for (pos, size) in nodeAllocations[highlightNode]:
            paintAllocation(data, pos, size, 246)

    WIDTH = int(sqrt(len(data)))
    data = numpy.reshape(data, (len(data) / WIDTH, WIDTH))
    HEIGHT, WIDTH = data.shape

    pilImage = fromarray(numpy.array(data, dtype=numpy.uint8), 'P')
    lut = [0, 0, 0]
    for x in (int(i / 245. * 255.) for i in range(245)):
        lut.extend([x * 0.8, x * 0.8, x * 0.8])
    # Extra colors are all purple.
    for x in range(10):
        lut.extend([186., 7., 17.])

    pilImage.putpalette(lut)

    d = ImageDraw(pilImage)
    for c in xrange(256):
        w, h = WIDTH * 0.8 / 256, HEIGHT * 0.01
        x, y = c * w + WIDTH * 0.1, HEIGHT * 0.9 - h
        d.rectangle((x, y, x + w, y + h), fill=c)
    pilImage = pilImage.resize((WIDTH * 2, HEIGHT * 2))
    pilImage.save(opts.output)
Пример #3
0
def draw_postblobs(postblob_img, blobs_abcde):
    """ Connect the dots on the post-blob image for the five common dots.
    """
    blob_A, blob_B, blob_C, blob_D, blob_E = blobs_abcde
    
    draw = ImageDraw(postblob_img)
    
    draw.line((blob_B.x, blob_B.y, blob_C.x, blob_C.y), fill=(0x99, 0x00, 0x00))
    draw.line((blob_D.x, blob_D.y, blob_C.x, blob_C.y), fill=(0x99, 0x00, 0x00))
    draw.line((blob_D.x, blob_D.y, blob_B.x, blob_B.y), fill=(0x99, 0x00, 0x00))
    
    draw.line((blob_A.x, blob_A.y, blob_D.x, blob_D.y), fill=(0x99, 0x00, 0x00))
    draw.line((blob_D.x, blob_D.y, blob_E.x, blob_E.y), fill=(0x99, 0x00, 0x00))
    draw.line((blob_E.x, blob_E.y, blob_A.x, blob_A.y), fill=(0x99, 0x00, 0x00))
Пример #4
0
def votecount_to_image(img, game, xpos=0, ypos=0, max_width=600):
    draw = ImageDraw(img)
    regular_font = ImageFont.truetype(settings.REGULAR_FONT_PATH, 15)
    bold_font = ImageFont.truetype(settings.BOLD_FONT_PATH, 15)

    game.template = VotecountTemplate.objects.get(id=11)
    vc = VotecountFormatter(game)
    vc.go(show_comment=False)

    (header_text, footer_text) = re.compile("\[.*?\]").sub(
        '', vc.bbcode_votecount).split("\r\n")
    (header_x_size,
     header_y_size) = draw_wordwrap_text(draw, header_text, 0, 0, max_width,
                                         bold_font)
    draw.line([0, header_y_size - 2, header_x_size, header_y_size - 2],
              fill=(0, 0, 0, 255),
              width=2)
    ypos = 2 * header_y_size

    (vc_x_size, ypos) = draw_votecount_text(draw, vc, 0, ypos, max_width,
                                            regular_font, bold_font)
    ypos += header_y_size

    (x_size, ypos) = draw_wordwrap_text(draw, footer_text, 0, ypos, max_width,
                                        regular_font)

    votes = Vote.objects.select_related(depth=2).filter(game=game,
                                                        target=None,
                                                        unvote=False,
                                                        ignored=False,
                                                        nolynch=False)
    if len(votes) > 0:
        ypos += header_y_size
        if len(votes) == 1:
            warning_text = "Warning: There is currently 1 unresolved vote.  The votecount may be inaccurate."
        else:
            warning_text = "Warning: There are currently %s unresolved votes.  The votecount may be inaccurate." % len(
                votes)

        (warning_x, ypos) = draw_wordwrap_text(draw, warning_text, 0, ypos,
                                               max_width, bold_font)
        x_size = max(x_size, warning_x)

    return (max(header_x_size, vc_x_size, x_size), ypos)
Пример #5
0
    def renderiza(self):

        print "ACAO:", "Iniciando matriz da imagem"
        #Inicializando a matriz Imagem

        self.inicializa_matriz_imagem()

        #Inicializando a matriz Rotacao
        print "ACAO:", "Iniciando matriz de rotacao"
        self.inicializa_matriz_rotacao()

        #Chama o algoritmo de Phonger
        print "ACAO:", "Executando algoritmo de phong..."
        self.algoritmo_phonger()

        #Normaliza
        print "ACAO:", "Normalizando imagem"

        #Gera imagem de saida
        print "ACAO:", "Gerando saida"

        imagem = Image.new("RGB", (int(self.parametros.camera.largura),
                                   int(self.parametros.camera.altura)))
        imagem_desenhavel = ImageDraw(imagem)

        for x in range(int(self.parametros.camera.largura)):
            for y in range(int(self.parametros.camera.altura)):
                vetor_rgb = self.__normaliza(x, y)
                imagem_desenhavel.point(
                    (x, y),
                    (int(vetor_rgb[0]), int(vetor_rgb[1]), int(vetor_rgb[2])))

        imagem.show()
        imagem.save("imagem.jpg")

        print "ACAO:", "Pronto!"
Пример #6
0
def imgblobs(img, highpass_filename=None, preblobs_filename=None, postblobs_filename=None):
    """ Extract bboxes of blobs from an image.
    
        Assumes blobs somewhere in the neighborhood of 0.25" or so
        on a scan not much smaller than 8" on its smallest side.
        
        Each blob is a bbox: (xmin, ymin, xmax, ymax)
    """
    thumb = img.copy().convert('L')
    thumb.thumbnail((1500, 1500), ANTIALIAS)
    
    # needed to get back up to input image size later.
    scale = float(img.size[0]) / float(thumb.size[0])
    
    # largest likely blob size, from scan size, 0.25", and floor of 8" for print.
    maxdim = min(*img.size) * 0.25 / 8.0
    
    # smallest likely blob size, wild-ass-guessed.
    mindim = 8
    
    thumb = autocontrast(thumb)
    thumb = lowpass(thumb, 1)
    thumb = highpass(thumb, 16)
    
    if highpass_filename:
        thumb.save(highpass_filename)
    
    thumb = thumb.point(lambda p: (p < 116) and 0xFF or 0x00)
    thumb = thumb.filter(MinFilter(5)).filter(MaxFilter(5))
    
    if preblobs_filename:
        thumb.save(preblobs_filename)
    
    ident = img.copy().convert('L').convert('RGB')
    draw = ImageDraw(ident)
    
    blobs = []
    
    for (xmin, ymin, xmax, ymax, pixels) in detect(thumb):
    
        coverage = pixels / float((1 + xmax - xmin) * (1 + ymax - ymin))
        
        if coverage < 0.7:
            # too spidery
            continue
        
        xmin *= scale
        ymin *= scale
        xmax *= scale
        ymax *= scale
        
        blob = Blob(xmin, ymin, xmax, ymax, pixels)
        
        if blob.w < mindim or blob.h < mindim:
            # too small
            continue
        
        if blob.w > maxdim or blob.h > maxdim:
            # too large
            continue
        
        if max(blob.w, blob.h) / min(blob.w, blob.h) > 2:
            # too stretched
            continue
        
        draw.rectangle(blob.bbox, outline=(0xFF, 0, 0))
        draw.text(blob.bbox[2:4], str(len(blobs)), fill=(0x99, 0, 0))

        blobs.append(blob)
    
    if postblobs_filename:
        ident.save(postblobs_filename)
    
    return blobs
Пример #7
0
def imgblobs(img,
             highpass_filename=None,
             preblobs_filename=None,
             postblobs_filename=None):
    """ Extract bboxes of blobs from an image.
    
        Assumes blobs somewhere in the neighborhood of 0.25" or so
        on a scan not much smaller than 8" on its smallest side.
        
        Each blob is a bbox: (xmin, ymin, xmax, ymax)
    """
    thumb = img.copy().convert('L')
    thumb.thumbnail((1500, 1500), ANTIALIAS)

    # needed to get back up to input image size later.
    scale = float(img.size[0]) / float(thumb.size[0])

    # largest likely blob size, from scan size, 0.25", and floor of 8" for print.
    maxdim = min(*img.size) * 0.25 / 8.0

    # smallest likely blob size, wild-ass-guessed.
    mindim = 8

    thumb = autocontrast(thumb)
    thumb = lowpass(thumb, 1)
    thumb = highpass(thumb, 16)

    if highpass_filename:
        thumb.save(highpass_filename)

    thumb = thumb.point(lambda p: (p < 116) and 0xFF or 0x00)
    thumb = thumb.filter(MinFilter(5)).filter(MaxFilter(5))

    if preblobs_filename:
        thumb.save(preblobs_filename)

    ident = img.copy().convert('L').convert('RGB')
    draw = ImageDraw(ident)

    blobs = []

    for (xmin, ymin, xmax, ymax, pixels) in detect(thumb):

        coverage = pixels / float((1 + xmax - xmin) * (1 + ymax - ymin))

        if coverage < 0.7:
            # too spidery
            continue

        xmin *= scale
        ymin *= scale
        xmax *= scale
        ymax *= scale

        blob = Blob(xmin, ymin, xmax, ymax, pixels)

        if blob.w < mindim or blob.h < mindim:
            # too small
            continue

        if blob.w > maxdim or blob.h > maxdim:
            # too large
            continue

        if max(blob.w, blob.h) / min(blob.w, blob.h) > 2:
            # too stretched
            continue

        draw.rectangle(blob.bbox, outline=(0xFF, 0, 0))
        draw.text(blob.bbox[2:4], str(len(blobs)), fill=(0x99, 0, 0))

        blobs.append(blob)

    if postblobs_filename:
        ident.save(postblobs_filename)

    return blobs
Пример #8
0
def draw_postblobs(postblob_img, blobs_abcde):
    """ Connect the dots on the post-blob image for the five common dots.
    """
    blob_A, blob_B, blob_C, blob_D, blob_E = blobs_abcde

    draw = ImageDraw(postblob_img)

    draw.line((blob_B.x, blob_B.y, blob_C.x, blob_C.y),
              fill=(0x99, 0x00, 0x00))
    draw.line((blob_D.x, blob_D.y, blob_C.x, blob_C.y),
              fill=(0x99, 0x00, 0x00))
    draw.line((blob_D.x, blob_D.y, blob_B.x, blob_B.y),
              fill=(0x99, 0x00, 0x00))

    draw.line((blob_A.x, blob_A.y, blob_D.x, blob_D.y),
              fill=(0x99, 0x00, 0x00))
    draw.line((blob_D.x, blob_D.y, blob_E.x, blob_E.y),
              fill=(0x99, 0x00, 0x00))
    draw.line((blob_E.x, blob_E.y, blob_A.x, blob_A.y),
              fill=(0x99, 0x00, 0x00))