Ask Your Question

Can I import a graphic and add to it?

asked 2017-10-26 14:06:06 -0500

diana.davis gravatar image

One of my collaborators has made an image of some regions, and now my job is to superimpose a plot on it. So I would like to do something like the following:

pic = load('pic.png')
pts = list_plot(points) 
fig = pic + pts

However, I get the error:

IOError: [Errno 2] No such file or directory:

Is there a way to get around this, or perhaps convert my png to a sobj? (The path to the file is correct.)

edit retag flag offensive close merge delete


The quick doc of load, accessed for instance via ?load contains the information:

   Load Sage object from the file with name filename, which will have
   an ".sobj" extension added if it doesn’t have one.  Or, if the
   input is a filename ending in ".py", ".pyx", ".sage", ".spyx",
   ".f", ".f90" or ".m", load that file into the current running

   Loaded files are not loaded into their own namespace, i.e., this is
   much more like Python’s "execfile" than Python’s "import".

   This function also loads a ".sobj" file over a network by
   specifying the full URL.  (Setting "verbose = False" suppresses the
   loading progress indicator.)

and so on. Sage expects code, not a pic! Just take a pencil...

dan_fulea gravatar imagedan_fulea ( 2017-10-26 19:09:51 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2017-10-27 01:37:21 -0500

ndomes gravatar image

updated 2017-10-27 01:41:33 -0500

See an example using PIL here.

Please note: The images 'test.png' and 'test2.png' have to match, (same size, aspect_ratio).

edit flag offensive delete link more


Thank you! This seems very promising. I used your code to try to add a red line on top of my image:

p = plot(x,-2,2,color='red',aspect_ratio=1, figsize=12.3)'test.png')
import PIL
img ='pic.png')
img2 ='test.png')
img3 = PIL.Image.blend(img,img2,alpha=0.7)

I adjusted the figsize of the line image to be 12.3 so that it matches my "pic" image, and both images are 906 x 906. However, I still get the following error:

ValueError: images do not match

Do you have any suggestions? When I run just


each associated image opens as a bmp in a separate window, so it seems that the files are treated equally.

diana.davis gravatar imagediana.davis ( 2017-10-27 08:47:30 -0500 )edit

You can resize an image. See PIL or PILLOW documentation.

img2 = img2.resize(img.size)
img3 = PIL.Image.blend(img,img2,alpha=0.7)
ndomes gravatar imagendomes ( 2017-10-27 15:43:16 -0500 )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

1 follower


Asked: 2017-10-26 14:05:01 -0500

Seen: 65 times

Last updated: Oct 27 '17