Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think there is any specific solution in numpy, but you should be able to implement it efficiently without leaving the comfort of python. Correct me if I'm wrong, but when the size of the image is divisible by 2, a bilinear filter is basically the same as averaging 4 pixels of the original image to get 1 pixel of the new one, right? Well, if your image size is a power of two, then the following code:</p> <pre><code>from __future__ import division import numpy as np from PIL import Image def halve_image(image) : rows, cols, planes = image.shape image = image.astype('uint16') image = image.reshape(rows // 2, 2, cols // 2, 2, planes) image = image.sum(axis=3).sum(axis=1) return ((image + 2) &gt;&gt; 2).astype('uint8') def mipmap(image) : img = image.copy() rows, cols, planes = image.shape mipmap = np.zeros((rows, cols * 3 // 2, planes), dtype='uint8') mipmap[:, :cols, :] = img row = 0 while rows &gt; 1: img = halve_image(img) rows = img.shape[0] mipmap[row:row + rows, cols:cols + img.shape[1], :] = img row += rows return mipmap img = np.asarray(Image.open('lena.png')) Image.fromarray(mipmap(img)).save('lena_mipmap.png') </code></pre> <p>Produces this output:</p> <p><img src="https://i.stack.imgur.com/ZzqnJ.png" alt="enter image description here"></p> <p>With an original image of 512x512, it runs on my system in:</p> <pre><code>In [3]: img.shape Out[3]: (512, 512, 4) In [4]: %timeit mipmap(img) 10 loops, best of 3: 154 ms per loop </code></pre> <p>This will not work if an odd length of a side ever comes up, but depending on exactly how you want to handle the downsampling for those cases, you should be able to get rid of a full row (or column) of pixels, reshape your image to <code>(rows // 2, 2, cols // 2, 2, planes)</code>, so that <code>img[r, :, c, :, p]</code> is a 2x2 matrix of values to interpolate to get a new pixel value.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload