Python png.Reader() Examples

The following are 30 code examples of png.Reader(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module png , or try the search function .
Example #1
Source File: flowlib.py    From flownet2.pytorch with Apache License 2.0 6 votes vote down vote up
def read_disp_png(file_name):
    """
    Read optical flow from KITTI .png file
    :param file_name: name of the flow file
    :return: optical flow data in matrix
    """
    image_object = png.Reader(filename=file_name)
    image_direct = image_object.asDirect()
    image_data = list(image_direct[2])
    (w, h) = image_direct[3]['size']
    channel = len(image_data[0]) / w
    flow = np.zeros((h, w, channel), dtype=np.uint16)
    for i in range(len(image_data)):
        for j in range(channel):
            flow[i, :, j] = image_data[i][j::channel]
    return flow[:, :, 0] / 256 
Example #2
Source File: compute_gt_poses.py    From ObjectDatasetTools with MIT License 6 votes vote down vote up
def load_images(path, ID):
    
    """
    Load a color and a depth image by path and image ID 

    """
    global camera_intrinsics
    
    img_file = path + 'JPEGImages/%s.jpg' % (ID*LABEL_INTERVAL)
    cad = cv2.imread(img_file)

    depth_file = path + 'depth/%s.png' % (ID*LABEL_INTERVAL)
    reader = png.Reader(depth_file)
    pngdata = reader.read()
    # depth = np.vstack(map(np.uint16, pngdata[2]))
    depth = np.array(tuple(map(np.uint16, pngdata[2])))
    pointcloud = convert_depth_frame_to_pointcloud(depth, camera_intrinsics)


    return (cad, pointcloud) 
Example #3
Source File: flowlib.py    From DF-Net with MIT License 6 votes vote down vote up
def read_disp_png(file_name):
    """
    Read optical flow from KITTI .png file
    :param file_name: name of the flow file
    :return: optical flow data in matrix
    """
    image_object = png.Reader(filename=file_name)
    image_direct = image_object.asDirect()
    image_data = list(image_direct[2])
    (w, h) = image_direct[3]['size']
    channel = len(image_data[0]) / w
    flow = np.zeros((h, w, channel), dtype=np.uint16)
    for i in range(len(image_data)):
        for j in range(channel):
            flow[i, :, j] = image_data[i][j::channel]
    return flow[:, :, 0] / 256 
Example #4
Source File: flowlib.py    From DF-Net with MIT License 6 votes vote down vote up
def read_flow_png(flow_file):
    """
    Read optical flow from KITTI .png file
    :param flow_file: name of the flow file
    :return: optical flow data in matrix
    """
    flow_object = png.Reader(filename=flow_file)
    flow_direct = flow_object.asDirect()
    flow_data = list(flow_direct[2])
    (w, h) = flow_direct[3]['size']
    flow = np.zeros((h, w, 3), dtype=np.float64)
    for i in range(len(flow_data)):
        flow[i, :, 0] = flow_data[i][0::3]
        flow[i, :, 1] = flow_data[i][1::3]
        flow[i, :, 2] = flow_data[i][2::3]

    invalid_idx = (flow[:, :, 2] == 0)
    flow[:, :, 0:2] = (flow[:, :, 0:2] - 2 ** 15) / 64.0
    flow[invalid_idx, 0] = 0
    flow[invalid_idx, 1] = 0
    return flow 
Example #5
Source File: flowlib.py    From flownet2-tf with MIT License 6 votes vote down vote up
def read_disp_png(file_name):
    """
    Read optical flow from KITTI .png file
    :param file_name: name of the flow file
    :return: optical flow data in matrix
    """
    image_object = png.Reader(filename=file_name)
    image_direct = image_object.asDirect()
    image_data = list(image_direct[2])
    (w, h) = image_direct[3]['size']
    channel = len(image_data[0]) / w
    flow = np.zeros((h, w, channel), dtype=np.uint16)
    for i in range(len(image_data)):
        for j in range(channel):
            flow[i, :, j] = image_data[i][j::channel]
    return flow[:, :, 0] / 256 
Example #6
Source File: flowlib.py    From flownet2-tf with MIT License 6 votes vote down vote up
def read_flow_png(flow_file):
    """
    Read optical flow from KITTI .png file
    :param flow_file: name of the flow file
    :return: optical flow data in matrix
    """
    flow_object = png.Reader(filename=flow_file)
    flow_direct = flow_object.asDirect()
    flow_data = list(flow_direct[2])
    (w, h) = flow_direct[3]['size']
    flow = np.zeros((h, w, 3), dtype=np.float64)
    for i in range(len(flow_data)):
        flow[i, :, 0] = flow_data[i][0::3]
        flow[i, :, 1] = flow_data[i][1::3]
        flow[i, :, 2] = flow_data[i][2::3]

    invalid_idx = (flow[:, :, 2] == 0)
    flow[:, :, 0:2] = (flow[:, :, 0:2] - 2 ** 15) / 64.0
    flow[invalid_idx, 0] = 0
    flow[invalid_idx, 1] = 0
    return flow 
Example #7
Source File: pypng_backend.py    From nnabla with Apache License 2.0 6 votes vote down vote up
def accept(self, path, ext, operator):
        if operator == "resize":
            return 'NG'
        elif operator == "save":
            return 'OK'
        else:
            if ext == '.png':
                f = path if hasattr(path, "read") else open(path, "rb")

                r = png.Reader(file=f)
                width, height, pixels, metadata = r.asDirect()
                f.seek(0)
                bit_depth = metadata.get("bitdepth")

                if bit_depth not in [8, 16]:
                    return "NG"
                else:
                    return "Recommended"
            else:
                return "NG" 
Example #8
Source File: load_flow.py    From DenseMatchingBenchmark with MIT License 6 votes vote down vote up
def load_png(file_path):
    """
    Read from KITTI .png file
    Args:
        file_path string: file path(absolute)
    Returns:
        data (numpy.array): data of image in (Height, Width, 3) layout
    """
    flow_object = png.Reader(filename=file_path)
    flow_direct = flow_object.asDirect()
    flow_data = list(flow_direct[2])
    (w, h) = flow_direct[3]['size']

    flow = np.zeros((h, w, 3), dtype=np.float64)
    for i in range(len(flow_data)):
        flow[i, :, 0] = flow_data[i][0::3]
        flow[i, :, 1] = flow_data[i][1::3]
        flow[i, :, 2] = flow_data[i][2::3]

    invalid_idx = (flow[:, :, 2] == 0)
    flow[:, :, 0:2] = (flow[:, :, 0:2] - 2 ** 15) / 64.0
    flow[invalid_idx, 0] = 0
    flow[invalid_idx, 1] = 0

    return flow.astype(np.float32) 
Example #9
Source File: flowlib.py    From uois with GNU General Public License v3.0 6 votes vote down vote up
def read_disp_png(file_name):
    """
    Read optical flow from KITTI .png file
    :param file_name: name of the flow file
    :return: optical flow data in matrix
    """
    image_object = png.Reader(filename=file_name)
    image_direct = image_object.asDirect()
    image_data = list(image_direct[2])
    (w, h) = image_direct[3]['size']
    channel = len(image_data[0]) / w
    flow = np.zeros((h, w, channel), dtype=np.uint16)
    for i in range(len(image_data)):
        for j in range(channel):
            flow[i, :, j] = image_data[i][j::channel]
    return flow[:, :, 0] / 256 
Example #10
Source File: flowlib.py    From uois with GNU General Public License v3.0 6 votes vote down vote up
def read_flow_png(flow_file):
    """
    Read optical flow from KITTI .png file
    :param flow_file: name of the flow file
    :return: optical flow data in matrix
    """
    flow_object = png.Reader(filename=flow_file)
    flow_direct = flow_object.asDirect()
    flow_data = list(flow_direct[2])
    (w, h) = flow_direct[3]['size']
    flow = np.zeros((h, w, 3), dtype=np.float64)
    for i in range(len(flow_data)):
        flow[i, :, 0] = flow_data[i][0::3]
        flow[i, :, 1] = flow_data[i][1::3]
        flow[i, :, 2] = flow_data[i][2::3]

    invalid_idx = (flow[:, :, 2] == 0)
    flow[:, :, 0:2] = (flow[:, :, 0:2] - 2 ** 15) / 64.0
    flow[invalid_idx, 0] = 0
    flow[invalid_idx, 1] = 0
    return flow 
Example #11
Source File: flowlib.py    From GapFlyt with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def read_disp_png(file_name):
    """
    Read optical flow from KITTI .png file
    :param file_name: name of the flow file
    :return: optical flow data in matrix
    """
    image_object = png.Reader(filename=file_name)
    image_direct = image_object.asDirect()
    image_data = list(image_direct[2])
    (w, h) = image_direct[3]['size']
    channel = len(image_data[0]) / w
    flow = np.zeros((h, w, channel), dtype=np.uint16)
    for i in range(len(image_data)):
        for j in range(channel):
            flow[i, :, j] = image_data[i][j::channel]
    return flow[:, :, 0] / 256 
Example #12
Source File: flowlib.py    From GapFlyt with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def read_flow_png(flow_file):
    """
    Read optical flow from KITTI .png file
    :param flow_file: name of the flow file
    :return: optical flow data in matrix
    """
    flow_object = png.Reader(filename=flow_file)
    flow_direct = flow_object.asDirect()
    flow_data = list(flow_direct[2])
    (w, h) = flow_direct[3]['size']
    flow = np.zeros((h, w, 3), dtype=np.float64)
    for i in range(len(flow_data)):
        flow[i, :, 0] = flow_data[i][0::3]
        flow[i, :, 1] = flow_data[i][1::3]
        flow[i, :, 2] = flow_data[i][2::3]

    invalid_idx = (flow[:, :, 2] == 0)
    flow[:, :, 0:2] = (flow[:, :, 0:2] - 2 ** 15) / 64.0
    flow[invalid_idx, 0] = 0
    flow[invalid_idx, 1] = 0
    return flow 
Example #13
Source File: flowlib.py    From flownet2.pytorch with Apache License 2.0 6 votes vote down vote up
def read_flow_png(flow_file):
    """
    Read optical flow from KITTI .png file
    :param flow_file: name of the flow file
    :return: optical flow data in matrix
    """
    flow_object = png.Reader(filename=flow_file)
    flow_direct = flow_object.asDirect()
    flow_data = list(flow_direct[2])
    (w, h) = flow_direct[3]['size']
    flow = np.zeros((h, w, 3), dtype=np.float64)
    for i in range(len(flow_data)):
        flow[i, :, 0] = flow_data[i][0::3]
        flow[i, :, 1] = flow_data[i][1::3]
        flow[i, :, 2] = flow_data[i][2::3]

    invalid_idx = (flow[:, :, 2] == 0)
    flow[:, :, 0:2] = (flow[:, :, 0:2] - 2 ** 15) / 64.0
    flow[invalid_idx, 0] = 0
    flow[invalid_idx, 1] = 0
    return flow 
Example #14
Source File: myflowlib.py    From Fully-Automatic-Video-Colorization-with-Self-Regularization-and-Diversity with MIT License 6 votes vote down vote up
def read_flow_png(flow_file):
    """
    Read optical flow from KITTI .png file
    :param flow_file: name of the flow file
    :return: optical flow data in matrix
    """
    flow_object = png.Reader(filename=flow_file)
    flow_direct = flow_object.asDirect()
    flow_data = list(flow_direct[2])
    (w, h) = flow_direct[3]['size']
    flow = np.zeros((h, w, 3), dtype=np.float64)
    for i in range(len(flow_data)):
        flow[i, :, 0] = flow_data[i][0::3]
        flow[i, :, 1] = flow_data[i][1::3]
        flow[i, :, 2] = flow_data[i][2::3]

    invalid_idx = (flow[:, :, 2] == 0)
    flow[:, :, 0:2] = (flow[:, :, 0:2] - 2 ** 15) / 64.0
    flow[invalid_idx, 0] = 0
    flow[invalid_idx, 1] = 0
    return flow 
Example #15
Source File: myflowlib.py    From Fully-Automatic-Video-Colorization-with-Self-Regularization-and-Diversity with MIT License 6 votes vote down vote up
def read_disp_png(file_name):
    """
    Read optical flow from KITTI .png file
    :param file_name: name of the flow file
    :return: optical flow data in matrix
    """
    image_object = png.Reader(filename=file_name)
    image_direct = image_object.asDirect()
    image_data = list(image_direct[2])
    (w, h) = image_direct[3]['size']
    channel = len(image_data[0]) / w
    flow = np.zeros((h, w, channel), dtype=np.uint16)
    for i in range(len(image_data)):
        for j in range(channel):
            flow[i, :, j] = image_data[i][j::channel]
    return flow[:, :, 0] / 256 
Example #16
Source File: kitti_combined.py    From irr with Apache License 2.0 6 votes vote down vote up
def read_png_flow(flow_file):
    flow_object = png.Reader(filename=flow_file)
    flow_direct = flow_object.asDirect()
    flow_data = list(flow_direct[2])
    (w, h) = flow_direct[3]['size']
    flow = np.zeros((h, w, 3), dtype=np.float64)
    for i in range(len(flow_data)):
        flow[i, :, 0] = flow_data[i][0::3]
        flow[i, :, 1] = flow_data[i][1::3]
        flow[i, :, 2] = flow_data[i][2::3]

    invalid_idx = (flow[:, :, 2] == 0)
    flow[:, :, 0:2] = (flow[:, :, 0:2] - 2 ** 15) / 64.0
    flow[invalid_idx, 0] = 0
    flow[invalid_idx, 1] = 0
    return flow[:, :, 0:2], (1 - invalid_idx * 1)[:, :, None] 
Example #17
Source File: flowlib.py    From cc with MIT License 6 votes vote down vote up
def read_png_file(flow_file):
    """
    Read from KITTI .png file
    :param flow_file: name of the flow file
    :return: optical flow data in matrix
    """
    flow_object = png.Reader(filename=flow_file)
    flow_direct = flow_object.asDirect()
    flow_data = list(flow_direct[2])
    (w, h) = flow_direct[3]['size']
    print("Reading %d x %d flow file in .png format" % (h, w))
    flow = np.zeros((h, w, 3), dtype=np.float64)
    for i in range(len(flow_data)):
        flow[i, :, 0] = flow_data[i][0::3]
        flow[i, :, 1] = flow_data[i][1::3]
        flow[i, :, 2] = flow_data[i][2::3]

    invalid_idx = (flow[:, :, 2] == 0)
    flow[:, :, 0:2] = (flow[:, :, 0:2] - 2 ** 15) / 64.0
    flow[invalid_idx, 0] = 0
    flow[invalid_idx, 1] = 0
    return flow 
Example #18
Source File: flowlib.py    From cc with MIT License 6 votes vote down vote up
def read_disp_png(file_name):
    """
    Read optical flow from KITTI .png file
    :param file_name: name of the flow file
    :return: optical flow data in matrix
    """
    image_object = png.Reader(filename=file_name)
    image_direct = image_object.asDirect()
    image_data = list(image_direct[2])
    (w, h) = image_direct[3]['size']
    channel = len(image_data[0]) / w
    flow = np.zeros((h, w, channel), dtype=np.uint16)
    for i in range(len(image_data)):
        for j in range(channel):
            flow[i, :, j] = image_data[i][j::channel]
    return flow[:, :, 0] / 256 
Example #19
Source File: flow_io.py    From cc with MIT License 6 votes vote down vote up
def flow_read_png(fpath):
    """
    Read KITTI optical flow, returns u,v,valid mask

    """
    if not has_png:
        print('Error. Please install the PyPNG library')
        return

    R = png.Reader(fpath)
    width,height,data,_ = R.asDirect()
    # This only worked with python2.
    #I = np.array(map(lambda x:x,data)).reshape((height,width,3))
    I = np.array([x for x in data]).reshape((height,width,3))
    u_ = I[:,:,0]
    v_ = I[:,:,1]
    valid = I[:,:,2]

    u = (u_.astype('float64')-2**15)/64.0
    v = (v_.astype('float64')-2**15)/64.0

    return u,v,valid 
Example #20
Source File: FlowUtils.py    From EVDodgeNet with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def read_png_file(flow_file):
    """
    Read from KITTI .png file
    :param flow_file: name of the flow file
    :return: optical flow data in matrix
    """
    flow_object = png.Reader(filename=flow_file)
    flow_direct = flow_object.asDirect()
    flow_data = list(flow_direct[2])
    (w, h) = flow_direct[3]['size']
    print("Reading %d x %d flow file in .png format" % (h, w))
    flow = np.zeros((h, w, 3), dtype=np.float64)
    for i in range(len(flow_data)):
        flow[i, :, 0] = flow_data[i][0::3]
        flow[i, :, 1] = flow_data[i][1::3]
        flow[i, :, 2] = flow_data[i][2::3]

    invalid_idx = (flow[:, :, 2] == 0)
    flow[:, :, 0:2] = (flow[:, :, 0:2] - 2 ** 15) / 64.0
    flow[invalid_idx, 0] = 0
    flow[invalid_idx, 1] = 0
    return flow 
Example #21
Source File: FlowUtils.py    From EVDodgeNet with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def read_disp_png(file_name):
    """
    Read optical flow from KITTI .png file
    :param file_name: name of the flow file
    :return: optical flow data in matrix
    """
    image_object = png.Reader(filename=file_name)
    image_direct = image_object.asDirect()
    image_data = list(image_direct[2])
    (w, h) = image_direct[3]['size']
    channel = len(image_data[0]) / w
    flow = np.zeros((h, w, channel), dtype=np.uint16)
    for i in range(len(image_data)):
        for j in range(channel):
            flow[i, :, j] = image_data[i][j::channel]
    return flow[:, :, 0] / 256 
Example #22
Source File: flowlib.py    From GeoNet with MIT License 6 votes vote down vote up
def read_disp_png(file_name):
    """
    Read optical flow from KITTI .png file
    :param file_name: name of the flow file
    :return: optical flow data in matrix
    """
    image_object = png.Reader(filename=file_name)
    image_direct = image_object.asDirect()
    image_data = list(image_direct[2])
    (w, h) = image_direct[3]['size']
    channel = len(image_data[0]) / w
    flow = np.zeros((h, w, channel), dtype=np.uint16)
    for i in range(len(image_data)):
        for j in range(channel):
            flow[i, :, j] = image_data[i][j::channel]
    return flow[:, :, 0] / 256 
Example #23
Source File: flowlib.py    From GeoNet with MIT License 6 votes vote down vote up
def read_png_file(flow_file):
    """
    Read from KITTI .png file
    :param flow_file: name of the flow file
    :return: optical flow data in matrix
    """
    flow_object = png.Reader(filename=flow_file)
    flow_direct = flow_object.asDirect()
    flow_data = list(flow_direct[2])
    (w, h) = flow_direct[3]['size']
    print "Reading %d x %d flow file in .png format" % (h, w)
    flow = np.zeros((h, w, 3), dtype=np.float64)
    for i in range(len(flow_data)):
        flow[i, :, 0] = flow_data[i][0::3]
        flow[i, :, 1] = flow_data[i][1::3]
        flow[i, :, 2] = flow_data[i][2::3]

    invalid_idx = (flow[:, :, 2] == 0)
    flow[:, :, 0:2] = (flow[:, :, 0:2] - 2 ** 15) / 64.0
    flow[invalid_idx, 0] = 0
    flow[invalid_idx, 1] = 0
    return flow 
Example #24
Source File: mceutils.py    From MCEdit-Unified with ISC License 5 votes vote down vote up
def loadPNGData(filename_or_data):
    reader = png.Reader(filename_or_data)
    (w, h, data, metadata) = reader.read_flat()
    data = numpy.array(data, dtype='uint8')
    data.shape = (h, w, metadata['planes'])
    if data.shape[2] == 1:
        # indexed color. remarkably straightforward.
        data.shape = data.shape[:2]
        data = numpy.array(reader.palette(), dtype='uint8')[data]

    if data.shape[2] < 4:
        data = numpy.insert(data, 3, 255, 2)

    return w, h, data 
Example #25
Source File: TrackingForwarder_util.py    From TrackR-CNN with MIT License 5 votes vote down vote up
def open_flow_png_file(file_path_list):
  # Funtion from Kilian Merkelbach.
  # Decode the information stored in the filename
  flow_png_info = {}
  assert len(file_path_list) == 2
  for file_path in file_path_list:
    file_token_list = os.path.splitext(file_path)[0].split("_")
    minimal_value = int(file_token_list[-1].replace("minimal", ""))
    flow_axis = file_token_list[-2]
    flow_png_info[flow_axis] = {'path': file_path,
                                'minimal_value': minimal_value}

  # Open both files and add back the minimal value
  for axis, flow_info in flow_png_info.items():
    import png
    png_reader = png.Reader(filename=flow_info['path'])
    flow_2d = np.vstack(map(np.uint16, png_reader.asDirect()[2]))

    # Add the minimal value back
    flow_2d = flow_2d.astype(np.int16) + flow_info['minimal_value']

    flow_png_info[axis]['flow'] = flow_2d

  # Combine the flows
  flow_x = flow_png_info['x']['flow']
  flow_y = flow_png_info['y']['flow']
  flow = np.stack([flow_x, flow_y], 2)

  return flow 
Example #26
Source File: compute_gt_poses.py    From ObjectDatasetTools with MIT License 5 votes vote down vote up
def load_pcds(path, downsample = True, interval = 1):

    """
    load pointcloud by path and down samle (if True) based on voxel_size 

    """
    

    global voxel_size, camera_intrinsics 
    pcds= []
    
    for Filename in xrange(len(glob.glob1(path+"JPEGImages","*.jpg"))/interval):
        img_file = path + 'JPEGImages/%s.jpg' % (Filename*interval)
        
        cad = cv2.imread(img_file)
        cad = cv2.cvtColor(cad, cv2.COLOR_BGR2RGB)
        depth_file = path + 'depth/%s.png' % (Filename*interval)
        reader = png.Reader(depth_file)
        pngdata = reader.read()
        # depth = np.vstack(map(np.uint16, pngdata[2]))
        depth = np.array(tuple(map(np.uint16, pngdata[2])))
        mask = depth.copy()
        depth = convert_depth_frame_to_pointcloud(depth, camera_intrinsics)


        source = geometry.PointCloud()
        source.points = utility.Vector3dVector(depth[mask>0])
        source.colors = utility.Vector3dVector(cad[mask>0])

        if downsample == True:
            pcd_down = source.voxel_down_sample(voxel_size = voxel_size)
            pcd_down.estimate_normals(geometry.KDTreeSearchParamHybrid(radius = 0.002 * 2, max_nn = 30))
            pcds.append(pcd_down)
        else:
            pcds.append(source)
    return pcds 
Example #27
Source File: compute_gt_poses.py    From ObjectDatasetTools with MIT License 5 votes vote down vote up
def load_pcd(path, Filename, downsample = True, interval = 1):

     """
     load pointcloud by path and down samle (if True) based on voxel_size 
     
     """
    

     global voxel_size, camera_intrinsics 
    
 
     img_file = path + 'JPEGImages/%s.jpg' % (Filename*interval)

     cad = cv2.imread(img_file)
     cad = cv2.cvtColor(cad, cv2.COLOR_BGR2RGB)
     depth_file = path + 'depth/%s.png' % (Filename*interval)
     reader = png.Reader(depth_file)
     pngdata = reader.read()
     # depth = np.vstack(map(np.uint16, pngdata[2]))
     depth = np.array(tuple(map(np.uint16, pngdata[2])))
     mask = depth.copy()
     depth = convert_depth_frame_to_pointcloud(depth, camera_intrinsics)


     source = geometry.PointCloud()
     source.points = utility.Vector3dVector(depth[mask>0])
     source.colors = utility.Vector3dVector(cad[mask>0])

     if downsample == True:
          source = source.voxel_down_sample(voxel_size = voxel_size)
          source.estimate_normals(geometry.KDTreeSearchParamHybrid(radius = 0.002 * 2, max_nn = 30))
       
     return source 
Example #28
Source File: game_test.py    From picotool with MIT License 5 votes vote down vote up
def testPngToPicodataFromFile(self):
        pngpath = os.path.join(self.testdata_path, 'test_gol.p8.png')
        with open(pngpath, 'rb') as fh:
            width, height, data, attrs = png.Reader(file=fh).read()
            data = list(data)
        picodata = game.Game.get_picodata_from_pngdata(
            width, height, data, attrs)
        self.assertEqual(len(picodata), 32800)
        self.assertEqual(FILE_TEST_GOL_CODE_COMPRESSED_HEAD,
                         picodata[0x4300:
                         0x4300 + len(FILE_TEST_GOL_CODE_COMPRESSED_HEAD)]) 
Example #29
Source File: game.py    From picotool with MIT License 5 votes vote down vote up
def get_raw_data_from_p8png_file(cls, instr, filename=None):
        """Read and unpack raw section data from a .p8.png file.

        Args:
          instr: The input stream.
          filename: The filename, if any, for tool messages.

        Returns:
          An object with properties of raw data: gfx, p8map, gfx_props,
                  song, sfx, codedata, version, code_length, code,
                  compressed_size.
        """
        # To install: python3 -m pip install pypng
        import png
        try:
            r = png.Reader(file=instr)
            (width, height, data, attrs) = r.read()
            data = list(data)
        except png.Error:
            raise InvalidP8PNGError()

        picodata = cls.get_picodata_from_pngdata(width, height, data, attrs)

        class ParsedData(object):
            pass
        data = ParsedData()

        data.gfx = picodata[0x0:0x2000]
        data.p8map = picodata[0x2000:0x3000]
        data.gfx_props = picodata[0x3000:0x3100]
        data.song = picodata[0x3100:0x3200]
        data.sfx = picodata[0x3200:0x4300]
        data.codedata = picodata[0x4300:0x8000]
        data.version = picodata[0x8000]

        # TODO: Extract new_game.label from data

        (data.code_length, data.code, data.compressed_size) = \
            cls.get_code_from_bytes(data.codedata, data.version)

        return data 
Example #30
Source File: game.py    From picotool with MIT License 5 votes vote down vote up
def get_picodata_from_pngdata(cls, width, height, pngdata, attrs):
        """Extracts Pico-8 bytes from a .p8.png's PNG data.

        The arguments are expected in the format returned by png.Reader's
        read() method.

        Args:
          width: The PNG width.
          height: The PNG height.
          pngdata: The PNG data region, an iterable of 'height' rows, where
            each row is an indexable 'width' * 'attrs['planes']' long.
          attrs: The PNG attrs.

        Returns:
          The Pico-8 data, a list of width * height (0x8000) byte-size numbers.
        """
        picodata = [0] * width * height

        row_i = 0
        for row in pngdata:
            for col_i in range(width):
                picodata[row_i * width + col_i] |= (
                    (row[col_i * attrs['planes'] + 2] & 3) << (0 * 2))
                picodata[row_i * width + col_i] |= (
                    (row[col_i * attrs['planes'] + 1] & 3) << (1 * 2))
                picodata[row_i * width + col_i] |= (
                    (row[col_i * attrs['planes'] + 0] & 3) << (2 * 2))
                picodata[row_i * width + col_i] |= (
                    (row[col_i * attrs['planes'] + 3] & 3) << (3 * 2))
            row_i += 1

        return picodata