Unable to display IPython object in ubuntu, but displayed in colab output - python

I have a bunch of images in "test" folder and I have a python code in a file to display those images. Here is the code
import torch
from IPython.display import Image, clear_output
import matplotlib
import glob
import PIL
from IPython.display import Image, display
from io import BytesIO
print("hi")
for imageName in glob.glob('test/images/*.jpg'): #assuming JPG
print("in \n")
display(Image(filename=imageName)) #displaying successfully in colab
print("out")
print("\n")
and here is the output when I ran the python file in ubuntu terminal.
I have no other tab opened to display image the image. I tried other answers in stack overflow but didn't work. However, the images are visible in colab's output.

You are running in a terminal, terminals can only display text.
Thus display(Image(...)) cannot display your image.
display and Image are really IPython/Jupyter centric utilities that cannot completely function when using purely Python.
If you want to show images when running in a terminal you will need to use something like pillow or matplotlib and ask them to open a new window with the image, and depending on how you do it, you may need to close the window for the program to keep executing.
You can also locally run jupyter notebook (or, jupyter lab) to open a web interface that should support images.

Related

cant play audio using Ipython.display.Audio in Pycharm, but works in jupyter notebook

when I try in Pycharm to play an audio sound using the following code, it doesn't work. the code finishes without error, but nothing happens.
the same code works in Jupyter notebook and will create a small interface to play the audio sound. How can I make it work in Pycharm as well?
below is the code that works in Jupyter notebook
import numpy as np
import IPython.display as ipd
seconds=1
sample_rate=4000
t=np.arange( int(seconds*sample_rate) ) / sample_rate
x=np.sin(2*np.pi*440*t)
sound=ipd.display(ipd.Audio(data=x, rate=sample_rate))

Why does the PIL module ImageDraw.Draw exit without an error when I try to draw on a tiff?

I'm trying to open a TIF using PIL and create a draw object. Opening the image works just fine but when creating the draw object, the program stalls for about 2 seconds then exits without an error.
My system is Windows 10. I created an anaconda environment with:
python=3.9.15
pillow=9.0.1
I ran the following commands:
>>> import os
>>> from PIL import Image, ImageDraw
>>> img_path = os.path.join('path', 'to', 'image.tif')
>>> os.path.isfile(img_path)
True
>>> im = Image.open(img_path)
>>> draw = ImageDraw.Draw(im)
The ImageDraw.Draw causes the program to exit without any error.
One image on which I had this problem is accessible via WeTransfer. I had this problem using multiple tifs, I also tried changing the file extension to tiff and it didn't work. I converted the tifs to pngs and the code worked just fine.
I figured it out. Updating the conda package to pillow=9.2.0 didn't work so I used pip instead to download pillow=9.3.0 (pip install Pillow). That solved my problem.

Converting python to exe gives an exe with black screen

I have made a python app in one python file with the following import statements:
import numpy as np
import PySimpleGUI as sg
import cv2
from PIL import Image, ImageTk
import sys
import os
import pydicom as dicom
from io import BytesIO
import matplotlib.pyplot as plt
from pydicom.pixel_data_handlers import convert_color_space
from scipy.optimize import curve_fit
When I run my app (in spyder), it gives simply a window telling the user to select a folder:
When the user chooses a folder and click ok, the app starts to open other window with buttons to click on etc. I have tested in spyder many times that my code does not contain error and in particular will not crush. My goal is to convert this one single python file into an exe such that the user can just double click on exe (without python environment) such that the code starts to run and they should see the window telling them to select folders immediately after double click.
I use pyinstaller for the task (with command pyinstaller --onefile main.py, where main.py is the my file. The process is slow (and produces a lot of text) but there does not seem to have errors. When I click on the final exe file, I get two empty windows:
What can there possibly go wrong? Is there any workaround to this? That is, is there anything other than pyinstaller to help me with the task?

python PIL show image - file system error

I try to take a screenshot and display it with the following code:
import pyautogui
im = pyautogui.screenshot()
im.show()
but it says in the console Access is denied. and pops a windows error:
same error if i save the picture and try to double click it from the explorer (not with python), but I can open it with sublime text for some reason.
code used for saving:
import pyautogui
import PIL
im = pyautogui.screenshot()
im.save(r'screenshot1.png')
im = PIL.Image.open(r'screenshot1.png')
im.show()
how do i solve this? should i change permissions on taking the screenshot somehow?
You need to change the startup type of your Windows license management service.
Search for "Services" in your computer, and open the Services Management. Find the Windows license management service, and change the startup type to "Automatic". Press "apply" and "ok".
Refer to this for visualization of the process.

Inserting image into IPython notebook markdown

I am starting to depend heavily on the IPython notebook app to develop and document algorithms. It is awesome; but there is something that seems like it should be possible, but I can't figure out how to do it:
I would like to insert a local image into my (local) IPython notebook markdown to aid in documenting an algorithm. I know enough to add something like <img src="image.png"> to the markdown, but that is about as far as my knowledge goes. I assume I could put the image in the directory represented by 127.0.0.1:8888 (or some subdirectory) to be able to access it, but I can't figure out where that directory is. (I'm working on a mac.) So, is it possible to do what I'm trying to do without too much trouble?
Most of the answers given so far go in the wrong direction, suggesting to load additional libraries and use the code instead of markup. In Ipython/Jupyter Notebooks it is very simple. Make sure the cell is indeed in markup and to display a image use:
![alt text](imagename.png "Title")
Further advantage compared to the other methods proposed is that you can display all common file formats including jpg, png, and gif (animations).
Files inside the notebook dir are available under a "files/" url. So if it's in the base path, it would be <img src="files/image.png">, and subdirs etc. are also available: <img src="files/subdir/image.png">, etc.
Update: starting with IPython 2.0, the files/ prefix is no longer needed (cf. release notes). So now the solution <img src="image.png"> simply works as expected.
I am using ipython 2.0, so just two line.
from IPython.display import Image
Image(filename='output1.png')
Getting an image into Jupyter NB is a much simpler operation than most people have alluded to here.
Simply create an empty Markdown cell.
Then drag-and-drop the image file into the empty Markdown cell.
The Markdown code that will insert the image then appears.
For example, a string shown highlighted in gray below will appear in the Jupyter cell:
![Venus_flytrap_taxonomy.jpg](attachment:Venus_flytrap_taxonomy.jpg)
Then execute the Markdown cell by hitting Shift-Enter. The Jupyter server will then insert the image, and the image will then appear.
I am running Jupyter notebook server is: 5.7.4 with Python 3.7.0 on Windows 7.
This is so simple !!
UPDATE AS OF March 18, 2021:
This simple "Drag-and-Drop-from-Windows-File-System" method still works fine in JupyterLab. JupyterLab inserts the proper HTML code to embed the image directly and permanently into the notebook so the image is stored in the .ipynb file. I am running Jupyter Lab v2.2.7 on Windows 10 Python 3.7.9 still works in JupyterLab. I am running Jupyter Lab v2.2.7 using Python 3.7.9 on Windows 10.
This stopped working in Jupyter Classic Notebook v6.1.5 sometime last year. I reported an bug notice to the Jupyter Classic Notebook developers.
It works again in the latest version of Jupyter Classic Notebook. I just tried it in v6.4 on 7/15/2021. Thank you Jupyter NB Classic Developers !!
If you want to display the image in a Markdown cell then use:
<img src="files/image.png" width="800" height="400">
If you want to display the image in a Code cell then use:
from IPython.display import Image
Image(filename='output1.png',width=800, height=400)
[Obsolete]
IPython/Jupyter now has support for an extension modules that can insert images via copy and paste or drag & drop.
https://github.com/ipython-contrib/IPython-notebook-extensions
The drag & drop extension seems to work in most browsers
https://github.com/ipython-contrib/IPython-notebook-extensions/tree/master/nbextensions/usability/dragdrop
But copy and paste only works in Chrome.
I put the IPython notebook in the same folder with the image. I use Windows. The image name is "phuong huong xac dinh.PNG".
In Markdown:
<img src="phuong huong xac dinh.PNG">
Code:
from IPython.display import Image
Image(filename='phuong huong xac dinh.PNG')
First make sure you are in markdown edit model in the ipython notebook cell
This is an alternative way to the method proposed by others <img src="myimage.png">:
![title](img/picture.png)
It also seems to work if the title is missing:
![](img/picture.png)
Note no quotations should be in the path. Not sure if this works for paths with white spaces though!
Change the default block from "Code" to "Markdown" before running this code:
![<caption>](image_filename.png)
If image file is in another folder, you can do the following:
![<caption>](folder/image_filename.png)
Last version of jupyter notebook accepts copy/paste of image natively
For those looking where to place the image file on the Jupyter machine so that it could be shown from the local file system.
I put my mypic.png into
/root/Images/mypic.png
(that is the Images folder that shows up in the Jupyter online file browser)
In that case I need to put the following line into the Markdown cell to make my pic showing in the notepad:
![My Title](Images/mypic.png)
minrk's answer is right.
However, I found that the images appeared broken in Print View (on my Windows machine running the Anaconda distribution of IPython version 0.13.2 in a Chrome browser)
The workaround for this was to use <img src="../files/image.png"> instead.
This made the image appear correctly in both Print View and the normal iPython editing view.
UPDATE: as of my upgrade to iPython v1.1.0 there is no more need for this workaround since the print view no longer exists. In fact, you must avoid this workaround since it prevents the nbconvert tool from finding the files.
I never could get "insert image" into a markdown cell to work. However, the drag and drop entered the png file saved in the same directory as my notebook. It brought this text into the cell
""
The shift + enter > image is now displayed in notebook.
FWIW
You can find your current working directory by 'pwd' command in jupyter notebook without quotes.

Categories

Resources