Trying to open .img file with matplotlib - python

I am trying to open an .img. I run the following code:
import matplotlib.pyplot as plt
from planetaryimage import PDS3Image
image = ('/Users/alyse/ldem_1024_00n_15n_150_180.img')
plt.imshow(image, cmap='gray')
I get the following error: TypeError: Image data of dtype <U46 cannot be converted to float

You can also use PIL. To install: pip install pillow
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
image = Image.open('/Users/alyse/ldem_1024_00n_15n_150_180.img')
image_gray = image.convert("L") # Where L is option for grayscale
array_gray = np.asarray(image_gray)
plt.imshow(array_gray, cmap="gray")
plt.show()

Related

Extracting data from table and saving it. Layout parser package

I am new in working with python and I am using Melissa Dell's package to extract data from a table image. My image looks like this:
enter image description here
And my code, for now, is the following one:
pip install layoutparser[ocr]
import layoutparser as lp
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
import numpy as np
import cv2
from google.cloud.vision_v1 import types
import json
import re
from google.cloud import vision
pip show google-cloud-vision
ocr_agent = lp.GCVAgent.with_credential('mycredebtials.json',
languages = ['es'])
img = plt.imread(r'D:\pdfDispacher.do_Página_2.jpg', cv2.IMREAD_COLOR)
print(img)
plt.imshow(img)
res = ocr_agent.detect(img, return_response=True)
texts = ocr_agent.gather_text_annotations(res)
layout = ocr_agent.gather_full_text_annotation(res, agg_level=lp.GCVFeatureType.WORD)
lp.draw_box(img, layout)
lp.draw_text(img, layout, font_size=12, with_box_on_text=True,
text_box_width=1)
What I need is to tell python to get all the columns and rows and save them in CSV format. But I am not able to get this done.
I really appreciate it if anyone can help me with the next lines.

How do I plot a histogram's image from Pyimagej?

# Create an ImageJ gateway with the newest available version of ImageJ.
import imagej
import pathlib
import numpy
ij = imagej.init()
# Load an image.
img_path = pathlib.Path('C:/Users/Bernardo/TCC/thyroid/1_1.jpg')
image = ij.io().open(str(img_path))
ij.py.show(image, cmap='gray')
I wanna plot a histogram using pyimagej, after reading this image.
well, you can just use matplotlib:
# Create an ImageJ gateway with the newest available version of ImageJ.
import imagej
import pathlib
import numpy
ij = imagej.init()
# Load an image.
img_path = pathlib.Path('C:/Users/Bernardo/TCC/thyroid/1_1.jpg')
image = ij.io().open(str(img_path))
ij.py.show(image, cmap='gray')
import matplotlib.pyplot as plt
plt.hist(image.flatten())

Viewing images from pickle file in python

I am a newbie in Machine Learning.I have a dataset of images present in .p format(pickle).
How to view the images present inside the file ? I seached the internet but I didn't any appropriate answers.
Please help me to solve this issue.
Code I used:
import pandas as pd
import pickle
objects = []
with (open("full_CNN_train.p", "rb")) as openfile:
while True:
try:
objects.append(pickle.load(openfile))
except EOFError:
break
print(objects)
My output when I tried pickle.load()
It's probably just a standard image matrix, just try using matplotlib.pyplot.imshow()
from matplotlib import pyplot as plt
img = ... # pickle load or whatever
plt.imshow(img)
plt.show()
Just load the pickle file, and use the "imshow" method to visualize.
import pickle
import matplotlib.pyplot as plt
pkl = open('pickled_image.pickle', 'rb')
im = pickle.load(pkl)
plt.imshow(im)

How to resize the image of the tree using sklearn tree and export_graph_viz within a Jupyter Notebook

I am using export_graph_viz to visualize a decision tree but the image spreads out of view in my Jupyter Notebook.
If this was a pyplot figure I would use the command plt.figure(figsize = (12,7)) to constrain the visualization. But in this case I do not know how to proceed.
Below is a snapshot of my Jupyter Notebook and what I see:
You can save the visualized tree to a file and then show it with pyplot.
Example:
import matplotlib.pyplot as plt
import pydotplus
import matplotlib.image as mpimg
import io
from sklearn.externals.six import StringIO
from sklearn.tree import export_graphviz
dot_data = io.StringIO()
export_graphviz(clf, out_file=dot_data, rounded=True, filled=True)
filename = "tree.png"
pydotplus.graph_from_dot_data(dot_data.getvalue()).write_png(filename)
plt.figure(figsize=(12,12))
img = mpimg.imread(filename)
imgplot = plt.imshow(img)
plt.show()
Result:

How to import png images Python

I've tried to import a png file in Python 3.6 with Jupyter Notebook with no success.
I've seen some examples that don't work, at least not anymore, i.e.
import os,sys
import Image
jpgfile = Image.open("picture.jpg")
There is no module called Image that I can install with either:
conda install Image
or
pip install Image
Any simple solution would be greatly appreciated!
You can display an image from file in a Jupyter Notebook as follows:
from IPython.display import Image
img = 'fig31_Drosophila.jpg'
Image(url=img)
where img = 'fig31_Drosophila.jpg' is the path and filename of the image you want. (here, the image is in the same folder as the main script)
alternatively:
from IPython.display import Image
img = 'fig31_Drosophila.jpg'
Image(filename=img)
You can specify optional args (for width and height for instance:
from IPython.display import Image
img = 'fig31_Drosophila.jpg'
Image(url=img, width=100, height=100)

Categories

Resources