Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9205777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:01:59+00:00 2026-06-18T00:01:59+00:00

I’m checking with you if there is a neat numpy solution to resizing down

  • 0

I’m checking with you if there is a neat numpy solution to resizing down a 2D numpy array (which is an image) using bilinear filtering?

More specifically, my array has the shape (width, height, 4) (as in a rgba image). The downscaling is also only done on “even” steps: i.e. from (w, h, 4) to (w/2, h/2, 4) to (w/4, h/4, 4) etc.

I’ve browsed around for quite some time now but everyone seems to refer to the scipy/PIL versions of imresize.

I want to minimize the number of dependencies on python packages, hence the numpy only requirement.

I just wanted to check with SO before I go implement it in C++ instead.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:02:01+00:00Added an answer on June 18, 2026 at 12:02 am

    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:

    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) >> 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 > 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')
    

    Produces this output:

    enter image description here

    With an original image of 512×512, it runs on my system in:

    In [3]: img.shape
    Out[3]: (512, 512, 4)
    
    In [4]: %timeit mipmap(img)
    10 loops, best of 3: 154 ms per loop
    

    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 (rows // 2, 2, cols // 2, 2, planes), so that img[r, :, c, :, p] is a 2×2 matrix of values to interpolate to get a new pixel value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which has BIG numbers and small numbers in it. I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am confused How to use looping for Json response Array in another Array.
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.