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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:59:13+00:00 2026-06-10T13:59:13+00:00

I want to realize this function: calculate the average value(or the sum) rgb value

  • 0

I want to realize this function: calculate the average value(or the sum) rgb value of an image. More specifically, the image consists of an 2-d array of a tuple. Here is my code:

rgb = [0.0, 0.0, 0.0]
for r in range(0, 3):
    for ii in range(x, x + X_STEP):
        for jj in range(y, y + Y_STEP):
            rgb[r] += src_pix[ii][jj][r]
rgb = map(lambda a: a / X_STEP / Y_STEP, rgb) #this line does not matter, it is just the difference between sum and average

Question How to prettify it, or make it more pythonic? Maybe a nested map is still not the best. I hope it is like using itertools.

This link provides a solution close to my question. Another link is a possible duplicate of my code but he is not asking the same question.

Thanks a lot.

EDIT
I actually hope to calculate the sum of a sub 2-d array.

  • 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-10T13:59:15+00:00Added an answer on June 10, 2026 at 1:59 pm

    It looks like you are using PIL. If so I’d use numpy for this.

    Assuming you are using PIL version >= 1.1.6 , you can convert between the PIL object src_pix to numpy array like so:

    np_pix = numpy.array(srx_pix)
    

    Then just use numpy.sum :

    rgb = numpy.sum(np_pix)
    

    Or calculate the average (as your code above does):

    rgb = numpy.mean(np_pix)
    

    For older versions of PIL use numpy.asarray.

    If you want to compute the sum of a subarray, you can get that subarray using a slice like so:

    rgb_slice = numpy.mean(np_pix[X_start:X_end,Y_start:Y_end])
    

    References:

    • PIL and numpy (older SO
      post on this topic)

    • Scipy Documentation on Array Manipulation

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

Sidebar

Related Questions

i want to realize a construct in MS SQL that would look like this
I want to realize this class into php extension: class MyClass { protected $attrs
What I want to realize is the following: In a JSP x I have
I want to store short arrays in a field. (I realize there are reasons
Want to run javascript function from parent window in child window Example I have
want to know why String behaves like value type while using ==. String s1
Want the function to sort the table by HP but if duplicate HPs then
I realize this is likely a duplicate, but I've been googling/SOing for a day
So I realize this is a possible duplicate question, as there a number of
I have a join function that operates on STL strings. I want to be

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.