There is no PIL on Python3. Pillow seems to be the right way but it is not in a standard library. Is there a way to convert gif (or another image format) to numpy array that is not require installation of additional python packages?
You can also use the libraries PILasOPENCV or gif2numpy from Pypi. Install them with
pip install gif2numpy
gif2numpy works like this:
import gif2numpy
import cv2
np_frames, extensions, image_specifications = gif2numpy.convert("yourimage.gif")
cv2.imshow("test", np_frames[0])
cv2.waitKey()
If you have SciPy installed (as, I assume, most people using NumPy do), ndimage allows you to read in images as NumPy arrays:
from scipy import ndimage
im_array = ndimage.imread("image_file.gif")
ndimage is good. if you don't wanna see deprecated warning, you can use
import matplotlib.pyplot as plt
img_array = plt.imread('image_file.gif')
Related
I am reading a Python programming book. The author said that the map function was one way to apply math package functions such as sqrt to elements in a 1D array. I followed the book's instructions and ran the following code:
from math import sqrt
from numpy import array
a = array([4,9,16],float)
b = array(map(sqrt,a),float)
print(b)
Instead of the expected [2.0 3.0 4.0], the console displayed
TypeError: float() argument must be a string or a number, not 'map'
I also tried with the numpy sqrt; however, the console displayed the same error.
How can I improve my code to apply these elementary functions to my arrays?
I would appreciate any constructive criticism since I am new to stackoverflow and programming in general.
The problem with this is that map returns a map object, and not a list, which is what I believe you want.
You could just cast it to list like so:
b = array(list(map(sqrt, a)), float)
Of course this is only because you wanted to use the map function, you could always just do:
import numpy as np
b = np.sqrt(a)
Hope that helped =).
I am trying to use Artificial Intelligence algorithm to replace a system which identifies correct quantity. The quantity will be considered as "Yes" if it's in multiple of a number and "No" is it's not in multiples. Also, the other factor which it uses are > and < a number. I tried to use scikit learn RandomForestClassifier algorithms, but it doesn't get trained for the multipliers. Can you please suggest an algorithm which will best suit this. Thanks.
I tried to use scikit learn RandomForestClassifier algorithms
import sklearn
import seaborn as sns
import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.neural_network import MLPClassifier
from sklearn import tree
from sklearn.preprocessing import scale
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.model_selection import train_test_split
%matplotlib inline
from sklearn import svm
from TFANN import ANNR
from google.colab import files
files.upload()
data=pd.read_csv('qty.csv')
data.head()
data.info()
validate=LabelEncoder()
data['Type']=validate.fit_transform(data['Type'])
data['ans']=validate.fit_transform(data['ans'])
data.head()
sns.countplot(data['ans'])
X=data.drop('ans', axis=1)
y = data['ans']
X_train, X_test, y_train, y_test= train_test_split(X,y,test_size=0.2, random_state=42)
#sc=StandardScaler()
#X_train=sc.fit_transform(X_train)
#X_test=sc.fit_transform(X_test)
print(X_train)
rfc = RandomForestClassifier(n_estimators=200)
rfc.fit(X_train,y_train)
pred_rfc=rfc.predict([[0,12,20]])
#print(X_test)
print (pred_rfc)
If I want to predict multiple of 12 in the following, it doesn't enter code herework as expected. How can I use AI algorithm to train multiples?
pred_rfc=rfc.predict([[0,12,2400]])
In this case I'd start by further defining the problem. Do you need it to work for only multiples in your training set, all multiples within a specified range, or all multiples unconstrained?
If you only need it to work for values in your training set, then most ML algorithms will work just fine. If you need it to work on all values in a specified range, then again most ML algorithms will work just fine, but some might require some additional refinement. If you need it to work for all multiples, then you need to focus on selecting an appropriate underlying model.
A random forest like you are using here will not perform well beyond the extremes (high and low) of your training data because the underlying model does not extrapolate beyond the extremes of the training data. There are plenty of alternative models that can precisely match multiples however, for example a sine wave. The period of a sine wave determines how often the value reaches 1, so if you learn the correct period from the data, then you can predict all multiples with some degree of success.
I know I'm being a bit too picky but I really want to know which approach is better performance-wise in ES6:
import A from 'blabla/A';
import B from 'blabla/B';
import C from 'blabla/C';
or
import {A, B, C} from 'blabla';
If you are using Webpack or another tree-shaking bundler, both are roughly equivalent.
Assuming there is some submodule D in blabla that you don't want or need, your first example explicitly tells Webpack that you only need A, B, and C. In the second example, Webpack pulls in them all, but then should throw away D because it isn't actually used.
There may be some MINOR efficiency gains one way or another, but it generally won't be a big deal.
Is there a way to access Math functions such as "random", "floor", etc. in AngularDart? I know this can be done in AngularJS but I'm new to AngularDart and can't figure out what I'm missing (maybe an 'import') in order to do so.
The following is what I've imported:
import 'package:math/math.dart';
import 'package:math_expressions/math_expressions.dart';
This has nothing to do with AngularDart. You can use them everywhere, where you can write Dart code.
Some basic math methods are in the math package but methods that only take one argument are often methods of the int/double/num type itself.
new Random().nextInt(100);
(100/3).floor();
I would like to have a one click solution for exporting my Sketchup Pro 8 Scenes to individual 2D dwg files, similar to that using view.write_image?
Thanks
Unfortunately the SketchUp Ruby API doesn't expose any means of exporting 2D DWGs. You can only export 3D.
I think You can use sketchup-dxf-stl-exporter to control your export in .dxf format. For example you can move all you 2D entities to a special layer
autocad_entities = Array.new
Sketchup.active_model.entities.each do |entitie|
autocad_entities.push entitie if entitie.layer.name == 'AutoCad Layer'
end
and use skp_to_dxf.rb to export these entities.
Hope it help you.