Can someone explain how this python code works?

I am working in image processing right now in python using numpy and scipy all the time. I have one piece of code that can enlarge an image, but not sure how this works.

So please, some python scipy / numpy expert can explain me line by line. I am always ready to learn.

import numpy as N
import os.path
import scipy.signal
import scipy.interpolate
import matplotlib.pyplot as plt
import matplotlib.cm as cm


def enlarge(img, rowscale, colscale, method='linear'):
    x, y = N.meshgrid(N.arange(img.shape[1]), N.arange(img.shape[0]))
    pts = N.column_stack((x.ravel(), y.ravel()))
    xx, yy = N.mgrid[0.:float(img.shape[1]):1/float(colscale),
            0.:float(img.shape[0]):1/float(rowscale)]
    large = scipy.interpolate.griddata(pts, img.flatten(), (xx, yy), method).T
    large[-1,:] = large[-2,:]
    large[:,-1] = large[:,-2]
    return large

Many thanks.

+3
source share
1 answer

First, a grid of empty dots is created with a dot per pixel.

x, y = N.meshgrid(N.arange(img.shape[1]), N.arange(img.shape[0]))

Real image pixels are placed in a variable ptsthat will be needed later.

pts = N.column_stack((x.ravel(), y.ravel()))

; 200x400, colscale, 4 rowscale, 2, (200 * 4) x (400 * 2) 800x800 .

xx, yy = N.mgrid[0.:float(img.shape[1]):1/float(colscale),
        0.:float(img.shape[0]):1/float(rowscale)]

scipy, pts . - , .

large = scipy.interpolate.griddata(pts, img.flatten(), (xx, yy), method).T

100%, , , griddata. , , .

large[-1,:] = large[-2,:]
large[:,-1] = large[:,-2]
return large
+4

All Articles