AttributeError: module 'numpy' has no attribute 'asarray' - python

I am using the Spyder IDE via Anaconda but the Numpy asarray method won't work.
from PIL import Image
import numpy as np
im = Image.open("photo.jpg", "r")
data = np.asarray(im)
print(data)
Based similar questions it is recommended to make sure there is no other file called numpy.py in the current directory, but I have checked and the only files are this .py and the image referenced.

Related

No module named 'netCDF4'

I am currently working with netCDF data in the Spyder IDE, however when I try to import 'Dataset' from the module
from netCDF4 import Dataset
it is giving me the following error: "No module named 'netCDF4'". I already installed the module but I guess it is not in the same environment where I installed spyder. How can I fix this? If it helps solving my question. When I try to install netCDF4 in the spyder console using 'pip install netCDF4' it gives the following error:
"/Applications/Spyder.app/Contents/MacOS/python: No module named pip
Note: you may need to restart the kernel to use updated packages."
I looked up the path of the module: '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/netCDF4'
and I guess the path for spyder is:
'/Applications/Spyder.app/Contents/MacOS/python'
How can I make sure that they are both located in the same environment?
I tried to look up the issue, however nothing really solved my problem.
Would be glad if anyone could help me out.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from netCDF4 import Dataset
from cftime import numb2date
import tifffile as tifffile
# define arrays
XNR=[] # number
Xdt=[] # time
YGP=[] # GPP
YPP=[] # PPFD
# read csv file and extract column 0 (timestemp), PPFD (column 54) and GPP (column 312)
# take a file from FLUXNET for validation of the GPP model
with open(r'/Users/janoschbeer/Documents/Studium/ESS/ES_Observ/Assignments/Lab6/FLX_SJ-Adv_FLUXNET2015_SUBSET_2011-2014_1-4/FLX_SJ-Adv_FLUXNET2015_SUBSET_DD_2011-2014_1-4.csv') as csv:
lines = csv.readlines()
for i in range(2, len(lines)):
tmp=lines[i].split(",")
XNR.append(float(i))
Xdt.append(float(tmp[0]))
YGP.append(float(tmp[312]))
YPP.append(float(tmp[54]))

OpenCV not implementing GOTURN file

I am getting this message when I try to create a tracker:
AttributeError: module 'cv2.cv2' has no attribute 'TrackerGOTURN_create'
I've downloaded goturn.caffemodel and goturn.prototxt, and they are in the same folder as my python file.
import cv2
import numpy as np
import os
#import time
ballTracker = cv2.TrackerGOTURN_create()

imageio: AttributeError: 'module' object has no attribute 'imread'

I come up with the error AttributeError: 'module' object has no attribute 'imread' while using the imageio module with the following code. I searched the community a lot but all of the errors about imread command are discussed under scipy, but nothing for imageio. So, any comments or links are appreciated! Here is my code:
import os
import imageio
os.chdir('C:/concent')
png_dir='gif/'
images=[]
for file_name in os.listdir(png_dir):
if file_name.endswith('.png'):
file_path = os.path.join(png_dir, file_name)
images.append(imageio.imread(file_path))
imageio.mimsave('gif/movie.gif', images)
Probably you have another file named imageio.py in the working directory.
The import statement will import files from the current directory before searching standard library and site package locations.
You should be able to fix the problem by renaming your local imageio.py file to something else that does not clash with other modules.

scipy.misc.pilutil import imread works but still can't read in data

I'm working through a tutorial and the code is:
import numpy as np
from scipy import misc
from skimage import data
photo_data = misc.imread('foo.jpg')
This didn't work and stack overflow searches suggested I needed to install scikit-image. I installed it, but that still didn't work. More searching said I also need to install Pillow. Installed Pillow and that still didn't work.
More searching suggested I needed to write the code as:
import scipy.misc
from scipy.misc.pilutil import imread
No error from that (!) but then the code I to read in the photo doesn't work:
photo_data=misc.imread('foo.jpg')
error: module.scipy.misc has no attribute imread
if I try:
photo_data = misc.pilutil.imread('foo.jpg')
error is very ugly and talks about imread being deprecated and the errors are a LOT more complicated.
Is there is simple way to read this photo in?
Thanks.

Why I am getting this error: object has no attribute 'astype'

I have defined a function myfunc inside a python file myfile. when i import this function from a jupyter notebook, it is showing the following error:
import numpy as np
import os
from scipy.misc import imread
import ast
from myfile import myfunc
.....
class_mask = np.equal(image, i)
class_mask = class_mask.astype(np.float32)
.....
AttributeError: 'NotImplementedType' object has no attribute 'astype'
However, whenever I have the myfile content in a cell and running inside the jupyter, it is working without any problem? What is the reason for this error?
Thanks
We can only import function from a python module/file. A Jupyter Notebook when you save have an extention of .pynb, that means its not actually a python module.
Just open your Notebook with a text editor, you probably see html like content. Which is not possible for a python module to contain.
You rather want to export the Jupyter Notebook as Python Module in the same directory from where you are trying to run your current program.
File -> Export as Python File
Once exported you can use it as just any other python module. Hope it helps!

Categories

Resources