Exemple #1
0
    def write_image(self, request_iterator, context):
        print('write_image called')
        w = None
        h = None
        depth = None
        filename = None

        for key, value in context.invocation_metadata():
            if key == 'w': w = value
            if key == 'h': h = value
            if key == 'depth': depth = value
            if key == 'filename': filename = value

        if not w: raise Exception('missing w')
        if not h: raise Exception('missing h')
        if not depth: raise Exception('missing depth ')
        if not filename: raise Exception('missing filename')

        image_data = ['P3', w, h, depth]

        num_pixels = 1
        for rgb in request_iterator:
            print('Received {} from {}'.format(num_pixels, context.peer()))
            image_data.append(str(rgb.r))
            image_data.append(str(rgb.g))
            image_data.append(str(rgb.b))
            num_pixels += 1

        ppm.write_image('server_' + filename, image_data)
        return pgm_store_pb2.Empty()
Exemple #2
0
def greyscale(stub, filename):
    image_data = ppm.load_image(filename)

    total_pixels = (len(image_data) - 4) // 3

    def send_pixel_data():
        num_sent = 1
        ## ignore first 4 elements
        for i in range(4, len(image_data), 3):
            r = int(image_data[i + 0])
            g = int(image_data[i + 1])
            b = int(image_data[i + 2])
            yield pgm_store_pb2.PixelData(r=r, g=g, b=b)
            print('Send pixeldata {}/{}'.format(num_sent, total_pixels))
            num_sent += 1

    response = stub.greyscale(send_pixel_data())
    new_image_data = [
        image_data[0], image_data[1], image_data[2], image_data[3]
    ]
    num_received = 1
    for rgb in response:
        new_image_data.append(str(rgb.r))
        new_image_data.append(str(rgb.g))
        new_image_data.append(str(rgb.b))
        print('Received pixeldata {}/{}'.format(num_received, total_pixels))
        num_received += 1

    ppm.write_image('greyscale_' + filename, new_image_data)
Exemple #3
0
def get_image(stub, filename):
    argument = pgm_store_pb2.GetImageInput(filename=filename)
    response = stub.get_image(argument)

    # iterating over responses might may raise grpcError.
    # Should try accessing response before accessing metadata.
    image_data = []
    num_pixels = 1
    for rgb in response:
        image_data.append(str(rgb.r))
        image_data.append(str(rgb.g))
        image_data.append(str(rgb.b))
        print('Received {}'.format(num_pixels))
        num_pixels += 1

    w = None
    h = None
    depth = None
    for key, value in response.initial_metadata():
        if key == 'w': w = value
        if key == 'h': h = value
        if key == 'depth': depth = value

    if not w: raise Exception('missing w')
    if not h: raise Exception('missing h')
    if not depth: raise Exception('missing depth ')

    image_data = ['P3', w, h, depth] + image_data
    ppm.write_image('client_' + filename, image_data)
Exemple #4
0
    def test_ppm_write_then_read(self):
        mo = mock_open()
        with patch("ppm.open", mo):
            write_image(LED_EXAMPLE, "testfilename")
            wrotefile = get_file_from_mockopen(mo)
            self.assertEqual(PPM_EXAMPLE, wrotefile)

        with patch("ppm.open", mock_open(read_data=wrotefile)):
            read_leds = read_image("testfilename")
            self.assertEqual(read_leds, LED_EXAMPLE)
Exemple #5
0
    def test_ppm_read_then_write(self):
        """
        Test that read and write can work together
        """

        with patch("ppm.open", mock_open(read_data=PPM_EXAMPLE)):
            read_leds = read_image("testfilename")

        mo = mock_open()
        with patch("ppm.open", mo):
            write_image(read_leds, "testfilename")
            wrotefile = get_file_from_mockopen(mo)
            self.assertEqual(PPM_EXAMPLE, wrotefile)
Exemple #6
0
 def test_ppm_write(self):
     mo = mock_open()
     with patch("ppm.open", mo):
         write_image(LED_EXAMPLE, "testfilename")
         wrotefile = get_file_from_mockopen(mo)
         self.assertEqual(PPM_EXAMPLE, wrotefile)