#!/usr/bin/env python

import numpy as np
from random import randint
from PIL import Image

def print3darray(array):
    for array_row in array:
        print(array_row.tolist())

def print3darray_tofile(title, array, filename):
    f = open(filename, 'w')
    print(title, file=f)
    for array_row in array:
        print(array_row.tolist(), file=f)
    f.close()

def convert_to_box(img, box_size):
    height, width, temp = img.shape
    img_box = np.zeros((height*box_size, width*box_size, 3), dtype=np.uint8)
    for i in range(height):
        for j in range(width):
                img_box[i*box_size: i*box_size+(box_size),
                        j*box_size: j*box_size+(box_size)] = img[i, j]
    return img_box

# generate a random image
#73 128
height = 18
width = 32
box_size = 20
print('Height: {}, Width: {}, Box Size: {}'.format(height, width, box_size))
img = np.zeros((height, width, 3), dtype=np.uint8)

for i in range(height):
    for j in range(width):
        if i%2 == 0:
            if j%2 == 0:
                img[i, j, 0] = randint(0, 255)
            else:
                img[i, j, 1] = randint(0, 255)
        else:
            if j%2 == 0:
                img[i, j, 1] = randint(0, 255)
            else:
                img[i, j, 2] = randint(0, 255)


# print the updated box
print3darray_tofile('Bayer filtered image captured:', img, 'rgb.txt')
img_box = convert_to_box(img, box_size)
Image.fromarray(img_box, 'RGB').save('rgb_box.png')

# DEMOSAIC
img_demosaic = np.zeros(img.shape, dtype=np.uint8)
# we go from 1 to height-2
# since we leave off the edges
# have to use np.uint16 because 8 bit leads to overflow
for i in range(1, height-1):
    for j in range(1, width-1):
        if i%2 == 0:
            if j%2 == 0:
                # ceter is red
                r = img[i, j]
                g = (np.uint16(img[i-1, j]) + np.uint16(img[i+1, j]) + np.uint16(img[i, j-1]) + np.uint16(img[i, j+1]))/4
                b = (np.uint16(img[i-1, j-1]) + np.uint16(img[i-1, j+1]) + np.uint16(img[i+1, j-1]) + np.uint16(img[i+1, j+1]))/4
            else:
                # center is green
                r = (np.uint16(img[i, j-1]) + np.uint16(img[i, j+1]))/2
                g = img[i, j]
                b = (np.uint16(img[i-1, j]) + np.uint16(img[i+1, j]))/2
        else:
            if j%2 == 0:
                # centre is green
                r = (np.uint16(img[i-1, j]) + np.uint16(img[i+1, j]))/2
                g = img[i, j]
                b = (np.uint16(img[i, j-1]) + np.uint16(img[i, j+1]))/2
            else:
                # centre is blue
                r = (np.uint16(img[i-1, j-1]) + np.uint16(img[i-1, j+1]) + np.uint16(img[i+1, j-1]) + np.uint16(img[i+1, j+1]))/4
                g = (np.uint16(img[i-1, j]) + np.uint16(img[i+1, j]) + np.uint16(img[i, j-1]) + np.uint16(img[i, j+1]))/4
                b = img[i, j]
        img_demosaic[i, j] = r + g + b

# save demosaiced image
print3darray_tofile('Demosaiced Image:', img_demosaic, 'demosaic.txt')
img_demosaic_box = convert_to_box(img_demosaic, box_size)
Image.fromarray(img_demosaic_box, 'RGB').save('demosaic_box.png')

fc7902fcf2c8384ba1d8cbb69c2c1b20954b58d7
<mask token>


def print3darray(array):
    for array_row in array:
        print(array_row.tolist())


<mask token>


def convert_to_box(img, box_size):
    height, width, temp = img.shape
    img_box = np.zeros((height * box_size, width * box_size, 3), dtype=np.uint8
        )
    for i in range(height):
        for j in range(width):
            img_box[i * box_size:i * box_size + box_size, j * box_size:j *
                box_size + box_size] = img[i, j]
    return img_box


<mask token>

<mask token>


def print3darray(array):
    for array_row in array:
        print(array_row.tolist())


def print3darray_tofile(title, array, filename):
    f = open(filename, 'w')
    print(title, file=f)
    for array_row in array:
        print(array_row.tolist(), file=f)
    f.close()


def convert_to_box(img, box_size):
    height, width, temp = img.shape
    img_box = np.zeros((height * box_size, width * box_size, 3), dtype=np.uint8
        )
    for i in range(height):
        for j in range(width):
            img_box[i * box_size:i * box_size + box_size, j * box_size:j *
                box_size + box_size] = img[i, j]
    return img_box


<mask token>

<mask token>


def print3darray(array):
    for array_row in array:
        print(array_row.tolist())


def print3darray_tofile(title, array, filename):
    f = open(filename, 'w')
    print(title, file=f)
    for array_row in array:
        print(array_row.tolist(), file=f)
    f.close()


def convert_to_box(img, box_size):
    height, width, temp = img.shape
    img_box = np.zeros((height * box_size, width * box_size, 3), dtype=np.uint8
        )
    for i in range(height):
        for j in range(width):
            img_box[i * box_size:i * box_size + box_size, j * box_size:j *
                box_size + box_size] = img[i, j]
    return img_box


<mask token>
print('Height: {}, Width: {}, Box Size: {}'.format(height, width, box_size))
<mask token>
for i in range(height):
    for j in range(width):
        if i % 2 == 0:
            if j % 2 == 0:
                img[i, j, 0] = randint(0, 255)
            else:
                img[i, j, 1] = randint(0, 255)
        elif j % 2 == 0:
            img[i, j, 1] = randint(0, 255)
        else:
            img[i, j, 2] = randint(0, 255)
print3darray_tofile('Bayer filtered image captured:', img, 'rgb.txt')
<mask token>
Image.fromarray(img_box, 'RGB').save('rgb_box.png')
<mask token>
for i in range(1, height - 1):
    for j in range(1, width - 1):
        if i % 2 == 0:
            if j % 2 == 0:
                r = img[i, j]
                g = (np.uint16(img[i - 1, j]) + np.uint16(img[i + 1, j]) +
                    np.uint16(img[i, j - 1]) + np.uint16(img[i, j + 1])) / 4
                b = (np.uint16(img[i - 1, j - 1]) + np.uint16(img[i - 1, j +
                    1]) + np.uint16(img[i + 1, j - 1]) + np.uint16(img[i + 
                    1, j + 1])) / 4
            else:
                r = (np.uint16(img[i, j - 1]) + np.uint16(img[i, j + 1])) / 2
                g = img[i, j]
                b = (np.uint16(img[i - 1, j]) + np.uint16(img[i + 1, j])) / 2
        elif j % 2 == 0:
            r = (np.uint16(img[i - 1, j]) + np.uint16(img[i + 1, j])) / 2
            g = img[i, j]
            b = (np.uint16(img[i, j - 1]) + np.uint16(img[i, j + 1])) / 2
        else:
            r = (np.uint16(img[i - 1, j - 1]) + np.uint16(img[i - 1, j + 1]
                ) + np.uint16(img[i + 1, j - 1]) + np.uint16(img[i + 1, j + 1])
                ) / 4
            g = (np.uint16(img[i - 1, j]) + np.uint16(img[i + 1, j]) + np.
                uint16(img[i, j - 1]) + np.uint16(img[i, j + 1])) / 4
            b = img[i, j]
        img_demosaic[i, j] = r + g + b
print3darray_tofile('Demosaiced Image:', img_demosaic, 'demosaic.txt')
<mask token>
Image.fromarray(img_demosaic_box, 'RGB').save('demosaic_box.png')

import numpy as np
from random import randint
from PIL import Image


def print3darray(array):
    for array_row in array:
        print(array_row.tolist())


def print3darray_tofile(title, array, filename):
    f = open(filename, 'w')
    print(title, file=f)
    for array_row in array:
        print(array_row.tolist(), file=f)
    f.close()


def convert_to_box(img, box_size):
    height, width, temp = img.shape
    img_box = np.zeros((height * box_size, width * box_size, 3), dtype=np.uint8
        )
    for i in range(height):
        for j in range(width):
            img_box[i * box_size:i * box_size + box_size, j * box_size:j *
                box_size + box_size] = img[i, j]
    return img_box


height = 18
width = 32
box_size = 20
print('Height: {}, Width: {}, Box Size: {}'.format(height, width, box_size))
img = np.zeros((height, width, 3), dtype=np.uint8)
for i in range(height):
    for j in range(width):
        if i % 2 == 0:
            if j % 2 == 0:
                img[i, j, 0] = randint(0, 255)
            else:
                img[i, j, 1] = randint(0, 255)
        elif j % 2 == 0:
            img[i, j, 1] = randint(0, 255)
        else:
            img[i, j, 2] = randint(0, 255)
print3darray_tofile('Bayer filtered image captured:', img, 'rgb.txt')
img_box = convert_to_box(img, box_size)
Image.fromarray(img_box, 'RGB').save('rgb_box.png')
img_demosaic = np.zeros(img.shape, dtype=np.uint8)
for i in range(1, height - 1):
    for j in range(1, width - 1):
        if i % 2 == 0:
            if j % 2 == 0:
                r = img[i, j]
                g = (np.uint16(img[i - 1, j]) + np.uint16(img[i + 1, j]) +
                    np.uint16(img[i, j - 1]) + np.uint16(img[i, j + 1])) / 4
                b = (np.uint16(img[i - 1, j - 1]) + np.uint16(img[i - 1, j +
                    1]) + np.uint16(img[i + 1, j - 1]) + np.uint16(img[i + 
                    1, j + 1])) / 4
            else:
                r = (np.uint16(img[i, j - 1]) + np.uint16(img[i, j + 1])) / 2
                g = img[i, j]
                b = (np.uint16(img[i - 1, j]) + np.uint16(img[i + 1, j])) / 2
        elif j % 2 == 0:
            r = (np.uint16(img[i - 1, j]) + np.uint16(img[i + 1, j])) / 2
            g = img[i, j]
            b = (np.uint16(img[i, j - 1]) + np.uint16(img[i, j + 1])) / 2
        else:
            r = (np.uint16(img[i - 1, j - 1]) + np.uint16(img[i - 1, j + 1]
                ) + np.uint16(img[i + 1, j - 1]) + np.uint16(img[i + 1, j + 1])
                ) / 4
            g = (np.uint16(img[i - 1, j]) + np.uint16(img[i + 1, j]) + np.
                uint16(img[i, j - 1]) + np.uint16(img[i, j + 1])) / 4
            b = img[i, j]
        img_demosaic[i, j] = r + g + b
print3darray_tofile('Demosaiced Image:', img_demosaic, 'demosaic.txt')
img_demosaic_box = convert_to_box(img_demosaic, box_size)
Image.fromarray(img_demosaic_box, 'RGB').save('demosaic_box.png')

#!/usr/bin/env python

import numpy as np
from random import randint
from PIL import Image

def print3darray(array):
    for array_row in array:
        print(array_row.tolist())

def print3darray_tofile(title, array, filename):
    f = open(filename, 'w')
    print(title, file=f)
    for array_row in array:
        print(array_row.tolist(), file=f)
    f.close()

def convert_to_box(img, box_size):
    height, width, temp = img.shape
    img_box = np.zeros((height*box_size, width*box_size, 3), dtype=np.uint8)
    for i in range(height):
        for j in range(width):
                img_box[i*box_size: i*box_size+(box_size),
                        j*box_size: j*box_size+(box_size)] = img[i, j]
    return img_box

# generate a random image
#73 128
height = 18
width = 32
box_size = 20
print('Height: {}, Width: {}, Box Size: {}'.format(height, width, box_size))
img = np.zeros((height, width, 3), dtype=np.uint8)

for i in range(height):
    for j in range(width):
        if i%2 == 0:
            if j%2 == 0:
                img[i, j, 0] = randint(0, 255)
            else:
                img[i, j, 1] = randint(0, 255)
        else:
            if j%2 == 0:
                img[i, j, 1] = randint(0, 255)
            else:
                img[i, j, 2] = randint(0, 255)


# print the updated box
print3darray_tofile('Bayer filtered image captured:', img, 'rgb.txt')
img_box = convert_to_box(img, box_size)
Image.fromarray(img_box, 'RGB').save('rgb_box.png')

# DEMOSAIC
img_demosaic = np.zeros(img.shape, dtype=np.uint8)
# we go from 1 to height-2
# since we leave off the edges
# have to use np.uint16 because 8 bit leads to overflow
for i in range(1, height-1):
    for j in range(1, width-1):
        if i%2 == 0:
            if j%2 == 0:
                # ceter is red
                r = img[i, j]
                g = (np.uint16(img[i-1, j]) + np.uint16(img[i+1, j]) + np.uint16(img[i, j-1]) + np.uint16(img[i, j+1]))/4
                b = (np.uint16(img[i-1, j-1]) + np.uint16(img[i-1, j+1]) + np.uint16(img[i+1, j-1]) + np.uint16(img[i+1, j+1]))/4
            else:
                # center is green
                r = (np.uint16(img[i, j-1]) + np.uint16(img[i, j+1]))/2
                g = img[i, j]
                b = (np.uint16(img[i-1, j]) + np.uint16(img[i+1, j]))/2
        else:
            if j%2 == 0:
                # centre is green
                r = (np.uint16(img[i-1, j]) + np.uint16(img[i+1, j]))/2
                g = img[i, j]
                b = (np.uint16(img[i, j-1]) + np.uint16(img[i, j+1]))/2
            else:
                # centre is blue
                r = (np.uint16(img[i-1, j-1]) + np.uint16(img[i-1, j+1]) + np.uint16(img[i+1, j-1]) + np.uint16(img[i+1, j+1]))/4
                g = (np.uint16(img[i-1, j]) + np.uint16(img[i+1, j]) + np.uint16(img[i, j-1]) + np.uint16(img[i, j+1]))/4
                b = img[i, j]
        img_demosaic[i, j] = r + g + b

# save demosaiced image
print3darray_tofile('Demosaiced Image:', img_demosaic, 'demosaic.txt')
img_demosaic_box = convert_to_box(img_demosaic, box_size)
Image.fromarray(img_demosaic_box, 'RGB').save('demosaic_box.png')
