Example #1
0
def plot_sparkline_smooth(results, args):
   step = int(args.get('step', '2'))
   height = int(args.get('height', '20'))
   (dmin, dmax) = [int(x) for x in args.get('limits', '0,100').split(',')]
   im = PNGCanvas((len(results)-1)*step+4, height)
   im.color = rgb.colors('white')
   im.filledRectangle(0, 0, im.width-1, im.height-1)
   coords = zip(range(1,len(results)*step+1, step), [height - 3  - (y-dmin)/(float(dmax - dmin +1)/(height-4)) for y in results])
   im.color = rgb.colors('gray53')
   lastx, lasty = coords[0]
   for x0, y0, in coords:
     im.line(lastx, lasty, x0, y0)
     lastx, lasty = x0, y0
   min_color = rgb.colors(args.get('min-color', 'green'))
   max_color = rgb.colors(args.get('max-color', 'red'))
   last_color = rgb.colors(args.get('last-color', 'blue'))
   has_min = args.get('min-m', 'false')
   has_max = args.get('max-m', 'false')
   has_last = args.get('last-m', 'false')
   if has_min == 'true':
      min_pt = coords[results.index(min(results))]
      im.color = min_color
      im.filledRectangle(min_pt[0]-1, min_pt[1]-1, min_pt[0]+1, min_pt[1]+1)
   if has_max == 'true':
      im.color = max_color
      max_pt = coords[results.index(max(results))]
      im.filledRectangle(max_pt[0]-1, max_pt[1]-1, max_pt[0]+1, max_pt[1]+1)
   if has_last == 'true':
      im.color = last_color
      end = coords[-1]
      im.filledRectangle(end[0]-1, end[1]-1, end[0]+1, end[1]+1)
   return im.dump()
Example #2
0
def plot_sparkline_discrete(results, args, longlines=False):
    """The source data is a list of values between
      0 and 100 (or 'limits' if given). Values greater than 95 
      (or 'upper' if given) are displayed in red, otherwise 
      they are displayed in green"""
    width = int(args.get('width', '2'))
    height = int(args.get('height', '14'))
    upper = int(args.get('upper', '50'))
    below_color = args.get('below-color', 'dark gray')
    above_color = args.get('above-color', 'red')
    gap = 4
    if longlines:
        gap = 0
    im = PNGCanvas(len(results)*width-1, height)
    im.color = rgb.colors('white')
    im.filledRectangle(0, 0, im.width-1, im.height-1)

    (dmin, dmax) = [int(x) for x in args.get('limits', '0,100').split(',')]
    if dmax < dmin:
        dmax = dmin
    zero = im.height - 1
    if dmin < 0 and dmax > 0:
        zero = im.height - (0 - dmin) / (float(dmax - dmin + 1) / (height - gap))
    for (r, i) in zip(results, range(0, len(results)*width, width)):
        color = (r >= upper) and above_color or below_color
        if r < 0:
            y_coord = im.height - (r - dmin) / (float(dmax - dmin + 1) / (height - gap))
        else:
            y_coord = im.height - (r - dmin) / (float(dmax - dmin + 1) / (height - gap))
        im.color = rgb.colors(color)
        if longlines:
            im.filledRectangle(i, zero, i+width-2, y_coord)
        else:
            im.filledRectangle(i, y_coord - gap, i+width-2, y_coord)
    return im.dump()
Example #3
0
def png_sparkline(numbers, width=60, height=14, color=None, bgcolor='white'):
    if len(numbers) > width: return None
    color = png_colors.get(color) or png_colors.get('blue')
    bgcolor = png_colors.get(bgcolor) or png_colors.get('white')
    img = PNGCanvas(width, height, color=color, bgcolor=bgcolor)
    img.color = color
    _draw_series(img, numbers, width, height)
    return img.dump()
Example #4
0
def grabImage():
    windows = CGWindowListCopyWindowInfo(kCGWindowListOptionAll,
                                         kCGNullWindowID)

    for window in windows:
        #print(window)
        try:
            if window['kCGWindowName'] == 'Krunker':
                print('Krunker window found!')
                print(window)

                # get window dimensions
                x = int(window['kCGWindowBounds']['X'])
                y = int(window['kCGWindowBounds']['Y'])
                width = int(window['kCGWindowBounds']['Width'])
                height = int(window['kCGWindowBounds']['Height'])
                print('Dimensions: {}'.format((x, y, width, height)))

                center = (width // 2, height // 2)
                print('Center: {}'.format(center))

                window_id = int(window['kCGWindowNumber'])
                print('Window ID: {}'.format(window_id))

                #region = CGRectMake(x, y, width, height)
                #region = CGRectInfinite

                # get img data
                img = CGWindowListCreateImage(CGRectInfinite,
                                              kCGWindowListOptionAll,
                                              window_id, kCGWindowImageDefault)
                prov = CGImageGetDataProvider(img)
                img_data = CGDataProviderCopyData(prov)
                img_width, img_height = CGImageGetWidth(img), CGImageGetHeight(
                    img)

                # create canvas based on image data pixels
                canvas = PNGCanvas(img_width, img_height)
                for x in range(img_width):
                    for y in range(img_height):
                        offset = 4 * (
                            (img_width * int(round(y))) + int(round(x)))
                        b, g, r, a = struct.unpack_from('BBBB',
                                                        img_data,
                                                        offset=offset)
                        canvas.point(x, y, color=(r, g, b, a))

                # dump canvas to png
                with open('test.png', 'wb') as f:
                    f.write(canvas.dump())

        except:
            # handle exception
            pass
Example #5
0
 def convert_image(self, space_level):
   tile = PNGCanvas(SIZE, SIZE, bgcolor=[0xff,0xff,0xff,0])
   temp_color_scheme = []
   for i in range(self.level_max):
     temp_color_scheme.append(self.color_scheme.canvas[self.cache_levels[i]][0])
   for y in xrange(SIZE):
     for x in xrange(SIZE):
       if len(temp_color_scheme) > 0:
         tile.canvas[y][x] = [int(e) for e in temp_color_scheme[max(0, min(len(temp_color_scheme) - 1, self.scale_value(space_level[y][x])))]]
       else:
         tile.canvas[y][x] = [0,0,0,0]
   return tile
Example #6
0
 def convert_image(self, space_level):
     tile = PNGCanvas(len(space_level[0]),
                      len(space_level),
                      bgcolor=[0xff, 0xff, 0xff, 0])
     color_scheme = []
     for i in range(LEVEL_MAX):
         color_scheme.append(self.color_scheme.canvas[cache_levels[i]][0])
     for y in xrange(len(space_level[0])):
         for x in xrange(len(space_level[0])):
             #log.error(space_level[y][x])
             tile.canvas[y][x] = color_scheme[min(LEVEL_MAX - 1,
                                                  int(space_level[y][x]))]
     return tile
Example #7
0
    def create(self):
        self.img = PNGCanvas(self.width, self.height)
        self.img.color = [0xff, 0xff, 0xff, 0xff]
        #self.img.color = [0x39,0x9e,0xff,0xff]
        #self.img.verticalGradient(1,1,self.width-2, self.height-2,[0xff,0,0,0xff],[0x60,0,0xff,0x80])
        self.img.verticalGradient(1, 1, self.width - 2, self.height - 2,
                                  [0xff, 0x45, 0x45, 0xff],
                                  [0xff, 0xcb, 0x44, 0xff])

        for i in range(4):
            a = str(self.text)[i]
            self.writeText(a, i)

        return self.img.dump()
    def make_image(self):
        '''
        Creates PNG image with QR Code
        '''
        boxsize = 10 # pixels per box
        offset = 1 # boxes as border
        pixelsize = (self.getModuleCount() + offset + offset) * boxsize

        canvas = PNGCanvas(pixelsize, pixelsize)
        for row in range(self.getModuleCount()):
            for column in range(self.getModuleCount()):
                if (self.isDark(row, column) ):
                    pos_x = (column + offset) * boxsize
                    pos_y = (row + offset) * boxsize
                    canvas.filledRectangle(pos_x, pos_y, pos_x + boxsize, pos_y + boxsize)
        return canvas.dump()
Example #9
0
 def __init__(self, user, lat_north, lng_west, range_lat, range_lng):
   userinfo = UserInfo.all().filter('user ='******'t do any more math if we don't have any venues
     cur_canvas = self.color_scheme.canvas
     self.tile_img = PNGCanvas(SIZE, SIZE, bgcolor=cur_canvas[len(cur_canvas) - 1][0]) #NOTE the last index should be safe here, but for users with negative level_max's, self.cache_levels was an empty list and thus this was erroring
Example #10
0
def plot_error(results, args):
    im = PNGCanvas(40, 15)
    im.color = rgb.colors['red']
    im.line(0, 0, im.width - 1, im.height - 1)
    im.line(0, im.height - 1, im.width - 1, 0)
    return im.dump()
# -*- coding: utf-8 -*-
"""
Created on 2014-09-
Last edited on 2014-09-

@author: Ew3n
"""

from pngcanvas import PNGCanvas
import math

HEIGHT = 500
WIDTH = int(HEIGHT * (16 / 9))

canvas = PNGCanvas(WIDTH,
                   HEIGHT,
                   bgcolor=(255, 255, 255, 255),
                   color=(0, 0, 0, 255))


def get_point(x, y):
    """
    Return (Red, Green, Blue, Alpha) for the given (x, y) point.
    """
    R = (math.sin(x / 50)**2) * 0xFF
    G = (math.cos(y / 50)**2) * 0xFF
    B = (math.sin((x - y) / 50)**2) * 0xFF
    A = 0xFF
    return (int(R), int(G), int(B), int(A))


for x in range(WIDTH):
Example #12
0
        # Unpack data from string into Python'y integers
        b, g, r, a = struct.unpack_from(data_format, self._data, offset=offset)

        # Return BGRA as RGBA
        return (r, g, b, a)


def get_screenshot():
    sp = ScreenPixel()
    sp.capture()
    print(sp.width, sp.height)
    print(sp.pixel(0, 0))
# 这里是解析像素点色彩
    im = Image.frombytes("RGBA", (sp.width, sp.height), sp._data)
    b, g, r, a = im.split()
    im = Image.merge("RGBA", (r, g, b, a))
    return im

if __name__ == "__main__":
  sp = ScreenPixel()
# 设置区域截图(截图区域必须符合1024的规则,不然图像会异常)
  region = CG.CGRectMake(64, 64, 128, 128)
  sp.capture(region=region)
  c = PNGCanvas(sp.width, sp.height)
  for x in range(sp.width):
    for y in range(sp.height):
      c.point(x, y, color = sp.pixel(x, y))

# 这里是保存,如果不需要就可以不用保存,区域越大,保存时间越长
  with open("test.png", "wb") as f:
    f.write(c.dump())
Example #13
0
#!/usr/bin/python
# Gets partial screenshot and saves it into PNG file.
import Quartz.CoreGraphics as cg
region = cg.CGRectMake(
    0, 0, 400, 400)  # You can also use cg.CGRectInfinite for the full screen.
image = cg.CGWindowListCreateImage(region, cg.kCGWindowListOptionOnScreenOnly,
                                   cg.kCGNullWindowID,
                                   cg.kCGWindowImageDefault)
prov = cg.CGImageGetDataProvider(image)
img_data = cg.CGDataProviderCopyData(prov)
img_width, img_height = cg.CGImageGetWidth(image), cg.CGImageGetHeight(image)

from pngcanvas import PNGCanvas
import struct
canvas = PNGCanvas(img_width, img_height)
for x in range(img_width):
    for y in range(img_height):
        # Calculate offset, based on http://www.markj.net/iphone-uiimage-pixel-color/
        offset = 4 * ((img_width * int(round(y))) + int(round(x)))
        # Pixel data is unsigned char (8bit unsigned integer), and there are for (blue,green,red,alpha).
        # Unpack data from string into Python'y integers.
        b, g, r, a = struct.unpack_from("BBBB", img_data, offset=offset)
        # Assign BGRA color as RGBA.
        canvas.point(x, y, color=(r, g, b, a))

with open("test.png", "wb") as f:
    f.write(canvas.dump())