matplotlib is not showing output in simple tutorial program - python

import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib
img = cv2.imread('flood.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])
plt.show()
Above is my code and when I run this program Ii get
"/home/badal/Python-3.7.1/image_process/im2.py:9: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
plt.show()
After using tkagg I get
"Traceback (most recent call last):
File "/home/badal/Python-3.7.1/image_process/im2.py", line 5, in
import tkinter as tk
File "/usr/local/lib/python3.7/tkinter/init.py", line 36, in
import _tkinter # If this fails your Python may not be configured for Tk ModuleNotFoundError: No module named '_tkinter'"
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib
import tkinter as tk
matplotlib.use('tkagg')
img = cv2.imread('flood.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])
plt.show()
I have already installed tkinter, so I don't know what to do.

I think that the problem is on cv2 module that I don't know what is.
img = cv2.imread('flood.jpg',0)
However I made some changes.
First of all I don't import cv2 module.
Second I import Image from PIL.
And to open the pic I made
img = Image.open('flood.jpg')
So your code becomes:
import numpy as np
#import cv2
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image
img = Image.open('flood.jpg')
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])
plt.show()

Related

Why i am getting "name 'os' is not defined" error?

Why i am getting this error? it should be work.Probally i'm missing something from my sight.
Before that same thing happen the for Classes.I tried rewrite and still same.
import tensorflow as tf
import cv2
import os
import matplotlib.pyplot as plt
import numpy as np
img_array = cv2.imread("Training/0/Training_233976.jpg")
img_array.shape
plt.imshow(img_array)
Datadirectory = "Training/"
Classes = ["0","1","2","3","4","5","6"]
for category in Classes:
path = os.path.join(Datadirectory, category)
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path,img))
plt.imshow(cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB))
plt.show()
break
break
I am formalizing in an answer, all you need to do is add a line at the top
import os
import os
use this at beginning of your code or where you are importing other libraries and codes .

How do you load an image on VSC for Python

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread('25.png')
plt.imshow(image)
I've used this code here to try and produce the image but it keeps saying that the module does not exist.
First, you should import 'matplotlib.pyplot'
Second, make sure the photo is in the same directory as your code is.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread('25.png')
imgplot = plt.imshow(image)

python just import fails but works with from

Why does this not work:
import matplotlib.pyplot as plt
import os
import skimage
camera = skimage.io.imread(os.path.join(skimage.data_dir, 'camera.png'))
#plt.show(io.imshow(camera))
But using from skimage import io does. So this works:
import matplotlib.pyplot as plt
import os
import skimage # I still need to import skimage to get the data_dir
from skimage import io
camera = io.imread(os.path.join(skimage.data_dir, 'camera.png'))
#plt.show(io.imshow(camera))
I thought
import skimage
skimage.io."something"
Was equivalent to
from skimage import io
io."something"
I thought
import skimage
skimage.io."something"
Was equivalent to
from skimage import io
io."something"
It's not.
import skimage
causes python to look for the skimage module. Maybe there's a __init__.py that sets up what becomes visible and what is done when you import that module.

Display a .png image from python on mint-15 linux

I am trying to display a .png file I constructed using the following.
import pydot, StringIO
dot_data = StringIO.StringIO()
tree.export_graphviz( clf, out_file = dot_data,
feature_names =['age', 'sex', 'first_class', 'second_class', 'third_class'])
graph = pydot.graph_from_dot_data( dot_data.getvalue())
graph.write_png('titanic.png')
from IPython.core.display import Image
Image( filename ='titanic.png')
I tried the following but neither errors nor .png are displayed:
from PIL import Image
image = Image.open("titanic.png")
image.show()
if you just want to display it, you may use matplotlib:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('file-name.png')
plt.imshow(img)
plt.show()

Can not save file using the below python code. Error: numpy.ndarray object has no attribute 'save'

import os
import sys
import numpy as np
import scipy
import pylab
import pymorph
import mahotas
import matplotlib.pyplot as plt
import Image
from scipy import ndimage
from pymorph import regmax
from PIL import Image
path='all_images'
for file in os.listdir(path):
current = os.path.join(path, file)
extension = os.path.splitext(current)[-1]
fileType = extension.upper()
print(current)
if os.path.isfile(current):
img = mahotas.imread(current)
imgf = ndimage.gaussian_filter(img, 8)
pylab.gray()
imgf.save('dnaa.gif')
Can not save file using the below python code. Error: numpy.ndarray object has no attribute 'save'. Can anyone help how to save file using pylab. I guss the last line of the code has some issue.
Use mahotas.imsave('dnaa.gif', imgf) instead. The NumPy array you get from gaussian_filter doesn't have save functionality built in.

Categories