Ask Your Question

How to edit pixel values of color image in SageMath?

asked 2018-10-11 06:33:40 -0600

I would like to change pixel values in a color image with Sagemath. I can do the same in python, but my program contains some parts which can not be done with python. Here are the codes for SageMath:


from PIL import Image'image.pgm')
print pxl[0,0]

When I compile thse codes with sage, error occurs :

"  File "", line 8, in <module>
    print pxl[_sage_const_0 ,_sage_const_0 ]
TypeError: an integer is required

How can I fix this?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-10-11 06:46:24 -0600

eric_g gravatar image

This is because Sage's preparser transforms the 0 in px1[0,0] into Sage's integers, while the object pxl is expecting Python's integers. The solution is to force the conversion to Python's integers via int():


More generally to check what Sage's preparser is doing with things you type, use the preparse command:

sage: preparse("pxl[0,0]")

(Integer() is for Sage's integers, while int() is for Python's integers).

edit flag offensive delete link more


@eric_g thank you very much for your answer. gravatar ( 2018-10-11 07:28:44 -0600 )edit

Or you can use r (for "raw") to avoid conversion to a Sage Integer:

pxl[0r, 0r]

If you do pxl[int(0),int(0)], the preparser turns the Python int 0 into a Sage Integer and it is then transformed back into a Python Integer.

If you use the raw indicator, conversion to a Sage integer is avoided.

slelievre gravatar imageslelievre ( 2018-10-12 17:49:03 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2018-10-11 06:33:40 -0600

Seen: 46 times

Last updated: Oct 11