diff options
Diffstat (limited to 'numerical')
| -rw-r--r-- | numerical/image_inter.py | 93 | ||||
| -rw-r--r-- | numerical/scipy_interp.py | 11 |
2 files changed, 104 insertions, 0 deletions
diff --git a/numerical/image_inter.py b/numerical/image_inter.py new file mode 100644 index 0000000..97b37d9 --- /dev/null +++ b/numerical/image_inter.py @@ -0,0 +1,93 @@ +''' + use bilinear interpolation to resize an image + https://www.brezeale.com/?p=812 +''' + +import numpy as np +from PIL import Image + + +def resizeImage(name): + img1 = Image.open(name) + + old = np.asarray(img1) # convert to Numpy array + rows, cols, layers = old.shape + new = np.zeros((2 * rows - 1, 2 * cols - 1, layers)) + print("original dimensions:", old.shape) + + for layer in range(3): + new[:, :, layer] = resizeLayer(old[:, :, layer]) + + # convert the values to unsigned, 8-bit integers + new = new.astype(np.uint8) + print(" new dimensions:", new.shape) + + img2 = Image.fromarray(new) # convert back to Image + newName = "big-" + name + img2.save(newName) + + +def resizeLayer(old): + rows, cols = old.shape + + rNew = 2 * rows - 1 + cNew = 2 * cols - 1 + new = np.zeros((rNew, cNew)) + + # move old points + new[0:rNew:2, 0:cNew:2] = old[0:rows, 0:cols] + + ''' alternative approach + # something like this would be necessary in languages + # that don't support slicing + new = np.zeros( (2*rows - 1, 2*cols - 1) ) + for r in range(rows) : + for c in range(cols) : + new[2*r, 2*c] = old[r,c] + + rows, cols = new.shape + ''' + + # produce vertical values + new[1:rNew:2, :] = (new[0:rNew - 1:2, :] + new[2:rNew:2, :]) / 2 + ''' alternative approach + for r in range(1, rows, 2) : + for c in range(0, cols, 2) : + # top + bottom + new[r,c] = ( new[r-1,c] + new[r+1,c] ) // 2 + ''' + + # produce horizontal values + new[:, 1:cNew:2] = (new[:, 0:cNew - 1:2] + new[:, 2:cNew:2]) / 2 + ''' alternative approach + for r in range(0, rows, 2) : + for c in range(1, cols, 2) : + # left + right + new[r,c] = ( new[r,c-1] + new[r,c+1] ) // 2 + ''' + + # produce center values + new[1:rNew:2, 1:cNew:2] = (new[0:rNew - 2:2, 0:cNew - 2:2] + + new[0:rNew - 2:2, 2:cNew:2] + + new[2:rNew:2, 0:cNew - 2:2] + + new[2:rNew:2, 2:cNew:2]) / 4 + ''' alternative approach + for r in range(1, rows, 2) : + for c in range(1, cols, 2) : + # top + bottom + left + right + new[r,c] = ( new[r-1,c] + new[r+1,c] + new[r,c-1] + new[r,c+1] ) // 4 + ''' + + return new + + +#################### main #################### + +test = np.array([[10, 40], [30, 20]]) +print(test) +test = resizeLayer(test) +print() +print(test) + +# filename = 'book_fausett_small.jpg' +# resizeImage(filename) diff --git a/numerical/scipy_interp.py b/numerical/scipy_interp.py new file mode 100644 index 0000000..fb7d3e8 --- /dev/null +++ b/numerical/scipy_interp.py @@ -0,0 +1,11 @@ +import numpy as np + +from scipy import interpolate +x = np.asarray(range(2)) +y = np.asarray(range(2)) +z = np.asarray([[10, 40], [30, 20]]) +f = interpolate.interp2d(x, y, z, kind='linear') + +for x in range(6): + for y in range(6): + print(x, y, f(x, y)) |
