Ask Your Question
0

How to edit pixel values of color image in SageMath?

asked 2018-10-11 13:33:40 +0100

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:

MWE:

from PIL import Image
img=Image.open('image.pgm')
pxl=img.load()
#pxls=img.getdata()
print pxl[0,0]

When I compile thse codes with sage, error occurs :

"  File "smmm.sage.py", 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
2

answered 2018-10-11 13:46:24 +0100

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():

pxl[int(0),int(0)]

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

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

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

edit flag offensive delete link more

Comments

@eric_g thank you very much for your answer.

BSFU gravatar imageBSFU ( 2018-10-11 14:28:44 +0100 )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-13 00:49:03 +0100 )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

Stats

Asked: 2018-10-11 13:33:40 +0100

Seen: 586 times

Last updated: Oct 11 '18