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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:45:52+00:00 2026-05-31T09:45:52+00:00

Imagine you have an RGB image and want to process every pixel: import numpy

  • 0

Imagine you have an RGB image and want to process every pixel:

import numpy as np
image = np.zeros((1024, 1024, 3))

def rgb_to_something(rgb):
    pass

vfunc = np.vectorize(rgb_to_something)
vfunc(image)

vfunc should now get every RGB value. The problem is that numpy flattens the
array and the function gets r0, g0, b0, r1, g1, b1, ... when it should get
rgb0, rgb1, rgb2, ....
Can this be done somehow?

http://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html

Maybe by converting the numpy array to some special datatype beforehand?

For example (of course not working):

image = image.astype(np.float32)
import ctypes
RGB = ctypes.c_float * 3
image.astype(RGB)
ValueError: shape mismatch: objects cannot be broadcast to a single shape

Update:
The main purpose is efficiency here. A non vectorized version could simply look like this:

import numpy as np
image = np.zeros((1024, 1024, 3))
shape = image.shape[0:2]
image = image.reshape((-1, 3))
def rgb_to_something((r, g, b)):
    return r + g + b
transformed_image = np.array([rgb_to_something(rgb) for rgb in image]).reshape(shape)
  • 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-05-31T09:45:53+00:00Added an answer on May 31, 2026 at 9:45 am

    The easy way to solve this kind of problem is to pass the entire array to the function and used vectorized idioms inside it. Specifically, your rgb_to_something can also be written

    def rgb_to_something(pixels):
        return pixels.sum(axis=1)
    

    which is about 15 times faster than your version:

    In [16]: %timeit np.array([old_rgb_to_something(rgb) for rgb in image]).reshape(shape)
    1 loops, best of 3: 3.03 s per loop
    
    In [19]: %timeit image.sum(axis=1).reshape(shape)
    1 loops, best of 3: 192 ms per loop
    

    The problem with np.vectorize is that it necessarily incurs a lot of Python function call overhead when applied to large arrays.

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

Sidebar

Related Questions

Imagine I have 2 pictures, imagea.jpg and imageb.jpg Image A Image B I want
Imagine you have a collection of items (say A) around 50,000 but every item
Imagine we have an array of students: Students: Ben Sam ... Every student has
In F#, imagine we have an array of bytes representing pixel data with three
Imagine I have the following string : set(SEXY_STRING I love CMake) then I want
Imagine I have String in C#: I Don’t see ya.. I want to remove
Imagine I have the folling XML file: <a>before<b>middle</b>after</a> I want to convert it into
Imagine I have a process that starts several child processes. The parent needs to
Imagine you have an entity that has some relations with other entities and you
Imagine I have a table which stores a series of sparse vectors. A sparse

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.