Ask Your Question

How to import images in sagemath?

asked 2023-02-02 18:40:09 +0200

ajit gravatar image

Is there convenient way of importing images in various format such is PNG, JPG etc in Sagemath other than using python packages?

edit retag flag offensive close merge delete


Sage is built on Python, so Python packages are the most natural choice. What do you mean by "import," what do you want to do with the images, and why not use Python packages?

John Palmieri gravatar imageJohn Palmieri ( 2023-02-02 19:04:01 +0200 )edit

What do you mean by "import"ing images ?

  • If you want to add external images in a Jupyter notebook havin Sage cells, the easiest solution, as pointed out by @John Palmieri, is to use the Python tools interfacing with Jupyter ; a simple Google search on "Jupyter import images" will point you to a lot of ways to do this.

  • The same holds, mutatis mutandis, for importing images in a document using Sage parts, such as a SageTeX document (where the standard tools for inserting images work), or an Org document (ditto).

  • If you want to import images whose content is to be processed by Sage, the first part is to load them as Python objects. Again, a Google query on "Python image processing" will give you pointers to a variety of ways.


Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-02-02 20:13:59 +0200 )edit

Thanks John for your detailed answer. I was trying to image compression using SVD with SageMath and wanted the image as a matrix in the format that Sage is able to find its SVD. I did use python packages and succeeded.

ajit gravatar imageajit ( 2023-02-05 02:52:52 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2023-02-03 07:16:28 +0200

ortollj gravatar image

Hi @ajit

Maybe this example could help you (assuming you want to display an image in a cell ?, but not sure it is what you want)

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from matplotlib.pyplot import figure
#imgPath="C:\\Users\\User\\Documents\\IPYNBpc\\LinearAlgebraMIT\\" # from Firefox in W11
imgPath='/mnt/c/Documents and Settings/user/Mes documents/IPYNBpc/LinearAlgebraMIT/' #  Firefox  UBUNTU
figure(num=None, figsize=(figS, 3*figS), dpi=100, facecolor='w', edgecolor='k')
img = mpimg.imread(imgPath+"GthChap1Exercice11.png")
imgplot = plt.imshow(img)
edit flag offensive delete link more


Thanks a lot jean-pierre Ortolland. It is helpful.

ajit gravatar imageajit ( 2023-02-05 02:54:40 +0200 )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: 2023-02-02 18:40:09 +0200

Seen: 361 times

Last updated: Feb 03 '23