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 8401499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:49:21+00:00 2026-06-09T21:49:21+00:00

I am implementing color interpolation using a look-up-table (LUT) with NumPy. At one point

  • 0

I am implementing color interpolation using a look-up-table (LUT) with NumPy. At one point I am using the 4 most significant bits of RGB values to choose corresponding CMYK values from a 17x17x17x4 LUT. Right now it looks something like this:

import numpy as np
rgb = np.random.randint(16, size=(3, 1000, 1000))
lut = np.random.randint(256, size=(17, 17, 17, 4))
cmyk = lut[rgb[0], rgb[1], rgb[2]]

Here comes the first question… Is there no better way? It sort of seems natural that you could tell NumPy that the indices for lut are stored along axis 0 of rgb, without having to actually write it out. So is there anything like cmyk = lut.fancier_take(rgb, axis=0) in NumPy?

Furthermore, I am left with an array of shape (1000, 1000, 4), so to be consistent with the input, I need to rotate it all around using a couple of swapaxes:

cmyk = cmyk.swapaxes(2, 1).swapaxes(1, 0).copy()

And I also need to add the copy statement, because if not the resulting array is not contiguous in memory, and that brings trouble later on.

Right now I am leaning towards rotating the LUT before the fancy indexing and then do something along the lines of:

swapped_lut = lut.swapaxes(2, 1).swapaxes(1, 0)
cmyk = swapped_lut[np.arange(4), rgb[0], rgb[1], rgb[2]]

But again, it just does not seem right… There has to be a more elegant way to do this, right? Something like cmyk = lut.even_fancier_take(rgb, in_axis=0, out_axis=0)…

  • 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-09T21:49:22+00:00Added an answer on June 9, 2026 at 9:49 pm

    You’ll need to do the following if you swap lut, np.arange(4) will not work:

    swapped_lut = np.rollaxis(lut, -1)
    cmyk = swapped_lut[:, rgb[0], rgb[1], rgb[2]].copy()
    

    Or you can replace

    cmyk = lut[rgb[0], rgb[1], rgb[2]]
    cmyk = cmyk.swapaxes(2, 1).swapaxes(1, 0).copy()
    

    with:

    cmyk = lut[tuple(rgb)]
    cmyk = np.rollaxis(cmyk, -1).copy()
    

    But to try and do it all in one step, … Maybe:

    rng = np.arange(4).reshape(4, 1, 1)
    cmyk = lut[rgb[0], rgb[1], rgb[2], rng]
    

    That’s not very readable at all is it?

    Take a look at the answer to this question, Numpy multi-dimensional array indexing swaps axis order. It does a good job of explaining how numpy broadcasts multiple arrays to get the output size. Here you want to create indices into lut that broadcast to (4, 1000, 1000). Hope that makes some sense.

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

Sidebar

Related Questions

When implementing a hash table using a good hash function (one where the probability
I am learning JS from scratch and implementing HighCharts code using JS. Seeing one
I'm implementing some caching by using the nifty Rails.cache.fetch . However, in one particular
implementing publishActivity in PHP using the REST API using this code: $activity = array(
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
Before implementing j_security_check using MySQL realm authentication in my web app. I had the
When implementing the Strategy Pattern, where does one put the code that determines which
I'm looking for a way to find the most dominant color/tone in an image
I'm implementing a color picker. There is problem with the rendering. When I call
I am implementing paint Application. My problem is that when i color on object.After

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.