1

I am trying to learn Python, this is the first code that I have written:

    #!/usr/bin/python
    # Filename: read_image.py


    f=open('1.raw','r+b')
    image=f.read()
    f.close()


    f=open('copy.raw','w+b')
    f.write(image)
    f.close()

    for i in range(1,256):
        image[i]=0

In the first part I am simply reading a '.raw' image as a binary file and making a copy of it. This part works fine on its own and I get a copy of the image after execution of the code. However I wish to manipulate this image, for starters I was trying to blacken the first line of the image, however I get the following error:

Traceback (most recent call last):
  File "C:/Python32/read_image.py", line 15, in <module>
    image[i]=0
TypeError: 'bytes' object does not support item assignment

I tried using 'int' type variables by copying the image into them, however the error persists except instead of 'bytes' object does not support assignment, I get 'int' object does not support assignment. How should I go about solving this problem?

Please note this is a grayscale image, and the pixel values range from 0 to 255, I tried printing the array image on the shell and it showed me values in this range.

2 Answers 2

3

If you're really trying to do image processing in Python, try the Python Imaging Library* (PIL) found here: http://www.pythonware.com/products/pil/

[*] Keep in mind that you will have to use Python 2.x as opposed to 3.x if you use this library, unfortunately as is currently the case with a lot of powerful python libraries.

Sign up to request clarification or add additional context in comments.

1 Comment

I just wanted to write my first bit of code... But Ill be sure to use the library if I have to implement anything.. read the documentation.. seems very useful.. Thank you!
3

In Python bytes are immutable. You can't change them, that's why it gives you an error when you try to do item assignment. You could convert your immutable bytes object into a bytearray:

image = bytearray(image)
for i in range(1,256):
    image[i]=0

5 Comments

Okay, tried converting it to a bytesarray, get the following error: NameError: name 'bytesarray' is not defined,
Sorry, it's bytearray. I did typo that.
Thanks a lot! The 'int' object doesnt support assignment too.. should I convert it to an intarray as well.. Coz I may have to manipulate integer arrays as well in the future..
@Kedar No, you just use a normal list and fill it with ints: a = [1,2,3]; a[1]=5
Hello Thomas... But how can I create lists without initializing them? Because I want to store all the pixel values in an integer/float array and then manipulate this array and then round off this array and copy the values back into a byte array... Can this be achieved in Python?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.