Example #1
0
File: tex.py Project: LEGENDFF/mhff
def convert_tex(tex_file, png_file):
    tex = open(tex_file, 'rb')

    magic = tex.read(4)
    header = array.array('I', tex.read(12))

    constant = header[0] & 0xfff
    unknown1 = (header[0] >> 12) & 0xfff
    size_shift = (header[0] >> 24) & 0xf
    unknown2 = (header[0] >> 28) & 0xf

    mipmap_count = header[1] & 0x3f
    width = (header[1] >> 6) & 0x1fff
    height = (header[1] >> 19) & 0x1fff

    unknown3 = header[2] & 0xff
    unknown4 = (header[2] >> 8) & 0xff
    unknown5 = (header[2] >> 16) & 0x1fff

    offsets = array.array('I', tex.read(4*mipmap_count))

    if unknown4 == 11:
        pixel_data = decode_etc1(tex.read(width*height//2), width)
        image = Image.frombytes('RGBA', (width, height), pixel_data, 'raw', 'RGBA')
        image.save(png_file)
    elif unknown4 == 12:
        pixel_data = decode_etc1(tex.read(width*height), width, True)
        image = Image.frombytes('RGBA', (width, height), pixel_data, 'raw', 'RGBA')
        image.save(png_file)
    else:
        print('unknown format')

    tex.close()
Example #2
0
File: image.py Project: Purg/vital
    def get_pil_image(self):
        """ Get image in python friendly format
        Assumptions are that the image has byte pixels.

        :return: numpy array containing image
        :rtype: pil image
        """
        import PIL.Image as PIM
        img_first_byte = self.first_pixel_address()

        # get buffer from image
        pixels = ctypes.pythonapi.PyBuffer_FromReadWriteMemory
        pixels.argtypes = [ ctypes.c_void_p, ctypes.c_int ]
        pixels.restype = ctypes.py_object
        img_pixels = pixels( img_first_byte, self.size() )

        # determine image format from strides
        if self.depth() == 3:
            if self.d_step() == 1:
                mode = "BGR"
            elif self.d_step() < 0:
                mode = "RGB"
            else:
                raise RuntimeError("Unsupported image format.")

            pil_image = PIM.frombytes("RGB", (self.width(), self.height()), img_pixels,
                                      "raw", mode, self.h_step(), 1 )
        elif self.depth() == 1:
            pil_image = PIM.frombytes("L", (self.width(), self.height()), img_pixels,
                                      "raw", "L", self.h_step(), 1 )
        else:
            raise RuntimeError("Unsupported image depth.")

        return pil_image
Example #3
0
  def invoke(self, arg, from_tty):
    frame = gdb.selected_frame()
    val = frame.read_var(arg)
    if str(val.type.strip_typedefs()) == 'SkBitmap':
      pixels = val['fPixels']
      row_bytes = val['fRowBytes']
      info = val['fInfo']
      width = info['fWidth']
      height = info['fHeight']
      color_type = info['fColorType']
      alpha_type = info['fAlphaType']

      process = gdb.selected_inferior()
      memory_data = process.read_memory(pixels, row_bytes * height)
      size = (width, height)
      image = None
      # See Unpack.c for the values understood after the "raw" parameter.
      if color_type == ColorType.bgra_8888.value:
        if alpha_type == AlphaType.unpremul.value:
          image = Image.frombytes("RGBA", size, memory_data.tobytes(),
                                  "raw", "BGRA", row_bytes, 1)
        elif alpha_type == AlphaType.premul.value:
          # RGBA instead of RGBa, because Image.show() doesn't work with RGBa.
          image = Image.frombytes("RGBA", size, memory_data.tobytes(),
                                  "raw", "BGRa", row_bytes, 1)

      if image:
        # Fails on premultiplied alpha, it cannot convert to RGB.
        image.show()
      else:
        print ("Need to add support for %s %s." % (
               str(ColorType(int(color_type))),
               str(AlphaType(int(alpha_type)))
        ))
Example #4
0
    def getDecodedColorImage(self):
        #Get the width and height info
        width, height = tuple(self.size.split(","))
        width = int(width)
        height = int(height)

        #Decode the concatinated base64 channels
        decoded = base64.decodebytes(bytearray(self.encoded, "utf-8"))

        #Split the decoded into its 3 channels
        rgb_len = int(len(decoded) / 3)
        r = decoded[0:rgb_len]
        g = decoded[rgb_len: rgb_len*2]
        b = decoded[rgb_len*2:]

        #Load the images of the 3 channels
        r_im = Image.frombytes('L',(width,height), r).load()
        g_im = Image.frombytes('L',(width,height), g).load()
        b_im = Image.frombytes('L',(width,height), b).load()


        #Make a new image with the proper dimensions
        im = Image.new(mode="RGB", size=(width, height))
        image = im.load()

        #Add the 3 channels to the new image
        for x in range(height):
            for y in range(width):
                image[x,y] = (r_im[x,y], g_im[x,y], b_im[x,y])

        return im
def read_record(database, f):
    """Load image from ETL binary

    Args:
        database (string):  'ETL8B2' or 'ETL1C'. Read the ETL documentation to add support
            for other datasets.
        f (opened file): binary file

    Returns:
        img_out (PIL image): image of the Japanese character
    """
    W, H = 64, 63
    if database == 'ETL8B2':
        s = f.read(512)
        r = struct.unpack('>2H4s504s', s)
        i1 = Image.frombytes('1', (W, H), r[3], 'raw')
        img_out = r + (i1,)
        return img_out

    elif database == 'ETL1C':
        s = f.read(2052)
        r = struct.unpack('>H2sH6BI4H4B4x2016s4x', s)
        iF = Image.frombytes('F', (W, H), r[18], 'bit', 4)
        iP = iF.convert('P')

        enhancer = ImageEnhance.Brightness(iP)
        iE = enhancer.enhance(40)
        size_add = 12
        iE = iE.resize((W + size_add, H + size_add))
        iE = iE.crop((size_add / 2,
                      size_add / 2,
                      W + size_add / 2,
                      H + size_add / 2))
        img_out = r + (iE,)
        return img_out
Example #6
0
	def set_image(self, left, right, width, height, timestamp):
		self.left_image = Image.frombytes(mode="RGB", size=(width,height), data=np.asarray(left,dtype=np.uint8))
		self.left_image.save("/dados/DIARA/log_%d_%lf_left.ppm" % (self.year, timestamp) )
		
		self.right_image = Image.frombytes(mode="RGB", size=(width,height), data=np.asarray(right,dtype=np.uint8))
		self.right_image.save("/dados/DIARA/log_%d_%lf_right.ppm" % (self.year, timestamp) )
		
		self.dataset_file.write("%d,%lf,%lf,%lf\n" % (self.year, timestamp, self.gps.x, self.gps.y) )
Example #7
0
def stereoMatchSSD(left, right, filename, path):
	print "?"
	startTime = time.time()
	DISPARITY = 79
	WINDOWSIZE = 5
	HALFSIZE = WINDOWSIZE/2
	leftWidth, leftHeight = left.size
	rightWidth, rightHeight = right.size
	leftPix = np.asarray(list(left.getdata())).reshape(leftHeight, leftWidth)
	rightPix = np.asarray(list(right.getdata())).reshape(rightHeight, rightWidth)
	leftPixPadding = np.pad(leftPix, (HALFSIZE, HALFSIZE), 'constant')
	rightPixPadding = np.pad(rightPix, (HALFSIZE, HALFSIZE), 'constant')
	leftResult = np.zeros((leftHeight, leftWidth), dtype=np.int)
	for i in xrange(HALFSIZE, leftHeight):
		print "leftSSD: ", i
		for j in xrange(HALFSIZE, leftWidth):
			min = sys.maxint
			resultDisparity = 0
			for d in xrange(0, DISPARITY + 1):
				if j - d >= HALFSIZE:
					tempSum = 0
					for k in xrange(-HALFSIZE, HALFSIZE + 1):
						for t in xrange(-HALFSIZE, HALFSIZE + 1):
							tempSum += pow(leftPixPadding[i + k][j + t] - rightPixPadding[i + k][j - d + t], 2)
					if tempSum < min:
						min = tempSum
						resultDisparity = d
				else:
					break
			leftResult[i][j] = resultDisparity
	leftResult *= 3
	result = Image.frombytes('L', (leftWidth, leftHeight), np.uint8(leftResult).tobytes())
	result.save(path + filename + "_disp1_SSD.png")
	rightResult = np.zeros((rightHeight, rightWidth), dtype=np.int)
	for i in xrange(HALFSIZE, rightHeight):
		print "SSD: ", i
		for j in xrange(HALFSIZE, rightWidth):
			min = sys.maxint
			resultDisparity = 0 
			for d in xrange(0, DISPARITY + 1):
				if j + d < rightWidth:
					tempSum = 0
					for k in xrange(-HALFSIZE, HALFSIZE + 1):
						for t in xrange(-HALFSIZE, HALFSIZE + 1):
							tempSum += pow(rightPixPadding[i + k][j + t] - leftPixPadding[i + k][j + d + t], 2)
					if tempSum < min:
						min = tempSum
						resultDisparity = d
				else:
					break
			rightResult[i][j] = resultDisparity
	rightResult *= 3
	result = Image.frombytes('L', (rightWidth, rightHeight), np.uint8(rightResult).tobytes())
	result.save(path + filename + "_disp5_SSD.png")
	endTime = time.time()
	print "SSD: ", endTime - startTime
Example #8
0
    def unpack_1(mode, rawmode, value):
        assert mode == "1"
        im = None

        if py3:
            im = Image.frombytes(mode, (8, 1), bytes([value]), "raw", rawmode, 0, 1)
        else:
            im = Image.frombytes(mode, (8, 1), chr(value), "raw", rawmode, 0, 1)

        return tuple(im.getdata())
Example #9
0
    def updateCursor(self, x, y, width, height, image, mask):
        if self.factory.nocursor:
            return

        if not width or not height:
            self.cursor = None

        self.cursor = Image.frombytes('RGBX', (width, height), image)
        self.cmask = Image.frombytes('1', (width, height), mask)
        self.cfocus = x, y
        self.drawCursor()
Example #10
0
 def _to_pil_image(self):
     if self._format_enum == _format[_RAW]:
         if self.channels == 1:
             img = _PIL_image.frombytes('L', (self._width, self._height), str(self._image_data))
         elif self.channels == 3:
             img = _PIL_image.frombytes('RGB', (self._width, self._height), str(self._image_data))
         elif self.channels == 4:
             img = _PIL_image.frombytes('RGBA', (self._width, self._height), str(self._image_data))
         else:
             raise ValueError('Unsupported channel size: ' + str(self.channels))
     else:
         img = _PIL_image.open(_StringIO.StringIO(self._image_data))
     return img
Example #11
0
def array2pil(a: np.array) -> Image:
    if a.dtype == np.dtype("B"):
        if a.ndim == 2:
            return Image.frombytes("L", (a.shape[1], a.shape[0]),
                                   a.tostring())
        elif a.ndim == 3:
            return Image.frombytes("RGB", (a.shape[1], a.shape[0]),
                                   a.tostring())
        else:
            raise Exception("bad image rank")
    elif a.dtype == np.dtype('float32'):
        return Image.frombytes("F", (a.shape[1], a.shape[0]), a.tostring())
    else:
        raise Exception("unknown image type")
Example #12
0
 def ensurepil(self, invalidate=True):
     if self.dpil is None:
         if self.dbuf is not None:
             self.dpil = Image.frombytes("RGBA", self.shape, self.dbuf, "raw", "RGBA", 0, 1)
         elif self.darr is not None:
             data = self.scaledpixelarray(0,255.999)
             buf = np.rollaxis(data,1).astype(np.uint8).tostring()
             self.dpil = Image.frombytes("RGB", self.shape, buf, "raw", "RGB", 0, -1)
         else:
             raise ValueError("No source data for conversion to PIL image")
     if invalidate:
         self.dbuf = None
         self.darr = None
         self.rangearr = None
Example #13
0
File: map.py Project: macfreek/NBT
def get_map(chunk):
    # Show an image of the chunk from above
    pixels = b""

    for z in range(16):
        for x in range(16):
            # Find the highest block in this column
            max_height = chunk.get_max_height()
            ground_height = max_height
            tints = []
            for y in range(max_height,-1,-1):
                block_id = chunk.get_block(x, y, z)
                if block_id != None:

                    #block_data = 0  # TODO: use block properties
                    #if (block_id == 'water' or block_id == 'water'):
                        #tints.append({'h':228, 's':50, 'l':23}) # Water
                    #elif (block_id == 'leaves'):  # TODO: old id - update
                        #if (block_data == 1):
                            #tints.append({'h':114, 's':64, 'l':22}) # Redwood Leaves
                        #elif (block_data == 2):
                            #tints.append({'h':93, 's':39, 'l':10}) # Birch Leaves
                        #else:
                            #tints.append({'h':114, 's':64, 'l':22}) # Normal Leaves
                    #elif (block_id == 'ice'):
                        #tints.append({'h':240, 's':5, 'l':95}) # Ice
                    #elif (block_id == 'fire'):
                        #tints.append({'h':55, 's':100, 'l':50}) # Fire
                    #elif (block_id != 'air' or block_id != 'cave_air' or y == 0):
                    if (block_id not in block_ignore or y == 0):
                        # Here is ground level
                        ground_height = y
                        break

            if block_id != None:
                if block_id in block_colors:
                    color = block_colors[block_id]
                else:
                    color = {'h':0, 's':0, 'l':100}
                    print("warning: unknown color for block id: %s" % block_id)
                    print("hint: add that block to the 'block_colors' map")
            else:
                color = {'h':0, 's':0, 'l':0}

            height_shift = 0 #(ground_height-64)*0.25

            final_color = {'h':color['h'], 's':color['s'], 'l':color['l'] + height_shift}
            if final_color['l'] > 100: final_color['l'] = 100
            if final_color['l'] < 0: final_color['l'] = 0

            # Apply tints from translucent blocks
            for tint in reversed(tints):
                final_color = hsl_slide(final_color, tint, 0.4)

            rgb = hsl2rgb(final_color['h'], final_color['s'], final_color['l'])

            pixels += pack("BBB", rgb[0], rgb[1], rgb[2])

    im = Image.frombytes('RGB', (16,16), pixels)
    return im
Example #14
0
    def runMission( self, mission_spec, mission_record_spec, action_space ):
        '''Sets a mission running.
        
        Parameters:
        mission_spec : MissionSpec instance, specifying the mission.
        mission_record_spec : MissionRecordSpec instance, specifying what should be recorded.
        action_space : string, either 'continuous' or 'discrete' that says which commands to generate.
        '''
        sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)  # flush print output immediately
        self.world_state = None
        self.action_space = action_space
        total_reward = 0

        if mission_spec.isVideoRequested(0):
            self.canvas.config( width=mission_spec.getVideoWidth(0), height=mission_spec.getVideoHeight(0) )

        try:
            self.agent_host.startMission( mission_spec, mission_record_spec )
        except RuntimeError as e:
            tkMessageBox.showerror("Error","Error starting mission: "+str(e))
            return

        print "Waiting for the mission to start",
        self.world_state = self.agent_host.peekWorldState()
        while not self.world_state.is_mission_running:
            sys.stdout.write(".")
            time.sleep(0.1)
            self.world_state = self.agent_host.peekWorldState()
            for error in self.world_state.errors:
                print "Error:",error.text
        print
        if mission_spec.isVideoRequested(0) and action_space == 'continuous':
            self.canvas.config(cursor='none') # hide the mouse cursor while over the canvas
            self.canvas.event_generate('<Motion>', warp=True, x=self.canvas.winfo_width()/2, y=self.canvas.winfo_height()/2) # put cursor at center
            self.root.after(50, self.update)
        self.canvas.focus_set()

        while self.world_state.is_mission_running:
            self.world_state = self.agent_host.getWorldState()
            if self.world_state.number_of_observations_since_last_state > 0:
                self.observation.config(text = self.world_state.observations[0].text )
            if mission_spec.isVideoRequested(0) and self.world_state.number_of_video_frames_since_last_state > 0:
                frame = self.world_state.video_frames[-1]
                image = Image.frombytes('RGB', (frame.width,frame.height), str(frame.pixels) )
                photo = ImageTk.PhotoImage(image)
                self.canvas.delete("all")
                self.canvas.create_image(frame.width/2, frame.height/2, image=photo)
                self.canvas.create_line( frame.width/2 - 5, frame.height/2, frame.width/2 + 6, frame.height/2, fill='white' )
                self.canvas.create_line( frame.width/2, frame.height/2 - 5, frame.width/2, frame.height/2 + 6, fill='white' )
                self.root.update()
            for reward in self.world_state.rewards:
                total_reward += reward.getValue()
            self.reward.config(text = str(total_reward) )
            time.sleep(0.01)
        if mission_spec.isVideoRequested(0) and action_space == 'continuous':
            self.canvas.config(cursor='arrow') # restore the mouse cursor
        print 'Mission stopped'
        if not self.agent_host.receivedArgument("test"):
            tkMessageBox.showinfo("Ended","Mission has ended. Total reward: " + str(total_reward) )
        self.root.destroy()
Example #15
0
def array2image(arr, mode):
    """NumPy array to PIL Image"""
    arr = arr.swapaxes(1, 2).swapaxes(0, 2)
    arr[arr < 0] = 0
    arr[arr > 255] = 255
    arr = numpy.fix(arr).astype(numpy.uint8)
    return Image.frombytes(mode, arr.shape[1::-1], arr.tobytes())
Example #16
0
def basic_decode_load(path, mode, decode):
    with open(path, 'rb') as fp:
        fp.seek(128)
        data = fp.read()
    decode_data = basic_decoder(data,decode)
    print(mode,(52,52),len(data),len(decode_data))
    return Image.frombytes(mode, (52, 52), decode_data)
Example #17
0
    def test_optimize_full_l(self):
        from io import BytesIO

        im = Image.frombytes("L", (16, 16), bytes(bytearray(range(256))))
        test_file = BytesIO()
        im.save(test_file, "GIF", optimize=True)
        self.assertEqual(im.mode, "L")
Example #18
0
    def renderArea(self, width, height, srs, xmin, ymin, xmax, ymax, zoom):
        """
        """
        start_time = time()

        #
        # Mapnik can behave strangely when run in threads, so place a lock on the instance.
        #
        if global_mapnik_lock.acquire():
            try:
                if self.mapnik is None:
                    self.mapnik = get_mapnikMap(self.mapfile)
                    logging.debug('TileStache.Mapnik.ImageProvider.renderArea() %.3f to load %s', time() - start_time, self.mapfile)

                self.mapnik.width = width
                self.mapnik.height = height
                self.mapnik.zoom_to_box(Box2d(xmin, ymin, xmax, ymax))

                img = mapnik.Image(width, height)
                mapnik.render(self.mapnik, img)
            except:
                self.mapnik = None
                raise
            finally:
                # always release the lock
                global_mapnik_lock.release()

        img = Image.frombytes('RGBA', (width, height), img.tostring())

        logging.debug('bbox:%lf,%lf,%lf,%lf', xmin, ymin, xmax, ymax)
        logging.debug('TileStache.Mapnik.ImageProvider.renderArea() %dx%d in %.3f from %s', width, height, time() - start_time, self.mapfile)

        return img
def write_bitmap(bitmap_instance, filename):
    # Need to install PIL
    # sudo pip install pillow
    from PIL import Image
    bitmap_bytes = bitmap_instance.fields.mBuffer.array_data
    image = Image.frombytes('RGBA', (bitmap_instance.fields.mWidth, bitmap_instance.fields.mHeight), bitmap_bytes)
    image.save(filename)
Example #20
0
def encode(data, version=0, level=QR_ECLEVEL_L, hint=QR_MODE_8,
           case_sensitive=True):
    """Creates a QR-Code from string data.

    Args:
      data: string: The data to encode in a QR-code. If a unicode string is
          supplied, it will be encoded in UTF-8.
      version: int: The minimum version to use. If set to 0, the library picks
          the smallest version that the data fits in.
      level: int: Error correction level. Defaults to 'L'.
      hint: int: The type of data to encode. Either QR_MODE_8 or QR_MODE_KANJI.
      case_sensitive: bool: Should string data be encoded case-preserving?
    Returns:
      A (version, size, image) tuple, where image is a size*size PIL image of
      the QR-code.
    """
    if isinstance(data, unicode):
        data = data.encode('utf8')
    elif not isinstance(data, basestring):
        raise ValueError('data argument must be a string.')
    version = int(version)
    if level not in levels:
        raise ValueError('Invalid error-correction level.')
    if hint not in hints:
        raise ValueError('Invalid encoding mode.')
    if case_sensitive:
        version, size, data = _encode(data, version, level, hint, True)
    else:
        version, size, data = _encode(data, version, level, hint, False)

    im = Image.frombytes('L', (size, size), data)
    return (version, size, im)
Example #21
0
def get_images(pdf_file):
    with open(pdf_file, 'rb') as fp:
        reader = PdfFileReader(fp)
        page = reader.getPage(0)
        xObject = page['/Resources']['/XObject'].getObject()

        for obj in xObject:
            if xObject[obj]['/Subtype'] == '/Image':
                width, height = (xObject[obj]['/Width'], xObject[obj]['/Height'])
                # Ignore smaller images.
                if height < 100:
                    continue

                size = width, height
                data = xObject[obj].getData()
                if xObject[obj]['/ColorSpace'] == '/DeviceRGB':
                    mode = "RGB"
                else:
                    mode = "P"

                encoding = xObject[obj]['/Filter']
                if encoding == '/FlateDecode' or '/FlateDecode' in encoding:
                    yield Image.frombytes(mode, size, data)
                else:
                    raise Exception(
                        'Unexpected image encoding: {}'.format(encoding))
Example #22
0
def test_basic_material():
    # create context
    ctx = ModernGL.create_standalone_context()

    # create renderer
    renderer = ModernGLRenderer(ctx)

    # create scene
    scene = radiant.Scene()

    cube_geom = radiant.PlaneGeometry()
    red = radiant.MeshBasicMaterial(color=(1.0, 0.0, 0.0, 0.0))
    cube = radiant.Mesh(cube_geom, red)
    scene.append_child(cube)

    # create camera
    camera = radiant.PerspectiveCamera(position=[10, 10, 10], target=[0, 0, 0], up=[0, 1, 0])
    scene.append_child(camera)

    # create framebuffer and render into it
    fbo = ctx.framebuffer(ctx.renderbuffer((512, 512)))
    fbo.use()
    renderer.render(scene, camera)

    # read from the framebuffer and write to an image file
    data = fbo.read(components=3, alignment=1)
    img = Image.frombytes('RGB', fbo.size, data).transpose(Image.FLIP_TOP_BOTTOM)
    filename = "test_basic.png"
    with open(filename, mode="wb") as fh:
        img.save(fh)

    # complete the test
    assert os.path.exists(filename)
Example #23
0
    def to_xxx_colorsys(self, im, func, mode):
        # convert the hard way using the library colorsys routines.

        (r, g, b) = im.split()

        if bytes is str:
            conv_func = self.str_to_float
        else:
            conv_func = self.int_to_float

        if hasattr(itertools, 'izip'):
            iter_helper = itertools.izip
        else:
            iter_helper = itertools.zip_longest

        converted = [self.tuple_to_ints(func(conv_func(_r), conv_func(_g),
                                             conv_func(_b)))
                     for (_r, _g, _b) in iter_helper(r.tobytes(), g.tobytes(),
                                                     b.tobytes())]

        if str is bytes:
            new_bytes = b''.join(chr(h)+chr(s)+chr(v) for (
                h, s, v) in converted)
        else:
            new_bytes = b''.join(bytes(chr(h)+chr(s)+chr(v), 'latin-1') for (
                h, s, v) in converted)

        hsv = Image.frombytes(mode, r.size, new_bytes)

        return hsv
Example #24
0
 def __init__(self, app, w, b=None, inf=None):
     self.render_flag = 0
     self.l = w
     self.app = app
     if b:
         b.configure(command=self.done)
     self.inf = inf
     self._image = None
     self.tkimage = None
     self.top = 0
     self.left = 0
     self.right = 0
     self.bottom = 0
     self.blurred = None
     self.xor = None
     self.x0 = None
     self.y0 = None
     w.bind("<Button-1>", self.start)
     w.bind("<Double-Button-1>", self.start)
     w.bind("<Button1-Motion>", self.motion)
     w.bind("<ButtonRelease-1>", self.end)
     dummy_image = Image.frombytes('RGB', (1, 1), '\0\0\0')
     self.dummy_tkimage = PhotoImage(dummy_image)
     self.state = DRAG_NONE
     self.round = 1
     self.image = None
     w.configure(image=self.dummy_tkimage)
     self.v = IntVar(app)
Example #25
0
def apply_mirror(pixbuf):
    '''
    image left to right
    '''
    width,height = pixbuf.get_width(),pixbuf.get_height() 
    y = ImageOps.mirror(Image.frombytes(K.ImageConstants.RGB_SHORT_NAME,(width,height),pixbuf.get_pixels() ))
    return I.fromImageToPixbuf(y)
Example #26
0
def apply_flip(pixbuf):
    '''
    image top to bottom
    '''
    width,height = pixbuf.get_width(),pixbuf.get_height() 
    y = ImageOps.flip(Image.frombytes(K.ImageConstants.RGB_SHORT_NAME,(width,height),pixbuf.get_pixels() ))
    return I.fromImageToPixbuf(y)
Example #27
0
def apply_equalizer(pixbuf):    
    '''
    creates a uniform distribution of grayscale values in the output image
    '''
    width,height = pixbuf.get_width(),pixbuf.get_height() 
    y = ImageOps.equalize(Image.frombytes(K.ImageConstants.RGB_SHORT_NAME,(width,height),pixbuf.get_pixels() ))
    return I.fromImageToPixbuf(y)
Example #28
0
def createImageHistogram(pixbuf):
    width,height = pixbuf.get_width(),pixbuf.get_height() 
    y = Image.frombytes(K.ImageConstants.RGB_SHORT_NAME,(width,height),pixbuf.get_pixels() )
    histogram = y.histogram()
    #create new image with histogram
    histogramImage = Image.new("RGBA", (300, 200))   
    histogramLineImage = ImageDraw.Draw(histogramImage)
    
    #draw histogram lines  
    red = (255,0,0)              
    green = (0,255,0)             
    blue = (0,0,255) 
    xAxis=0 
    yAxis=0
    scalingFactor = float((200)*1.5)/max(histogram)
    for value in histogram:
        if (value > 0):
            rgb = red
            if (yAxis > 255): 
                rgb = green
            if (yAxis > 511): 
                rgb = blue
            histogramLineImage.line((xAxis, 200, xAxis, 200-(value*scalingFactor)), fill=rgb)        
            if (xAxis > 255): 
                xAxis=0
            else: 
                xAxis+=1
            yAxis+=1
    
    return I.fromImageToPixbuf(histogramImage)
Example #29
0
 def load(self, im):
     im.fp.seek(0)  # rewind
     return Image.frombytes(
         "RGB", im.size,
         Image.core.drawwmf(im.fp.read(), im.size, self.bbox),
         "raw", "BGR", (im.size[0]*3 + 3) & -4, -1
         )
Example #30
0
def apply_invert(pixbuf):
    '''
    negative of an image (darkest-->lightest | lightest-->darkest)
    '''
    width,height = pixbuf.get_width(),pixbuf.get_height() 
    y = ImageOps.invert(Image.frombytes(K.ImageConstants.RGB_SHORT_NAME,(width,height),pixbuf.get_pixels() ))
    return I.fromImageToPixbuf(y)
Example #31
0
def replayStep(step: Step):
    global currentStep
    global delay
    global replayLast
    global timedDelays
    currentStep = currentStep + 1
    if step.readyImage is not None:
        count = 0
        # if difference between current image and reference image is all black
        if step.hasAlpha:
            # create out ignore mask, this is an image where any transparent part of the readyImage will be transparent on the check image
            ignoreMask = step.readyImage
        else:
            # create our ignore mask, this is an image where any white part of the readyImage will be white on the check image
            ignoreMask = step.readyImage.point(lambda x: 255
                                               if x == 255 else 0)
            transparencyMask = ignoreMask.convert("1", dither=0)
            ignoreMask.putalpha(transparencyMask)
        count = 1
        while True:
            sct_img = sct.grab(bbox)
            im = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw",
                                 "BGRX")
            im = im.convert("RGBA")
            if step.hasAlpha:
                im2 = Image.alpha_composite(im, ignoreMask)
                dif = ImageChops.difference(im2, im)
            else:
                im = Image.alpha_composite(im, ignoreMask)
                dif = ImageChops.difference(im, step.readyImage)
            count = count + 1
            if step.output == OutputType.ENTER_UNTIL:
                keyboard.press(Key.enter)
                keyboard.release(Key.enter)
            if count % 50 == 0:
                if step.imageName.startswith(".\\elf"):
                    newRefImage = loopAndGrabImage()
                    refImageName = ".\\elf\\" + str(
                        currentStep) + "_" + '%05x' % random.randrange(
                            16**5) + ".png"
                    print("creating:" + refImageName)
                    newRefImage.save(refImageName)
                print("Waiting on:" + step.imageName)
                if step.imageName.endswith("2_cc112.png"):
                    dif.show()
                    dif.save("dif.png")
                    os._exit(0)
            if count % 2000 == 0:
                print("Waiting on:" + step.imageName)
                dif.show()
                dif.save("dif.png")
                os._exit(0)
            if dif.getbbox() is None or dif.getbbox()[3] < 20:
                break
    if step.output == OutputType.TARGET_JIFFY:
        sct_img = sct.grab(bbox)
        im = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
        coords = targetJiffy(im)
        if coords is not None:
            print("TARGET LOCK: " + str(coords))
            moveMouse(coords[0] - currentMov[0], (coords[1] - currentMov[1]))
            time.sleep(0.07)
            mouse.press(Button.left)
            time.sleep(0.02)
            mouse.release(Button.left)

            timedDelays += 0.09
        else:
            print("TARGET FAILED TO COORD")
    if step.output == OutputType.WAIT:
        time.sleep(0.05)
        timedDelays += 0.05
    if step.output == OutputType.WAIT_GAME:
        time.sleep(0.05)
        timedDelays += 0.05
        keyboard.press("w")
        keyboard.release("w")
    if step.output == OutputType.UP:
        keyboard.press(Key.up)
        keyboard.release(Key.up)
    if step.output == OutputType.ESCAPE:
        keyboard.press(Key.esc)
        keyboard.release(Key.esc)
    if step.output == OutputType.LEFT:
        keyboard.press(Key.left)
        keyboard.release(Key.left)
    if step.output == OutputType.DOWN:
        keyboard.press(Key.down)
        keyboard.release(Key.down)
    if step.output == OutputType.PG_UP:
        keyboard.press(Key.page_up)
        keyboard.release(Key.page_up)
    if step.output == OutputType.PG_DOWN:
        keyboard.press(Key.page_down)
        keyboard.release(Key.page_down)
    if step.output == OutputType.HOME:
        keyboard.press(Key.home)
        keyboard.release(Key.home)
    if step.output == OutputType.END:
        keyboard.press(Key.END)
        keyboard.release(Key.END)
    if step.output == OutputType.ENTER:
        keyboard.press(Key.enter)
        keyboard.release(Key.enter)
    if step.output == OutputType.MOVE:
        moved = moveMouse(step.clickPos[0] - currentMov[0],
                          (step.clickPos[1] - currentMov[1]))
    if step.output == OutputType.ANIM_OFF:
        print("anim off")
        keyboard.press(Key.f7)
        time.sleep(0.02)
        keyboard.release(Key.f7)
        time.sleep(0.02)
        keyboard.press(Key.f8)
        time.sleep(0.02)
        keyboard.release(Key.f8)
        time.sleep(0.02)
        timedDelays += 0.08
    if step.output == OutputType.RIGHT:
        keyboard.press(Key.right)
        keyboard.release(Key.right)
    if step.output == OutputType.CLICK:
        if step.clickPos:
            moved = moveMouse(step.clickPos[0] - currentMov[0],
                              (step.clickPos[1] - currentMov[1]))
            if moved:
                time.sleep(0.07)
                timedDelays += 0.07
            if delay:
                print("extra sleep")
                time.sleep(0.1)
                timedDelays += 0.1
        mouse.press(Button.left)
        time.sleep(0.02)
        timedDelays += 0.02
        if delay:
            time.sleep(0.03)
            timedDelays += 0.03
        mouse.release(Button.left)
    if step.output == OutputType.LONG_CLICK:
        moved = moveMouse(step.clickPos[0] - currentMov[0],
                          (step.clickPos[1] - currentMov[1]))
        if moved:
            time.sleep(0.07)
            timedDelays += 0.07
        mouse.press(Button.left)
        time.sleep(0.1)
        timedDelays += 0.1
        mouse.release(Button.left)
    if step.output == OutputType.RESET:
        resetMouse()
        time.sleep(0.02)
        resetMouse()
        time.sleep(0.02)
        resetMouse()
        time.sleep(0.02)
        resetMouse()

        timedDelays += 0.06
    if step.output == OutputType.DELAY:
        delay = not delay
    if step.output == OutputType.JACOB:
        pressAndRelease("1")
        pressAndRelease("2")
        pressAndRelease("3")
        pressAndRelease("4")
        pressAndRelease("5")
        pressAndRelease("6")
        pressAndRelease("7")
        pressAndRelease("2")
        pressAndRelease("8")
        pressAndRelease("8")
        pressAndRelease("9")
        pressAndRelease("0")
        pressAndRelease(Key.enter)
    if step.output == OutputType.INV_LEFT:
        moveMouse(0 - currentMov[0], (650 - currentMov[1]))
        time.sleep(0.1)
        moveMouse(0 - currentMov[0], (550 - currentMov[1]))
    if step.output == OutputType.TIME:
        global startTime
        print(datetime.datetime.utcnow() - startTime)
        print(timedDelays)
    return currentStep
Example #32
0
def heic_to_jpg(f, hashed_file_name):
	image_heic = read_heif(f)
	image_jpg = ImagePIL.frombytes(mode=image_heic.mode, size=image_heic.size, data=image_heic.data)
	image_jpg.save(os.path.join(os.getcwd(), 'app/static/images', hashed_file_name), format="jpeg")
Example #33
0
def take_ss(monitor_config):
    return cv.cvtColor(
        np.array(
            Image.frombytes("RGB", (game_width, game_height),
                            sct.grab(monitor_config).rgb)), cv.COLOR_RGB2BGR)
def main():
    img = mpimg.imread(
        '/home/jjordening/git/thunderhill_data/dataset_sim_001_km_320x160/IMG/center_2017_03_07_07_21_54_311.jpg'
    )
    h, w = img.shape[:2]
    src = np.float32([[w / 2 - 57, h / 2], [w / 2 + 57, h / 2], [w + 140, h],
                      [-140, h]])
    dst = np.float32([[w / 4, 0], [w * 3 / 4, 0], [w * 3 / 4, h], [w / 4, h]])
    M = cv2.getPerspectiveTransform(src, dst)
    invM = cv2.getPerspectiveTransform(dst, src)
    transform = functools.partial(perspectiveTransform, M=M.copy())
    #plt.imshow(preprocessImage(img, transform))
    #plt.show()

    #showSamplesCompared(img, transform, '', '', '')
    #plt.xkcd()
    np.random.seed(0)
    #data = pd.read_csv('/home/jjordening/git/thunderhill_data/dataset_sim_000_km_few_laps/driving_log.csv',
    #                   header = None, names=['center','left', 'right', 'steering','throttle', 'brake', 'speed', 'position', 'orientation'])
    #data['positionX'], data['positionY'], data['positionZ'] = data['position'].apply(retrieveVectors)
    #data['orientationX'], data['orientationY'], data['orientationZ'] = data['orientation'].apply(retrieveVectors)
    #data['center'] = '/home/jjordening/git/thunderhill_data/dataset_sim_000_km_few_laps/'+data['center'].apply(lambda x: x.strip())
    data5 = pd.read_csv('/home/jjordening/data/823/output_processed.txt')
    data5['path'] = '/home/jjordening/data/1610/' + data5['path'].apply(
        lambda x: x.strip())

    data6 = pd.read_csv('/home/jjordening/data/832/output_processed.txt')
    data6['path'] = '/home/jjordening/data/1645/' + data6['path'].apply(
        lambda x: x.strip())

    data7 = pd.read_csv('/home/jjordening/data/837/output_processed.txt')
    data7['path'] = '/home/jjordening/data/1702/' + data7['path'].apply(
        lambda x: x.strip())

    #data['right'] = '../simulator/data/data/'+data['right'].apply(lambda x: x.strip())
    #data['left'] = '../simulator/data/data/'+data['left'].apply(lambda x: x.strip())
    angles = []
    dataNew = pd.DataFrame()
    offset = 0
    #print(data3['steering'])
    #print(data1['longitude'])
    for dat in [data5, data6, data7]:
        angles.extend(dat['steering'].values)
        for row in dat.iterrows():
            dat.loc[row[0], 'angleIndex'] = row[0] + offset
            #images.append(preprocessImage(mpimg.imread(row[1]['center'].strip())))
            #images.append(transform(mpimg.imread(row[1]['center'].strip())))
        offset += 100
        dataNew = dataNew.append(dat.ix[100:])

    dataNew['throttle'] = dataNew['accel'].apply(
        lambda x: .9 * max(x / np.max(dataNew['accel']), -.1) + .1)
    print(np.max(dataNew['throttle']), np.min(dataNew['throttle']))
    # TODO: Normalisation of position and orientation<
    del data5, data6, data7
    print(len(dataNew), dataNew.columns)
    print(np.histogram(dataNew['throttle'], bins=31))
    hist, edges = np.histogram(dataNew['steering'], bins=31)
    hist = 1. / np.array([
        val if val > len(dataNew) / 30. else len(dataNew) / 30. for val in hist
    ])
    hist *= len(dataNew) / 30.
    print(hist, len(dataNew))
    dataNew['norm'] = dataNew['steering'].apply(
        lambda x: getNormFactor(x, hist, edges))
    print(dataNew['norm'].unique())
    print(np.min(dataNew['steering']), np.max(dataNew['steering']))
    print(np.min(dataNew['throttle']), np.max(dataNew['throttle']))
    print(np.min(dataNew['brake']), np.max(dataNew['brake']))

    for col in ['longitude', 'latitude']:
        vals = dataNew[col].values
        mean = np.mean(vals)
        std = np.std(vals)
        dataNew[col] -= mean
        dataNew[col] /= std
        print('%s Mean:%.12f Std:%.12f' % (col, mean, std))

    dataNew['speed'] = dataNew['speed'].apply(lambda x: x / 40. - 1)

    dataNew = shuffle(dataNew, random_state=0)
    #plt.figure(1, figsize=(8,4))
    #plt.hist(dataNew['steering'], bins =31)

    #plt.show()

    dataTrain, dataTest = train_test_split(dataNew, test_size=.2)
    dataTrain, dataVal = train_test_split(dataTrain, test_size=.2)

    file = open(dataTrain['path'].iloc[0], 'rb')
    # Use the PIL raw decoder to read the data.
    #   - the 'F;16' informs the raw decoder that we are reading a little endian, unsigned integer 16 bit data.
    img = np.array(Image.frombytes('RGB', [960, 480], file.read(), 'raw'))
    file.close()

    imShape = preprocessImage(img).shape
    print(imShape)

    batchSize = 128
    epochBatchSize = 4096
    trainGenerator = generateImagesFromPaths(dataTrain, batchSize, imShape,
                                             [3], angles, True)
    t = time.time()
    trainGenerator.__next__()
    print("Time to build train batch: ", time.time() - t)
    valGenerator = generateImagesFromPaths(dataVal, batchSize, imShape, [3],
                                           angles)
    t = time.time()
    valGenerator.__next__()
    print("Time to build validation batch: ", time.time() - t)
    stopCallback = EarlyStopping(monitor='val_loss',
                                 patience=20,
                                 min_delta=0.01)
    checkCallback = ModelCheckpoint('psyncModel.ckpt',
                                    monitor='val_loss',
                                    save_best_only=True)
    visCallback = TensorBoard(log_dir='./logs')
    model = load_model('psyncModelBase.h5',
                       custom_objects={'customLoss': customLoss})
    prevWeights = []
    for layer in model.layers:
        prevWeights.append(layer.get_weights())
    if LOADMODEL:
        endModel = load_model('psyncModelBase.h5',
                              custom_objects={'customLoss': customLoss})
        endModel.fit_generator(
            trainGenerator,
            callbacks=[stopCallback, checkCallback, visCallback],
            nb_epoch=100,
            samples_per_epoch=epochBatchSize,
            max_q_size=8,
            validation_data=valGenerator,
            nb_val_samples=len(dataVal),
            nb_worker=8,
            pickle_safe=True)
        endModel.load_weights('psyncModel.ckpt')
        endModel.save('psyncModel.h5')

    else:
        inpC = Input(shape=(imShape[0], imShape[1], imShape[2]),
                     name='input_1')
        xC = Convolution2D(24,
                           8,
                           8,
                           border_mode='valid',
                           subsample=(2, 2),
                           weights=prevWeights[1],
                           trainable=False)(inpC)
        xC = BatchNormalization()(xC)
        xC = Activation('elu')(xC)
        print(xC.get_shape())
        xC = Convolution2D(36,
                           5,
                           5,
                           border_mode='valid',
                           subsample=(2, 2),
                           weights=prevWeights[4],
                           trainable=False)(xC)
        xC = BatchNormalization()(xC)
        xC = Activation('elu')(xC)
        print(xC.get_shape())
        xC = Convolution2D(48,
                           5,
                           5,
                           border_mode='valid',
                           subsample=(2, 2),
                           weights=prevWeights[7],
                           trainable=False)(xC)
        xC = BatchNormalization()(xC)
        xC = Activation('elu')(xC)
        print(xC.get_shape())
        xC = Convolution2D(64,
                           5,
                           5,
                           border_mode='valid',
                           weights=prevWeights[10],
                           trainable=False)(xC)
        xC = BatchNormalization()(xC)
        xC = Activation('elu')(xC)
        print(xC.get_shape())
        xC = Convolution2D(64,
                           5,
                           5,
                           border_mode='valid',
                           weights=prevWeights[13],
                           trainable=False)(xC)
        xC = BatchNormalization()(xC)
        xC = Activation('elu')(xC)
        print(xC.get_shape())
        xOut = Flatten()(xC)

        xVectorInp = Input(shape=(3, ), name='input_3')
        xVector = Dropout(.1)(xVectorInp)

        #inpAngles = Input(shape=(ANGLESFED,), name='input_2')

        xOut = Lambda(lambda x: K.concatenate(x, axis=1))([xOut, xVector])
        xOut = Dense(200, weights=prevWeights[20], trainable=False)(xOut)
        xOut = BatchNormalization()(xOut)
        xOut = Activation('elu')(xOut)
        xOut = Dense(100, weights=prevWeights[23], trainable=False)(xOut)
        xOut = BatchNormalization()(xOut)
        xEnd = Activation('elu')(xOut)

        xOutSteer = Dense(50, weights=prevWeights[26], trainable=False)(xEnd)
        xOutSteer = BatchNormalization()(xOutSteer)
        xOutSteer = Activation('elu')(xOutSteer)
        xOutSteer = Dropout(.3)(xOutSteer)
        xOutSteer = Dense(10, weights=prevWeights[30],
                          trainable=False)(xOutSteer)
        xOutSteer = BatchNormalization()(xOutSteer)
        xOutSteer = Activation('elu')(xOutSteer)
        xOutSteer = Dense(1,
                          activation='sigmoid',
                          weights=prevWeights[33],
                          trainable=False)(xOutSteer)
        xOutSteer = Lambda(lambda x: x * 10 - 5, name='outputSteer')(xOutSteer)

        xOutThr = Dense(50, name='thr1')(xEnd)
        xOutThr = BatchNormalization(name='thr2')(xOutThr)
        xOutThr = Activation('elu')(xOutThr)
        xOutThr = Dropout(.3)(xOutThr)
        xOutThr = Dense(10, name='thr3')(xOutThr)
        xOutThr = BatchNormalization(name='thr4')(xOutThr)
        xOutThr = Activation('elu')(xOutThr)
        xOutThr = Dense(1, activation='sigmoid', name='thr5')(xOutThr)
        xOutThr = Lambda(lambda x: x * 2 - 1, name='outputThr')(xOutThr)

        endModel = Model((inpC, xVectorInp), (xOutSteer, xOutThr))
        endModel.compile(optimizer=Adam(lr=1e-4),
                         loss=customLoss,
                         metrics=['mse'])
        #endModel.fit_generator(trainGenerator, callbacks = [visCallback],
        #                       nb_epoch=50, samples_per_epoch=epochBatchSize,
        #                       max_q_size=24, nb_worker=8, pickle_safe=True)
        endModel.fit_generator(trainGenerator,
                               nb_epoch=50,
                               samples_per_epoch=epochBatchSize,
                               max_q_size=24,
                               nb_worker=8,
                               pickle_safe=True)
        endModel.fit_generator(
            trainGenerator,
            callbacks=[stopCallback, checkCallback, visCallback],
            nb_epoch=200,
            samples_per_epoch=epochBatchSize,
            max_q_size=24,
            validation_data=valGenerator,
            nb_val_samples=len(dataVal),
            nb_worker=8,
            pickle_safe=True)
        endModel.load_weights('psyncModel.ckpt')
        endModel.save('psyncModel.h5')
        endModel.save('psyncModelBase.h5')

    endModel = load_model('psyncModelThr.h5',
                          custom_objects={'customLoss': customLoss})

    print(endModel.evaluate_generator(valGenerator, val_samples=len(dataVal)))
    print(
        endModel.evaluate_generator(generateImagesFromPaths(
            dataTest, batchSize, imShape, [3], angles),
                                    val_samples=len(dataTest)))
Example #35
0
    def test_sanity(self):
        im1 = hopper()
        im2 = Image.frombytes(im1.mode, im1.size, im1.tobytes())

        assert_image_equal(im1, im2)
Example #36
0
# Random Spiral IFS Fractals
import time
from PIL import Image
from cyspirals import generate_spirals

if __name__ == '__main__':
    size = 4096

    raw_data = generate_spirals(size, size, seed=time.time())
    data = raw_data.tostring()

    img = Image.frombytes('L', (size, size), data, decoder_name='raw')
    img.save('result.png')
Example #37
0
def pil_frombytes(im):
    return Image.frombytes('RGB', im.size, im.bgra, 'raw', 'BGRX').tobytes()
# Open the video device.
video = v4l2capture.Video_device("/dev/video0")

# Suggest an image size to the device. The device may choose and
# return another size if it doesn't support the suggested one.
size_x, size_y = video.set_format(1280, 1024)

# Create a buffer to store image data in. This must be done before
# calling 'start' if v4l2capture is compiled with libv4l2. Otherwise
# raises IOError.
video.create_buffers(1)

# Start the device. This lights the LED if it's a camera that has one.
video.start()

# Wait a little. Some cameras take a few seconds to get bright enough.
time.sleep(2)

# Send the buffer to the device.
video.queue_all_buffers()

# Wait for the device to fill the buffer.
select.select((video, ), (), ())

# The rest is easy :-)
image_data = video.read()
video.close()
image = Image.frombytes("RGB", (size_x, size_y), image_data)
image.save("image.jpg")
print("Saved image.jpg (Size: " + str(size_x) + " x " + str(size_y) + ")")
Example #39
0
def show(data):
    img = Image.frombytes('1', data.shape[::-1], np.packbits(data, 1))
    img.show()
    img.save('mandelbrot.png')
Example #40
0
print("theta0_minion;theta1_tower;theta2_approach;theta3_retreat")

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        while True:
            #print('=========Loop took: {} seconds'.format(time.time()-last_time))
            last_time = time.time()
            # update gametime
            state.game_time = time.time() - state.start_time
            # replaced by mss package
            #screen = cv2.resize(grab_screen(region=(0,40,1024,768)), (800,450))
            #screen = np.array(ImageGrab.grab(bbox=(0,40,1024,768)))
            # get screen recording and resize it to 800x450 pixels for output
            sct.get_pixels(mon)
            screen = Image.frombytes('RGB', (sct.width, sct.height), sct.image)
            screen = np.array(screen)
            screen = cv2.resize(screen, (MONITOR_WIDTH, MONITOR_HEIGHT))
            image_np = screen
            # ===================tensorflow template code===============
            # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
            image_np_expanded = np.expand_dims(image_np, axis=0)
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            # Each box represents a part of the image where a particular object was detected.
            boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
            # Each score represent how level of confidence for each of the objects.
            # Score is shown on the result image, together with the class label.
            scores = detection_graph.get_tensor_by_name('detection_scores:0')
            classes = detection_graph.get_tensor_by_name('detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name(
                'num_detections:0')
def main():
    sight = {'x': (-30, 30), 'z': (-30, 30), 'y': (-1, 1)}

    range_x = abs(sight['x'][1] - sight['x'][0]) + 1
    range_y = abs(sight['y'][1] - sight['y'][0]) + 1
    range_z = abs(sight['z'][1] - sight['z'][0]) + 1

    malmoutils.fix_print()
    agent_host = MalmoPython.AgentHost()
    malmoutils.parse_command_line(agent_host)
    recordingsDirectory = malmoutils.get_recordings_directory(agent_host)
    recordingsDirectory = "../human_trajectories"
    if (not os.path.exists(recordingsDirectory)):
        os.mkdir(recordingsDirectory)
    logging.basicConfig(level=logging.INFO)
    # pdb.set_trace()
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)  # set to INFO if you want fewer messages

    video_width = 640
    video_height = 480
    sys.argv

    mission_xml_path = "../custom_xmls/usar.xml"
    validate = True
    # my_mission = MalmoPython.MissionSpec(missionXML, validate)
    my_mission = MalmoPython.MissionSpec(getMissionXML(mission_xml_path),
                                         validate)

    # ObservationFromGrid
    my_mission.observeGrid(sight['x'][0], sight['y'][0], sight['z'][0],
                           sight['x'][1], sight['y'][1], sight['z'][1],
                           'relative_view')

    # agent_host.setObservationsPolicy(MalmoPython.ObservationsPolicy.LATEST_OBSERVATION_ONLY)
    agent_host.setVideoPolicy(MalmoPython.VideoPolicy.LATEST_FRAME_ONLY)

    if agent_host.receivedArgument("test"):
        num_reps = 1
    else:
        num_reps = 30000

    my_mission_record = MalmoPython.MissionRecordSpec()
    if recordingsDirectory:
        my_mission_record.recordRewards()
        my_mission_record.recordObservations()
        my_mission_record.recordCommands()
        # if agent_host.receivedArgument("record_video"): # my_mission_record.recordMP4(24,2000000)
        my_mission_record.recordMP4(24, 2000000)
    recording_name = datetime.now().strftime("%Y-%m-%d_%I-%M-%S_%p")
    for iRepeat in range(1):
        my_mission_record.setDestination(
            os.path.join(recordingsDirectory, recording_name + ".tgz"))
        max_retries = 3
        for retry in range(max_retries):
            try:
                agent_host.startMission(my_mission, my_mission_record)
                break
            except RuntimeError as e:
                if retry == max_retries - 1:
                    logger.error("Error starting mission: %s" % e)
                    exit(1)
                else:
                    time.sleep(2)

        logger.info('Mission %s', iRepeat)
        logger.info("Waiting for the mission to start")
        world_state = agent_host.getWorldState()
        while not world_state.has_mission_begun:
            print(".", end="")
            time.sleep(0.1)
            world_state = agent_host.getWorldState()
        print()

        img_counter = 0
        # print('observations', world_state.observations)
        while world_state.is_mission_running:
            world_state = agent_host.getWorldState()

            # Observations
            # msg = observe(agent_host)
            # if msg is not None:
            #     print('timestamp: ', msg['timestamp'])

            # NOTE : Nothing recorded in world state. Uncomment to test it out.

            # if world_state.number_of_observations_since_last_state > 0:
            #     timestamp = world_state.observations[-1].timestamp
            #     msg = world_state.observations[-1].text
            #     obs = json.loads(msg)
            #     print("{'timestamp': timestamp, 'observations': obs}")

            # Video Frames
            while world_state.number_of_video_frames_since_last_state < 1 and world_state.is_mission_running:
                logger.info("Waiting for frames...")
                time.sleep(0.05)
                world_state = agent_host.getWorldState()

            logger.info("Got frame!")
            # import ipdb; ipdb.set_trace
            # print('observations', world_state.observations)
            # world_state.observations
            if world_state.is_mission_running:
                # timestamp = world_state.observations[-1].timestamp
                # msg = world_state.observations[-1].text
                # print(timestamp)
                # print(msg)
                frame = world_state.video_frames[-1]
                img = Image.frombytes('RGB', (640, 480), bytes(frame.pixels))
                # imageio.imsave("./tmp_imgs/{}.png".format(img_counter), img)
                img_counter += 1
        logger.info("Mission has stopped.")
        time.sleep(1)  # let the Mod recover
Example #42
0
def runGraphFaster(video_file_name, input_tensor, output_tensor, labels, session, session_name):
    # Performs inference using the passed model parameters. 
    global flagfound
    global n
    n = 0
    results = []

    # setup pointer to video file
    if args.deinterlace == True:
        deinterlace = 'yadif'
    else:
        deinterlace = ''

    # Let's get the video meta data
    video_filename, video_file_extension = path.splitext(path.basename(video_file_name))
    video_metadata = getinfo(video_file_name)
    num_seconds = int(float(video_metadata['streams'][0]['duration']))
    num_of_frames = int(float(video_metadata['streams'][0]['duration_ts']))
    video_width = int(video_metadata['streams'][0]['width'])
    video_height = int(video_metadata['streams'][0]['height'])
    
    # let's get the real FPS as we don't want duplicate frames!
    effective_fps = int(num_of_frames / num_seconds)
    if effective_fps > int(args.fps):
        effective_fps = int(args.fps)
        num_of_frames = num_seconds * int(args.fps)
    
    source_frame_size = str(video_width) + 'x' + str(video_height)
    target_frame_size = args.width + 'x' + args.height

    if(args.training == True):
        frame_size = source_frame_size
    else:
        frame_size = target_frame_size
        video_width = int(args.width)
        video_height = int(args.height)

    print(' ')
    print('Procesing ' + str(num_seconds) + ' seconds of ' + video_filename + ' at ' + str(effective_fps) + 
        ' frame(s) per second with ' + frame_size + ' source frame size.')
    command = [
        FFMPEG_PATH, '-i', video_file_name,
        '-vf', 'fps=' + args.fps, '-r', args.fps, '-vcodec', 'rawvideo', '-pix_fmt', 'rgb24', '-vsync', 'vfr',
        '-hide_banner', '-loglevel', '0', '-vf', deinterlace, '-f', 'image2pipe', '-vf', 'scale=' + frame_size, '-'
    ]
    image_pipe = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=4*1024*1024)

    # setup the input and output tensors
    output_tensor = sess1.graph.get_tensor_by_name(session_name + '/' + output_tensor)
    input_tensor = sess1.graph.get_tensor_by_name(session_name + '/' + input_tensor)

    # count the number of labels
    top_k = []
    for i in range(0, len(labels)):
        top_k.append(i)

    while True:
        # read next frame
        raw_image = image_pipe.stdout.read(int(video_width)*int(video_height)*3)
        if not raw_image:
            break # stop processing frames EOF!
        else:
            # Run model and get predictions
            processed_image = np.frombuffer(raw_image, dtype='uint8')
            processed_image = processed_image.reshape((int(video_width), int(video_height), 3))
            
            if frame_size != target_frame_size:
                # we need to fix the frame size so the model does not panic!
                fixed_image = Image.frombytes('RGB', (int(video_width), int(video_height)), processed_image)
                fixed_image = fixed_image.resize((int(args.width), int(args.height)), PIL.Image.ANTIALIAS)
                fixed_image = np.expand_dims(fixed_image, 0)
                final_image = np.divide(np.subtract(fixed_image, [0]), [255])
            else:
                processed_image = processed_image.astype(float)
                processed_image = np.expand_dims(processed_image, 0)
                final_image = np.divide(np.subtract(processed_image, [0]), [255])

            predictions = session.run(output_tensor, {input_tensor: final_image})
            predictions = np.squeeze(predictions)
            image_pipe.stdout.flush()
            n = n + 1

        data_line = []
        for node_id in top_k:
            human_string = labels[node_id]
            score = predictions[node_id]

            score = float("{0:.4f}".format(score))
            data_line.append(human_string)
            data_line.append(score)

            # save frames that are around the decision boundary so they can then be used for later model re-training.
            if args.training == True:
                if score >= float(int(args.traininglower) / 100) and score <= float(int(args.trainingupper) / 100):
                    save_training_frames(raw_image, n, human_string, video_width, video_height, int(score * 100), video_filename)

        results.append(data_line)
        drawProgressBar(percentage(n, (num_of_frames)) / 100, 40)  # --------------------- Start processing logic

    print(' ')
    print(str(n - 1) + ' video frames processed for ' + video_file_name)
    return results
Example #43
0
def rollout(env,
            agent,
            max_path_length=np.inf,
            animated=False,
            speedup=1,
            save_video=False,
            video_filename='sim_out.mp4',
            reset_arg=None):

    observations = []
    actions = []
    rewards = []
    agent_infos = []
    env_infos = []
    images = []
    o = env.reset(reset_args=reset_arg)
    agent.reset()
    path_length = 0
    if animated:
        env.render()
    while path_length < max_path_length:
        a, agent_info = agent.get_action(o)
        next_o, r, d, env_info = env.step(a)
        observations.append(env.observation_space.flatten(o))
        rewards.append(r)
        actions.append(env.action_space.flatten(a))
        agent_infos.append(agent_info)
        env_infos.append(env_info)
        path_length += 1
        if d:  # and not animated:  # TODO testing
            break
        o = next_o
        if animated:
            env.render()
            timestep = 0.05
            time.sleep(timestep / speedup)
            if save_video:
                from PIL import Image

                # import ipdb
                # ipdb.set_trace()

                # while 'get_viewer' not in dir(env):
                #     env = env.wrapped_env

                image = env.get_viewer().get_image()
                pil_image = Image.frombytes('RGB', (image[1], image[2]),
                                            image[0])
                images.append(np.flipud(np.array(pil_image)))

    if animated:
        if save_video and len(images) >= max_path_length:
            import moviepy.editor as mpy
            clip = mpy.ImageSequenceClip(images, fps=20 * speedup)
            if video_filename[-3:] == 'gif':
                clip.write_gif(video_filename, fps=20 * speedup)
            else:
                clip.write_videofile(video_filename, fps=20 * speedup)
        #return

    return dict(
        observations=tensor_utils.stack_tensor_list(observations),
        actions=tensor_utils.stack_tensor_list(actions),
        rewards=tensor_utils.stack_tensor_list(rewards),
        agent_infos=tensor_utils.stack_tensor_dict_list(agent_infos),
        env_infos=tensor_utils.stack_tensor_dict_list(env_infos),
    )
Example #44
0
 def extract_image(self,
                   workspace,
                   highlight_word=True,
                   left=300,
                   right=300,
                   top=15,
                   bottom=15,
                   force=False) -> Tuple[Path, Image.Image]:
     if self.cached_image_path.is_file() and not force:
         PDFToken.log.debug(f'cached_image_path: {self.cached_image_path}')
         try:
             img = Image.open(str(self.cached_image_path))
             PDFToken.log.debug(f'img: {img}')
             return self.cached_image_path, img
         except:
             PDFToken.log.error(
                 f'Error with image file, will attempt regeneration.\n{traceback.format_exc()}'
             )
             return self.extract_image(workspace,
                                       highlight_word,
                                       left,
                                       right,
                                       top,
                                       bottom,
                                       force=True)
     PDFToken.log.debug(f'Generating image for {self}')
     xref, pagerect, pix = workspace._cached_page_image(
         self.docid, self.page_n)  # TODO
     xscale = pix.width / pagerect.width
     yscale = pix.height / pagerect.height
     #PDFToken.log.debug(f'extract_image ({self.index}): {tokenrect} {xscale} {yscale}')
     image = Image.frombytes('RGB', (pix.width, pix.height), pix.samples)
     tokenrect = self.rect.irect * fitz.Matrix(xscale, yscale)
     #PDFToken.log.debug(f'tokenrect ({self.index}): {tokenrect}')
     #PDFToken.log.debug(f'word_image ({self.index}): {image} token {self} filename {self.cached_image_path}')
     if workspace.config.combine_hyphenated_images and self.is_hyphenated:
         next_token = workspace.docs[self.docid].tokens[self.index + 1]
         PDFToken.log.debug(
             f'Going to create combined image for {self} and {next_token}')
         _, next_token_img = next_token.extract_image(workspace,
                                                      highlight_word=False,
                                                      left=0,
                                                      right=right,
                                                      top=top,
                                                      bottom=bottom,
                                                      force=True)
         #PDFToken.log.debug(f'next_token_img ({self.index}): {next_token_img}')
         centering_offset = int(
             (tokenrect.height - next_token_img.height) / 2)
         #PDFToken.log.debug(f'centering_offset: ({tokenrect.height} - {next_token_img.height})/2 = {centering_offset}')
         paste_coords = (tokenrect.x1, tokenrect.y0 + centering_offset)
         #PDFToken.log.debug(f'paste_coords ({self.index}): {paste_coords}')
         image.paste(next_token_img, paste_coords)
         tokenrect.x1 += next_token_img.width - left
     croprect = (
         max(0, tokenrect.x0 - left),
         max(0, tokenrect.y0 - top),
         min(pix.width, tokenrect.x1 + right),
         min(pix.height, tokenrect.y1 + bottom),
     )
     #PDFToken.log.debug(f'extract_image ({self.index}): {croprect}')
     if highlight_word:
         draw = ImageDraw.Draw(image)
         if self.gold:
             color = (0x28, 0xa7, 0x45)  # bootstrap green #28a745
         else:
             color = (0xdc, 0x35, 0x45)  # bootstrap red #dc3545
         draw.rectangle(tokenrect, outline=color, width=3)
     image = image.crop(croprect)
     image.save(self.cached_image_path)
     return self.cached_image_path, image
Example #45
0
def pil_frombytes_rgb(im):
    return Image.frombytes('RGB', im.size, im.rgb).tobytes()
from PIL import Image, ImageEnhance

RECORD_SIZE = 8199
i = 0

files = glob.glob("ETL8G/*")
for filename in files:
    print("Reading {}".format(filename))
    _, file_pre = filename.split('/')
    with open(filename, 'rb') as f:
        while True:
            s = f.read(RECORD_SIZE)
            if s is None or len(s) < RECORD_SIZE:
                break
            r = struct.unpack(">HH8sIBBBBHHHHBB30x8128s11x", s)
            img = Image.frombytes('F', (128, 127), r[14], 'bit', (4, 0))
            img = img.convert('L')
            i = i + 1
            dirname = b'\x1b$B' + r[1].to_bytes(2, 'big') + b'\x1b(B'
            dirname = dirname.decode("iso-2022-jp")
            try:
                os.makedirs(f"extract/{dirname}")
            except:
                pass

            enhancer = ImageEnhance.Brightness(img)
            img = enhancer.enhance(16)

            imagefile = f"extract/{dirname}/{file_pre}_{i:0>6}.png"
            print(imagefile)
            img.save(imagefile)
Example #47
0
    def draw(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        lightAmbient = np.array([0.2, 0.2, 0.2, 1.0])
        lightDiffuse = np.array([10.5, 10.0, 10.0, 1.0])
        lightSpecular = np.array([0.5, 0.5, 0.5, 1.0])
        lPosition = np.append([self.camera.position], [1])
        lDirection = self.camera.forward

        #	glPushMatrix()
        gluLookAt(self.camera.position[0], self.camera.position[1],
                  self.camera.position[2],
                  self.camera.position[0] + self.camera.forward[0],
                  self.camera.position[1] + self.camera.forward[1],
                  self.camera.position[2] + self.camera.forward[2],
                  self.camera.up[0], self.camera.up[1], self.camera.up[2])

        glEnable(GL_LIGHTING)
        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightAmbient)
        #glLightModelfv( GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE )
        glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE)
        glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient)
        glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse)
        glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular)
        glLightfv(GL_LIGHT0, GL_POSITION, lPosition)
        glLightfv(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 7.5)
        glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0)
        glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lDirection)
        glLightfv(GL_LIGHT0, GL_SPOT_EXPONENT, 0.5)
        glEnable(GL_LIGHT0)
        #glDisable( GL_LIGHT0 )
        glDisable(GL_LIGHT1)
        glDisable(GL_LIGHT2)
        glDisable(GL_LIGHT3)
        glDisable(GL_LIGHT4)
        glDisable(GL_LIGHT5)
        glDisable(GL_LIGHT6)
        glDisable(GL_LIGHT7)

        #        vp = glGetIntegerv(GL_VIEWPORT)
        #        glRenderMode(GL_SELECT)
        #        gluPickMatrix(0, 0, width, height, vp)
        #        gluPerspective(45.0, float(width)/float(height), 0.0001, 50.0)
        #        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )

        self.create_vbo()
        self.draw_vbo
        draw_mesh()

        materialAmbient = np.array([9.0, 9.0, 9.0, 1.0])
        materialSpecular = np.array([0.5, 0.5, 0.5, 1.0])
        materialShininess = np.array([100.0])
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
                     materialAmbient)
        glMaterialfv(GL_FRONT, GL_SPECULAR, materialSpecular)
        glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess)

        #	glPopMatrix()
        glFlush()
        glutSwapBuffers()

        if self.return_from_func == 1:
            np.savetxt(self.file_name + '.txt',
                       zip(self.camera.position, self.camera.forward),
                       fmt="%f %f")
            glPixelStorei(GL_PACK_ALIGNMENT, 1)
            data = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE)
            saveimage = Image.frombytes("RGB", (width, height), data)
            saveimage = ImageOps.flip(saveimage)
            saveimage.save(self.file_name + '.png', 'PNG')

            sys.exit()
Example #48
0
 def dump(self):
     with Image.frombytes('RGB', self.size, self._buffer) as image:
         image.save('dumps/{}.png'.format(time.time()))
Example #49
0
def image(width, height):
    return Image.frombytes('RGB', (width, height), urandom(width * height * 3))
Example #50
0
                print(y)
                new_list.append(chunk[-1])
                found_bug += 1
    print(count)
    count += 1

# append files that can be read into new list
new_list = sc.parallelize(new_list).flatMap(lambda x: x).collect()
# load into training DF
image_data = spark.read.format("image").load(new_list).select(
    'image.origin', 'image.data', 'image.width', 'image.height')
image_data = image_data.rdd
print("1")
image_data = image_data.map(lambda x:
                            (str(x[0]),
                             Image.frombytes("RGB", (int(x[2]), int(x[3])),
                                             io.BytesIO(x[1]).getvalue())))
print("2")
image_data = image_data.map(lambda x: (x[0], np.asarray(x[1])))
print("3")
image_data = image_data.map(lambda x: (x[0], x[1][:, :, ::-1]))
print("4")
image_data = image_data.map(lambda x: (x[0], Image.fromarray(np.uint8(x[1]))))
print("5")
image_data = image_data.leftOuterJoin(image_df_crop_data)
print("6")
image_data = image_data.values()
print("7")

image_data = image_data.map(lambda x: (x[0].crop(
    (int(x[1][0]), int(x[1][1]), int(x[1][2]), int(x[1][3]))), x[1][4]))
print("8")
Example #51
0
    def __init__(self,
                 font,
                 string,
                 camera=None,
                 color=(255, 255, 255, 255),
                 shadow=(0, 0, 0, 255),
                 shadow_radius=0,
                 font_size=24,
                 margin=5.0,
                 justify='C',
                 background_color=None,
                 shader=None,
                 f_type='',
                 mipmap=True,
                 width=None):
        """Arguments:

    *font*:
      File path/name to a TrueType font file.

    *string*:
      String to write.

    *camera*:
      Camera object passed on to constructor of sprite

    *color*:
      Color in format '#RRGGBB', (255,0,0,255), 'orange' etc (as accepted 
      by PIL.ImageDraw) default (255, 255, 255, 255) i.e. white 100% alpha

    *shadow*:
      Color of shadow, default black.

    *shadow_radius*:
      Gaussian blur radius applied to shadow layer, default 0 (no shadow)

    *font_size*:
      Point size for drawing the letters on the internal Texture. default 24

    *margin*:
      Offsets from the top left corner for the text and space on right
      and bottom. default 5.0

    *justify*:
      L(eft), C(entre), R(ight) default C

    *background_color*:
      filled background in ImageDraw format as above. default None i.e.
      transparent. 

    *shader*:
      can be passed to init otherwise needs to be set in set_shader or
      draw. default None

    *f_type*:
      filter type. BUMP will generate a normal map (indented by default,
      +BUMP or BUMP+ will make it stick out), EMBOSS, CONTOUR, BLUR and 
      SMOOTH do what they sound like they will do.
    """
        super(FixedString, self).__init__(font, mipmap=mipmap)
        self.font = font
        try:
            imgfont = ImageFont.truetype(font, font_size)
        except IOError:
            abspath = os.path.abspath(font)
            msg = "Couldn't find font file '%s'" % font
            if font != abspath:
                msg = "%s - absolute path is '%s'" % (msg, abspath)
            raise Exception(msg)

        justify = justify.upper()
        f_type = f_type.upper()
        ascent, descent = imgfont.getmetrics()
        height = ascent + descent
        lines = string.split('\n')
        new_lines = []
        maxwid = 0
        for l in lines:
            line_wid = imgfont.getsize(l)[0]
            if width is not None and line_wid > width:
                new_line = ""
                space = ""
                words = l.split(" ")
                for word in words:
                    check_line = "{}{}{}".format(new_line, space, word)
                    if imgfont.getsize(check_line)[0] <= width:
                        new_line = check_line
                    else:  # wrap before this word TODO cope with lines with no spaces
                        if "-" in word:  # TODO make this a function to split first on " " then "-" then on
                            split_word = word.split("-")
                            pre = split_word[0]
                            post = "-".join(split_word[1:])
                            check_line = "{} {}-".format(new_line, pre)
                            if imgfont.getsize(check_line)[0] <= width:
                                new_line = check_line
                                word = post
                        new_lines.append(new_line)
                        new_line = word
                    space = " "
                new_lines.append(new_line)
            else:
                new_lines.append(l)
        lines = new_lines
        for l in lines:
            line_wid = imgfont.getsize(l)[0]
            if line_wid > maxwid:
                maxwid = line_wid
        maxwid += 2.0 * margin
        if Display.INSTANCE is not None:
            max_size = Display.INSTANCE.opengl.max_texture_size.value
        else:
            max_size = MAX_SIZE
        texture_wid = min(int(maxwid / 4) * 4, max_size)
        nlines = len(lines)
        texture_hgt = int(nlines * height + 2 * margin)

        self.im = Image.new("RGBA", (texture_wid, texture_hgt),
                            background_color)
        self.ix, self.iy = texture_wid, texture_hgt
        draw = ImageDraw.Draw(self.im)
        if shadow_radius > 0:
            from PIL import ImageFilter
            self._render_text(lines, justify, margin, imgfont, maxwid, height,
                              shadow, draw)
            self.im = self.im.filter(
                ImageFilter.GaussianBlur(radius=shadow_radius))
            if background_color == None:
                im_arr = self._force_color(np.array(self.im), shadow)
                try:  # numpy not quite working fully in pypy so have to convert tobytes
                    self.im = Image.fromarray(im_arr)
                except:
                    h, w, c = im_arr.shape
                    rgb = 'RGB' if c == 3 else 'RGBA'
                    self.im = Image.frombytes(rgb, (w, h), im_arr.tobytes())

            draw = ImageDraw.Draw(self.im)

        self._render_text(lines, justify, margin, imgfont, maxwid, height,
                          color, draw)

        force_color = background_color is None and shadow_radius == 0
        if f_type == '':
            self.image = np.array(self.im)
        elif 'BUMP' in f_type:
            amount = -1.0 if '+' in f_type else 1.0
            self.image = self._normal_map(np.array(self.im, dtype=np.uint8),
                                          amount)
            force_color = False

        else:
            from PIL import ImageFilter
            if f_type == 'EMBOSS':
                self.im = self.im.filter(ImageFilter.EMBOSS)
            elif f_type == 'CONTOUR':
                self.im = self.im.filter(ImageFilter.CONTOUR)
            elif f_type == 'BLUR':
                self.im = self.im.filter(ImageFilter.BLUR)
            elif f_type == 'SMOOTH':
                self.im = self.im.filter(ImageFilter.SMOOTH_MORE)
            self.image = np.array(self.im)

        if force_color:
            self.image = self._force_color(self.image, color)

        self._tex = ctypes.c_uint()

        bmedge = nlines * height + 2.0 * margin
        self.sprite = Sprite(camera=camera, w=maxwid, h=bmedge)
        buf = self.sprite.buf[0]  #convenience alias
        buf.textures = [self]
        if shader != None:
            self.sprite.shader = shader
            buf.shader = shader
        buf.unib[6] = float(maxwid / texture_wid)  #scale to fit
        buf.unib[7] = float(bmedge / texture_hgt)
Example #52
0
 def pixel_image(self, image_data: dict, pixel_coordinates: tuple):
     '''Creates a new image based on the color data of a given pixel'''
     color = image_data[pixel_coordinates]['rgb']
     text = image_data[pixel_coordinates]['text']
     img = self.create_pixel_img(self.scale, text, color)
     return Image.frombytes("RGB", size=img.size, data=img.tobytes())
Example #53
0
def ScreenGrab():
	im=mss().grab(bbox)
	im=Image.frombytes("RGB", im.size, im.bgra, "raw", "BGRX")
	return im
Example #54
0
def ArrayToImage(arr,
                 high=255,
                 low=0,
                 cmin=None,
                 cmax=None,
                 pal=None,
                 mode=None,
                 channel_axis=None):
    """Takes a numpy array and returns a PIL image.
    The mode of the PIL image depends on the array shape and the `pal` and
    `mode` keywords.

    For 2-D arrays, if `pal` is a valid (N,3) byte-array giving the RGB values
    (from 0 to 255) then ``mode='P'``, otherwise ``mode='L'``, unless mode
    is given as 'F' or 'I' in which case a float and/or integer array is made.

    Notes
    -----
    For 3-D arrays, the `channel_axis` argument tells which dimension of the
    array holds the channel data.

    For 3-D arrays if one of the dimensions is 3, the mode is 'RGB'
    by default or 'YCbCr' if selected.

    The numpy array must be either 2 dimensional or 3 dimensional.

    """
    data = asarray(arr)
    if iscomplexobj(data):
        raise ValueError("Cannot convert a complex-valued array.")
    shape = list(data.shape)
    valid = len(shape) == 2 or ((len(shape) == 3) and ((3 in shape) or
                                                       (4 in shape)))
    if not valid:
        raise ValueError("'arr' does not have a suitable array shape for "
                         "any mode.")
    if len(shape) == 2:
        shape = (shape[1], shape[0])  # columns show up first
        if mode == 'F':
            data32 = data.astype(numpy.float32)
            image = Image.frombytes(mode, shape, data32.tostring())
            return image
        if mode in [None, 'L', 'P']:
            bytedata = bytescale(data,
                                 high=high,
                                 low=low,
                                 cmin=cmin,
                                 cmax=cmax)
            image = Image.frombytes('L', shape, bytedata.tostring())
            if pal is not None:
                image.putpalette(asarray(pal, dtype=uint8).tostring())
                # Becomes a mode='P' automagically.
            elif mode == 'P':  # default gray-scale
                pal = (arange(0, 256, 1, dtype=uint8)[:, newaxis] * ones(
                    (3, ), dtype=uint8)[newaxis, :])
                image.putpalette(asarray(pal, dtype=uint8).tostring())
            return image
        if mode == '1':  # high input gives threshold for 1
            bytedata = (data > high)
            image = Image.frombytes('1', shape, bytedata.tostring())
            return image
        if cmin is None:
            cmin = amin(ravel(data))
        if cmax is None:
            cmax = amax(ravel(data))
        data = (data * 1.0 - cmin) * (high - low) / (cmax - cmin) + low
        if mode == 'I':
            data32 = data.astype(numpy.uint32)
            image = Image.frombytes(mode, shape, data32.tostring())
        else:
            raise ValueError(_errstr)
        return image

    # if here then 3-d array with a 3 or a 4 in the shape length.
    # Check for 3 in datacube shape --- 'RGB' or 'YCbCr'
    if channel_axis is None:
        if (3 in shape):
            ca = numpy.flatnonzero(asarray(shape) == 3)[0]
        else:
            ca = numpy.flatnonzero(asarray(shape) == 4)
            if len(ca):
                ca = ca[0]
            else:
                raise ValueError("Could not find channel dimension.")
    else:
        ca = channel_axis

    numch = shape[ca]
    if numch not in [3, 4]:
        raise ValueError("Channel axis dimension is not valid.")

    bytedata = bytescale(data, high=high, low=low, cmin=cmin, cmax=cmax)
    if ca == 2:
        strdata = bytedata.tostring()
        shape = (shape[1], shape[0])
    elif ca == 1:
        strdata = transpose(bytedata, (0, 2, 1)).tostring()
        shape = (shape[2], shape[0])
    elif ca == 0:
        strdata = transpose(bytedata, (1, 2, 0)).tostring()
        shape = (shape[2], shape[1])
    if mode is None:
        if numch == 3:
            mode = 'RGB'
        else:
            mode = 'RGBA'

    if mode not in ['RGB', 'RGBA', 'YCbCr', 'CMYK']:
        raise ValueError(_errstr)

    if mode in ['RGB', 'YCbCr']:
        if numch != 3:
            raise ValueError("Invalid array shape for mode.")
    if mode in ['RGBA', 'CMYK']:
        if numch != 4:
            raise ValueError("Invalid array shape for mode.")

    # Here we know data and mode is correct
    image = Image.frombytes(mode, shape, strdata)
    return image
Example #55
0
    def update(self, dt):
        """ This method is scheduled to be called repeatedly by the pyglet
        clock.

        Parameters
        ----------
        dt : float
            The change in time since the last call.

        """
        self.world_counter += 1

        if self.world_counter % WORLD_COUNTER_DISPLAY_FREQUENCY == 0:
            print "FRAME #" + str(self.world_counter)

        if self.world_counter >= self.max_frames:
            self.player.save()
            self.player.log.logMessage("Game Over!\tFINAL SCORE: %d" %
                                       self.player.total_score)
            pyglet.app.exit()
            #sys.exit(1)

        self.model.process_queue()
        sector = sectorize(self.player.position)
        if sector != self.sector:
            self.model.change_sectors(self.sector, sector)
            if self.sector is None:
                self.model.process_entire_queue()
            self.sector = sector
        m = 8
        dt = min(dt, 0.2)
        for _ in xrange(m):
            self._update(dt / m)

        #self.player.update()
        if self.world_counter % DECISION_FREQUENCY == 0:

            PIXEL_BYTE_SIZE = 1  # Use 1 for grayscale, 4 for RGBA

            # Initialize an array to store the screenshot pixels
            screenshot = (GLubyte *
                          (PIXEL_BYTE_SIZE * self.width * self.height))(0)

            # Grab a screenshot
            # Use GL_RGB for color and GL_LUMINANCE for grayscale!
            #glReadPixels(0, 0, self.width, self.height, GL_RGB, GL_UNSIGNED_BYTE, screenshot)
            glReadPixels(0, 0, self.width, self.height, GL_LUMINANCE,
                         GL_UNSIGNED_BYTE, screenshot)

            im = Image.frombytes(mode="L",
                                 size=(WINDOW_SIZE, WINDOW_SIZE),
                                 data=screenshot)
            im = im.transpose(Image.FLIP_TOP_BOTTOM)

            # If you want to see the agent's view, then you can save a screenshot

            # im.save('screenshots/test%d.png' % time.time())
            # print("SAVED SCREENSHOT on frame #", self.world_counter)
            # time.sleep(1)

            # Another way to save screenshots...might be slower or faster
            #pyglet.image.get_buffer_manager().get_color_buffer().save('screenshots/test%d.png' % time.time())

            frame = Frame(im)

            self.player.getDecision(frame)
def read_record_ETL8B2(f):
    s = f.read(512)
    r = struct.unpack('>2H4s504s', s)
    i1 = Image.frombytes('1', (64, 63), r[3], 'raw')
    return r + (i1, )
            "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
            'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])),
        }))
        writer.write(example.SerializeToString())  # Serialize To String
writer.close()


## Load Data Method 1: Simple read ============================================
# read data one by one in order
for serialized_example in tf.python_io.tf_record_iterator("train.tfrecords"):
    example = tf.train.Example()    # SequenceExample for seuqnce example
    example.ParseFromString(serialized_example)
    img_raw = example.features.feature['img_raw'].bytes_list.value
    label = example.features.feature['label'].int64_list.value
    ## converts a image from bytes
    image = Image.frombytes('RGB', (224, 224), img_raw[0])
    tl.visualize.frame(np.asarray(image), second=0.5, saveable=False, name='frame', fig_idx=1283)
    print(label)


## Read Data Method 2: Queue and Thread =======================================
# use sess.run to get a batch of data
def read_and_decode(filename):
    # generate a queue with a given file name
    filename_queue = tf.train.string_input_producer([filename])
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)     # return the file and the name of file
    features = tf.parse_single_example(serialized_example,  # see parse_single_sequence_example for sequence example
                                       features={
                                           'label': tf.FixedLenFeature([], tf.int64),
                                           'img_raw' : tf.FixedLenFeature([], tf.string),
Example #58
0
def makeMaps(worldFolder, outputFolder, serverType, unlimitedTracking=False):
    nbtMapData = []
    if serverType == "bds":
        # open leveldb
        db = leveldb.open(os.path.join(worldFolder, "db"))

        # iterate over all the maps
        for a in tqdm(leveldb.iterate(db),
                      "leveldb map keys -> nbt".ljust(24),
                      bar_format="{l_bar}{bar}"):
            key = bytearray(a[0])
            if b"map" in key:
                # get extract an nbt object
                mapNbtIo = BytesIO(a[1])
                mapNbtFile = nbtlib.File.parse(mapNbtIo, byteorder="little")
                mapNbt = mapNbtFile.root
                mapId = int(mapNbt["mapId"])
                epoch = 0
                nbtMapData.append({"epoch": epoch, "id": mapId, "nbt": mapNbt})

    elif serverType == "java":
        mapDatFiles = findMapFiles(worldFolder)
        for mapDatFile in tqdm(mapDatFiles,
                               "map_*.dat -> nbt".ljust(24),
                               bar_format="{l_bar}{bar}"):
            mapNbtFile = nbtlib.load(mapDatFile)
            mapNbt = mapNbtFile.root["data"]
            mapId = int(os.path.basename(mapDatFile)[4:-4])
            epoch = int(os.path.getmtime(mapDatFile))
            nbtMapData.append({"epoch": epoch, "id": mapId, "nbt": mapNbt})

    maps = []
    os.makedirs(outputFolder, exist_ok=True)
    mapPngs = getMapPngs(outputFolder)

    currentIds = {x.mapId: x for x in mapPngs}

    for nbtMap in tqdm(nbtMapData,
                       "nbt -> png".ljust(24),
                       bar_format="{l_bar}{bar}"):
        mapId = nbtMap["id"]
        mapNbt = nbtMap["nbt"]
        mapEpoch = nbtMap["epoch"]
        try:
            mapUnlimitedTracking = mapNbt["unlimitedTracking"]
        except KeyError:
            mapUnlimitedTracking = False

        if mapUnlimitedTracking and not unlimitedTracking:
            continue
        scale = int(mapNbt["scale"])
        x = int(mapNbt["xCenter"])
        z = int(mapNbt["zCenter"])

        dimension = mapNbt["dimension"]
        mapColors = mapNbt["colors"]

        if type(dimension) == nbtlib.tag.Int:
            dimension = dimDict[mapNbt["dimension"]]
        elif type(dimension) == nbtlib.tag.Byte:
            dimension = dimDict[mapNbt["dimension"]]
        else:
            dimension = dimension.strip('"')
        dimension = dimension.replace(":", "@")
        try:
            mapBanners = mapNbt["banners"]
        except KeyError:
            mapBanners = []

        banners = set()
        for banner in mapBanners:
            X = int(banner["Pos"]["X"])
            Y = int(banner["Pos"]["Y"])
            Z = int(banner["Pos"]["Z"])
            color = banner["Color"]

            try:
                name = json.loads(banner["Name"])["text"]

            except KeyError:
                name = ""

            bannerDict = {
                "X": X,
                "Y": Y,
                "Z": Z,
                "color": color,
                "name": name,
                "dimension": dimension
            }
            bannerTuple = BannerTuple(**bannerDict)
            banners.add(bannerTuple)

        frames = []
        # logging.debug(mapColors)

        if serverType == "bds":
            mapImage = Image.frombytes("RGBA", (128, 128),
                                       bytes([x % 256 for x in mapColors]),
                                       'raw')
        elif serverType == "java":
            colorTuples = [allColors[x % 256] for x in mapColors]
            mapImage = Image.new("RGBA", (128, 128))
            mapImage.putdata(colorTuples)

        mapHash = hashlib.md5(mapImage.tobytes()).hexdigest()

        # empty map
        if mapHash == "fcd6bcb56c1689fcef28b57c22475bad":
            continue

        if mapId not in currentIds:
            # brand new image
            logging.debug("%s is a new map", mapId)
            epoch = mapEpoch
        else:
            # changed image
            logging.debug("%s is already known", mapId)
            if mapHash != currentIds.get(mapId).mapHash:
                # map has changed based on the hash

                logging.debug("%s changed and will get an updated epoch",
                              mapId)
                epoch = now if not mapEpoch else mapEpoch

            elif mapEpoch > currentIds.get(mapId).epoch:
                logging.debug(
                    "%s has a more recent epoch from it's dat file, updating",
                    mapId)
                epoch = mapEpoch

            else:
                logging.debug("%s has not changed and will keep it's epoch",
                              mapId)
                epoch = currentIds.get(mapId).epoch

        mapPng = MapPngTuple(mapId=mapId,
                             mapHash=mapHash,
                             epoch=epoch,
                             dimension=dimension,
                             x=x,
                             z=z,
                             scale=scale)

        mapImage = mapImage.resize((128 * 2**scale, ) * 2, Image.NEAREST)
        filename = mapPngFilenameFormat.format(**mapPng._asdict())

        try:
            oldFilename = mapPngFilenameFormat.format(
                **currentIds.get(mapId)._asdict())
            os.remove(os.path.join(outputFolder, oldFilename))
        except:
            logging.debug("%s isn't there, didn't delete", mapId)

        mapImage.save(os.path.join(outputFolder, filename))

        mapData = MapTuple(mapData=mapPng,
                           bannerData=banners,
                           frameData=frames)
        maps.append(mapData)

    logging.debug(maps)
    logging.info("Processed %s maps", len(maps))

    return maps
    def saveToImage(self, targetImagePath):
        if (self.xml == 'y'):  #if already an XML, get the encoded version
            self.encodeOriginalText()
            if (self.msgType != 'GrayImage') and (
                    self.msgType !=
                    'ColorImage'):  #if not image then raise TypeError
                raise TypeError('Not an image file!!!')
            if (len(self.encodedString) == 0):  #encoded string has nothing
                raise Exception

            if (self.msgType == 'ColorImage'):  #only if its a color image
                decoded = list(bytes(base64.b64decode(self.encodedString)))
                # print(len(decoded))
                # print(self.msgSize)
                red_data = []
                green_data = []
                blue_data = []
                #print(len(decoded))
                red_end = int(len(decoded) / 3)
                green_end = int(red_end * 2)
                #print(red_end)
                #print(green_end)
                blue_end = red_end * 3
                #print(blue_end)
                for i in range(0, red_end):
                    red_data.append(decoded[i])
                for i in range(red_end, green_end):
                    green_data.append(decoded[i])
                for i in range(green_end, blue_end):
                    blue_data.append(decoded[i])
                total_data = []  #the total data

                for i in range(0, len(red_data)):  #since lengths are the same
                    total_data.append((red_data[i], green_data[i],
                                       blue_data[i]))  #make the tuple
                #print(total_data)
                '''
                #print(len(total_data))
                #print(len(red_data))
                #print(len(green_data))
                #print(len(blue_data))
                '''
                # tup = tuple
                w = self.msgSize.split(',')[0]
                h = self.msgSize.split(',')[1]
                tup = (int(w), int(h))  #get the dimensions
                #print(tup)
                im_new = Image.new('RGB', tup)
                im_new.putdata(total_data)
                im_new.save(
                    targetImagePath)  #save to the correct output file name

            else:  #else if gray image
                decoded = base64.b64decode(self.encodedString)  #decode
                tup = tuple
                w = self.msgSize.split(',')[0]
                h = self.msgSize.split(',')[1]
                tup = (int(w), int(h))  #get the dimensions
                im_new = Image.frombytes('L', tup, decoded)
                im_new.save(
                    targetImagePath)  #save to the correct output file name

        else:  #not an xml string, ie just a gray image ir color image
            self.getXmlString()
            if (self.msgType != 'GrayImage') and (
                    self.msgType !=
                    'ColorImage'):  #if not image then raise TypeError
                raise TypeError('Not an image file!!!')
            if (len(self.encodedString) == 0):  #encoded string has nothing
                raise Exception
            if (self.msgType == 'GrayImage'):  #if its a gray image
                decoded = base64.b64decode(self.encodedString)  #decode
                #print(decoded)
                tup = tuple
                w = self.msgSize.split(',')[0]
                h = self.msgSize.split(',')[1]
                tup = (int(w), int(h))  #get the dimensions
                im_new = Image.frombytes('L', tup, decoded)
                im_new.save(
                    targetImagePath)  #save to the correct output file name
            else:  #if its a color image
                red_data = list(bytes(base64.b64decode(self.redEncoded)))
                green_data = list(bytes(base64.b64decode(self.greenEncoded)))
                blue_data = list(bytes(base64.b64decode(self.blueEncoded)))
                total_data = []  #the total data
                for i in range(0, len(red_data)):  #since lengths are the same
                    total_data.append((red_data[i], green_data[i],
                                       blue_data[i]))  #make the tuple
                #print(total_data)
                tup = tuple
                w = self.msgSize.split(',')[0]
                h = self.msgSize.split(',')[1]
                tup = (int(w), int(h))  #get the dimensions
                #print(tup)
                im_new = Image.new('RGB', tup)
                im_new.putdata(total_data)
                im_new.save(
                    targetImagePath)  #save to the correct output file name
import numpy as np
import cv2
from mss import mss
from PIL import Image

sct = mss()

while 1:
    w, h = 800, 640
    monitor = {'top': 0, 'left': 0, 'width': w, 'height': h}
    img = Image.frombytes('RGB', (w, h), sct.grab(monitor).rgb)
    cv2.imshow('test', np.array(img))
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break