Python: Difference between revisions

1,022 bytes added ,  29 April 2020
Line 385: Line 385:
===Pillow (PIL)===
===Pillow (PIL)===
<code>pip install pillow</code>
<code>pip install pillow</code>
<syntaxhighlight lang="python>
<syntaxhighlight lang="python">
from PIL import Image
from PIL import Image
from
from
Line 395: Line 395:
* <code>flip()</code> - flips across y axis
* <code>flip()</code> - flips across y axis
* <code>mirror()</code> - flips across x axis
* <code>mirror()</code> - flips across x axis
===Bilinear Interpolation===
Coped from [https://stackoverflow.com/questions/12729228/simple-efficient-bilinear-interpolation-of-images-in-numpy-and-python https://stackoverflow.com/questions/12729228/simple-efficient-bilinear-interpolation-of-images-in-numpy-and-python]
<syntaxhighlight lang="python">
def bilinear_interpolate(im, x, y):
    """
    Basic bilinear interpolation
    :param im:
    :param x:
    :param y:
    :return:
    """
    x = np.asarray(x)
    y = np.asarray(y)
    x0 = np.floor(x).astype(int)
    x1 = x0 + 1
    y0 = np.floor(y).astype(int)
    y1 = y0 + 1
    x0 = np.clip(x0, 0, im.shape[1] - 1)
    x1 = np.clip(x1, 0, im.shape[1] - 1)
    y0 = np.clip(y0, 0, im.shape[0] - 1)
    y1 = np.clip(y1, 0, im.shape[0] - 1)
    Ia = im[y0, x0]
    Ib = im[y1, x0]
    Ic = im[y0, x1]
    Id = im[y1, x1]
    wa = (x1 - x) * (y1 - y)
    wb = (x1 - x) * (y - y0)
    wc = (x - x0) * (y1 - y)
    wd = (x - x0) * (y - y0)
    return wa * Ia + wb * Ib + wc * Ic + wd * Id
</syntaxhighlight>


==Libraries==
==Libraries==