Ask Your Question
1

Can I import a graphic and add to it?

asked 2017-10-26 21:06:06 +0100

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
fig.show()

However, I get the error:

IOError: [Errno 2] No such file or directory:
'pic.png.sobj'

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

Comments

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

Docstring:     
   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
   session.

   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-27 02:09:51 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2017-10-27 08:37:21 +0100

ndomes gravatar image

updated 2017-10-27 08:41:33 +0100

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

Comments

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

var('x')
p = plot(x,-2,2,color='red',aspect_ratio=1, figsize=12.3)
p.save('test.png')
import PIL
img = PIL.Image.open('pic.png')
img2 = PIL.Image.open('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

img.show()

or

img2.show()

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 15:47:30 +0100 )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 22:43:16 +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

1 follower

Stats

Asked: 2017-10-26 21:05:01 +0100

Seen: 833 times

Last updated: Oct 27 '17