First time here? Check out the FAQ!

Ask Your Question
0

How to edit pixel values of color image in SageMath?

asked 6 years ago

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?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 6 years ago

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).

Preview: (hide)
link

Comments

@eric_g thank you very much for your answer.

BSFU gravatar imageBSFU ( 6 years ago )

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 ( 6 years ago )

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: 6 years ago

Seen: 623 times

Last updated: Oct 11 '18