Inline display when using GalSim on iPython Notebook - galsim

It seems that all the demos write the image file to the system so that we can view it with DS9. Is there a way we can view the images we make inline on iPython Notebook, as we code? This would speed up my coding a lot.
Thank you!

GalSim Image objects store the pixel values in a numpy array: im.array. So if you're using an iPython notebook with %matplotlib inline, you can just do plt.imshow(im.array) to display an image of the data on the screen.

Related

Merge Image with Jupyter Notebook To Be One File

When we load an image in jupyter notebook , in actual we have two files. Is there a way to merge the both files in single note book file ?
Thank you .
In code cells
When you use code in code cell like below to show an image from an image file inside a notebook, it will actually get encoded within the notebook in the form of base64:
#use code like below to show images as output line because encodes as Base64 code saved within the notebook file
from IPython.display import Image
Image("<myimage_file>.png")
The easiest way to see that is to run such a cell, save the notebook, and inspect the underlying text of the notebook within a text editor. (This is possible right in Jupyter.)
For the corresponding code, you should now see something like this:
"cell_type": "code",
"execution_count": 1,
"id": "72bb5e3d-85c4-4c82-bb18-b466e06b98e7",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUg ...
The ... represents a long string with the image data encoded in base64. Importantly, if you now take that saved notebook and open a new Jupyter session somewhere else where you don't have that image file and you open up the Jupyter notebook, you'll see the image stays visible even without the image file, as long as you don't try to run the code in the cell. If you don't happen to have another computer with Jupyter handy, you can test this by going here and clicking launch binder to spin up a temporary notebook session accessible in your browser from a remote system served by MyBinder.org then dragging and dropping your saved notebook file in the file browser pane on the left side. Double-click to open the notebook and you should see your notebook open and display the image even though the remote system doesn't have your file. If you run the cell though, it isn't going to like the absence of the file, and so the best practice in data management with Jupyter notebook work is to place the notebook(s) and associated files a project folder and transport the entire directory.
There are ways to go back from base64 to the file, so as long as you have the notebook with the encoding you can regenerate the image, see here which is based on this stackoverflow post to convert the string text to bytes. However, I found that replacing the str.encode(string) part of my suggested code with string.encode(), and according to here that may perhaps be better.
In Markdown cells
Unfortunately, last I knew Markdown cells don't do this behavior and so you have to do additional steps to get an image encoded into the markdown cell of a notebook. It can be done though as discussed at the bottom of this post entitled 'Embedding image in ipython notebook for distribution'. (IPython notebooks are the previous version of what evolved into Jupyter notebooks by adding support for many kernel languages.)
I stored a notebook demonstrating using the outcome of following that process to embed images into markdown code within a notebook here, with some links to what I used to put it together. Looking at the text underlying that notebook in your text editor, you'll see the image code is embedded and by opening it elsewhere, you'll conform that it is portable.
Process to generate such base64 encoded image code I adapted from this answer here to 'embedding image into jupyter notebook and exporting to HTML':
%pip install pillow
image_to_convert = "<your_img_file_name_here>.png"
import base64, io, IPython
from PIL import Image as PILImage
image = PILImage.open(image_to_convert )
output = io.BytesIO()
image.save(output, format='PNG')
encoded_string = base64.b64encode(output.getvalue()).decode()
html = '<img src="data:image/png;base64,{}"/>'.format(encoded_string)
print("COPY THE TEXT BELOW INTO A MARKDOWN CELL:")
print(html)
In relation to placing the image encoded as Base64 into the img tag, I found this very helpful.
Someone posted code here to take base64-encoded images from notebooks and embed them in markdown; however, I haven't tried it yet. I also have tried the early part of this answer under 'How to embed image or picture in jupyter notebook, either from a local machine or from a web resource?' which is another take on this; however, I did test that resulting code there make a smiley when in a markdown cell in a notebook. (In fact you can take the part between parentheses that begins data and paste into the URL bar of your browser and see an image too.)

How does python support the display of images on canvas or label widgets?

The question is not if Python supports the display of images, but rather how it supports the display of images. By choice I am using Python 2.7, and this has Tkinter and Image libraries/modules installed. I don't have the pygame module, which seems to readily support the use of images on the graphical user interface.
To open the file or filename on the system, I am using tkFileDialog and this module works as intended. Then, I can combine the output of the tkFileDialog command with the Image module to create an image object (using the open method). Having done this, I can show my graphic using Image's show method, see below:
import Image
import Tkinter
import tkFileDialog as File
root = Tkinter.Tk()
root.title('Image Test')
root.file = File.askopenfile()
root.image = Image.open(root.file)
root.label = Tkinter.Label(image=root.image)
root.label.grid()
Python uses Image Magick to present a zoomed image, if after line 7, I use
root.image.show()
However, when I attempt to load the image to a Tkinter window using the Label (as illustrated in my code) and PhotoImage widgets (which are described to support image display) python throws a TclError and Runtime error, respectively. The messages are: it is "too early to create image" when using PhotoImage, and "image doesn't exist" when using Label (line 8 in my code). Can anyone provide assistance without suggesting that I add modules to my python (version: 2.7) install?
I am looking to work with bitmaps and jpeg/jpg/jpe images. Preferably, I would like to load the image onto the Canvas object using the create_image method. That is, if and only if I can load the image in to a PhotoImage object (no code included). I will settle for a Label with the image, that will eventually be loaded onto a Canvas.
Useful stackoverflow questions, for reference:
•How do I insert a JPEG image into a python Tkinter window?
•How to add an image in Tkinter?
•Cannot construct tkinter.PhotoImage from PIL Image
To everyone else following, it seems as though one can do just about any image file extension by opening their image using the Image module and programmatically saving that to a gif file type. Then, use the PhotoImage module to load the result to a Canvas widget.
import Tkinter, Image, tkFileDialog as File
filename = File.askopenfilename() #choose jpg
image = Image.open(filename)
image.save(fp='somename', format='GIF')
temporarygif = File.askopenfilename() #choose gift
photo = Tkinter.PhotoImage(file=temporarygif)
root = Tkinter.Tk()
root.title('Simple Image Display')
root.canvas = Tkinter. Canvas(root, options)
root.canvas.create_image(0,0,image=photo)
root.canvas.pack()
root.mainloop()
Great head turner.
Problems: the saved gif experiences some loss of color quality. It looks like a 256 color image, and not a full color image.

How to control the size of a picture using tkinter?

I want to control the size of the picture in m_image (say, I want it to be 420x380).
How do I do that?
import tkinter as tk
m_gui = tk.Tk()
m_image = tk.PhotoImage(file = 'pic.gif')
m_canvas = tk.Canvas(m_gui)
m_canvas.create_image(0, 0, image = m_image)
m_canvas.pack()
m_gui.mainloop()
It would be great if you could also give an explanation about the first two parameters of the create_image func.
Thank you
tk is a gui framework, not an image manipulation program. Two solutions:
Use an external image manipulation program to create a .bmp, .gif, or (if using tcl/tk 8.6) .png file with the size you want.
Install pillow (pip install pillow worked for me), use it to do the needed image manipulation, and use its ImageTk module to "create and modify Tkinter BitmapImage and PhotoImage objects from PIL images".

Reuse inline image in IPython notebook

Is it possible to create an inline png image, e.g. using matplotlib, and re-using it in a markdown cell (via html) without saving it on hard disk first?
IPython notebook saves inline images in the ipynb file, so the data is available, I wonder if it is also accessible?
One idea is to generate images for pretty slides (cell mode) and to to suppress the slides for image creation.
No it is not possible without saving the image. Using slide mode you can exclude some cells. is is also possible to use slide-mode and not to show the code if you are using custom templates.

Healpy Mollview function returned nothing

I am a new healpy user. I used healpy tutorial available at page http://healpy.readthedocs.org/en/latest/tutorial.html for creating a map but after execution of "healpy.mollview" command it returned nothing and no plot was visible. Need Help!
I have searched the problem already but unable to find the exact situation anywhere
Thanks,
Jibran
For using healpy plotting functions, the best is to use ipython, in particular:
ipython --pylab
You have to show the plot
import matplotlib.pyplot as plt
plt.show()
as you would do for any plot in matplotlib

Resources