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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:30:28+00:00 2026-06-12T11:30:28+00:00

I need to perform some operations on a 2D array of values read from

  • 0

I need to perform some operations on a 2D array of values read from an image, and then create an image with a resulting 2D array. I’m using python lists to represent the 2D array.

Something very odd is happening; the values in the 2D array (list of lists) appear to become “0” at some point between the two print calls I have labeled. That is, they seem to be read correctly from the image… but then somehow get set to zero.

Code:

image = Image.open("test.png").convert("L")

data = [ [255] * image.size[1] ] * image.size[0]
pix = image.load()
for x in range(0, image.size[0]):
    for y in range(0, image.size[1]):
        data[x][y] = pix[x, y]
        #data[x][y] = 77
        print "1. data[x][y] = " + str(data[x][y]) + " .vs. " + str(pix[x, y]) # Prints correct values

for x in range(0, image.size[0]):
    for y in range(0, image.size[1]):
        print "2. data[x][y] = " + str(data[x][y]) + " .vs. " + str(pix[x, y]) # Always prints "0 .vs. [correct value]"

However, if I comment out the line

data[x][y] = pix[x, y]

And uncomment:

data[x][y] = 77

Then the two print statements show that all elements in data are 77

What is going on? I’m not an expert on python, but I can’t think of any sensible reason why list values would change like that.

I have tried the following line, in case the pixel accessor is doing something wierd:

data[x][y] = 0 + int(pix[x, y])

But still get the same result. I’ve also tried using RGB images instead of greyscale.

I should make it clear that I am definitely not doing anything with data between those two print calls. The code above is exactly what I have reduced my original program to (after discovering that all my “results” image files were black).

  • 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-12T11:30:29+00:00Added an answer on June 12, 2026 at 11:30 am

    You’re problem is with this line:

    data = [ [255] * image.size[1] ] * image.size[0]
    

    This creates a list of length image.size[1] filled with the value 255. Then you create image.size[0] references to the same list and pack all of those references into another list. So, when you change a[1][1], you also change a[0][1] and a[2][1] etc. because a[0],a[1],a[2], ... are references to the same list.

    Here’s a quick example:

    a = [[255]*10]*10
    a[1][1] = 77
    print (a)
    

    The easiest workaround is:

    a = [[255]*10 for _ in range(10)]
    

    As this creates 10 new lists instead of a list of references to the same list.

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

Sidebar

Related Questions

I need to read a string from standard input, perform some operations and meanwhile
I am using mex bridge to perform some operations on Sparse matrices from Matlab.
I need to perform some date operations in ASP.net using C#. The date i
I need to pick up list items from an list, and then perform operations
I need to perform some operations on all folders within a file share which
I need to perform multiple operations on an Image, for example, I need to
I need to perform some file operations on WinCE platform. The paths of file
In python 2.7, I need to perform the same operations for a list of
i need to perform some operations on files - rename, delete and etc. what
I need to perform some operations by ajax before a node is submitted. So,

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.