Python how to resize images? - python

I am getting confused over all the different python datatypes. I wrote a face recognition algorithm in MATLAB, I want to redo it it Python. I am stuck trying to resize my images. How do I do that? Can someone link me to the things that i need to read to understand the difference in the functions between MATLAB in Python? Like MATLAB got imresize but Python got what?
Is there something like import all_matlab_functions so I can python in matlab language ?
I cant believe im struggling so hard. Help! My code below shows how stupid i am with python currently.
%matplotlib inline
import matplotlib.image as mpimg
import glob
import matplotlib.pyplot as plt
image_list = []
for filename in glob.glob('<directory>.pgm'):
im = mpimg.imread(filename)
image_list.append(im) # read all image
from PIL import Image
haha = image_list[1].resize((10 10), resample=0) # try to resize image BUT FAIL

using the jupyter qt-console interpreter, or jupyter notebook you can set the interpreter environment magic %pylab inline so that images will be displayed inline and the matplotlib.pylab namespace and numpy namespaces will be imported into the current namespace. All this means though is that you don't have to prefix the commands with matplotlib.pylab.plot or plt.plot, for example. You can just use plot().
%pylab inline
x = linsapce(1,2*pi,100)
y = sin(x)
plot(x,y)
matplotlib is meant to give good tools for plotting that look similar to matlab plotting, numpy for arrays and mathematics, and scipy gives you tools for more complex functions. For example, scipy.misc.imresize can do what you are looking for:
http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.misc.imresize.html
If you are looking for a direct way to run your matlab programs outside of matlab, you might want to try Octave instead.
https://www.gnu.org/software/octave/

Related

Showing numpy array as image with animation

I have a 2D numpy array which I want to show as an image. Now using matplotlib plt.imshow(my_array) works fine. The problem is that my array keeps on changing. There is a function foo() which changes the array. I want to show that array as image and also the changes done by foo function. It should be only one image window and the updates are done in that window instead of creating new one. FuncAnimation is one way but it is used with plots. How can I use it to show image?
My image is actually an arena of obstacles. I am trying to show path finding and visualise it. Breadth-First Search and Dijkstra's algorithm I am trying to visualise.
Kindly help. Thank you.
Not knowing how your code is structured but one solution to your problem could be using plt.pause()
An example,
import numpy as np
from matplotlib import pyplot as plt
def draw_me(img):
plt.pause(.01)
plt.imshow(img)
for idx in range(12):
img = np.random.randint(0, 255, (12,12))
draw_me(img)
note if you are using Sypder make sure you're not inline ploting in the console. For me that means entering the following in the console,
%matplotlib auto
%matplotlib qt

Why it shows "NameError: name 'histogram' is not defined"?

I'm taking a course on Coursera and doing an assignment after watching a teaching video.
I wanna know why it shows "NameError: name 'histogram' is not defined" after running my own code.
Add histogram library in your code. please add line in the starting of the code. I should work properly.
from histograms import histograms
You'll need to import the Histogram function as it is not built into Python. There are a few different packages that offer histograms -- for example pyplot and numpy. Here's an example using numpy
import numpy as np
np.histogram([1, 2])
Documentation: https://numpy.org/doc/1.18/reference/generated/numpy.histogram.html

Incorrect backend configuration with macosx in (old) matplotlib: plt.ion different from savefig, here overwritten alpha keyword

I am using interactive python with plt.ion() for generating figures (v2.7) and have noticed that the figure looks different from the figure exported by savefig (this is not a DPI issue (cf. matplotlib savefig() plots different from show()) - I think it might be a backend issue, but would appreciate help as I don't understand this properly).
Specifically, I wanted visualise the importance of a series of points by the intensity of their colour, which I thought I could do with the "alpha" keyword in matplotlib.
When I just do this, this works fine,
but when I want to add a line to the figure, the alpha keyword seemed to not work any more, and plt.ion() shows this:
I initially thought that perhaps the following issue on github may be related:
https://github.com/matplotlib/matplotlib/issues/4580
but then I noticed that exporting the figure actually produced the following file (i.e. as desired):
It would be great to understand a bit better what is going on, and how I can avoid such issues in the future. Is plt.ion()/plt.show() not the best way to show figures in interactive python, or is this an issue with the alpha keyword?
The code is here:
import numpy as np
from numpy import random as random
from matplotlib import pyplot as plt
fig2,ax2=plt.subplots(1,1,figsize=(3,3),sharey=True)
for ii in range(1):
ax2.plot(np.linspace(0,200,200), [0.1]*200, c= 'k')
for i in range(200):
test2=random.randint(5)
ydata= random.rand(test2)
test = random.rand(test2)
for j in range(test2):
ax2.plot(i,ydata[j],'o',ms=4, c= 'Darkblue',alpha=test[j],markeredgecolor='None')

Changing Figure Size in Sympy.mpmath.plot

I hope this question isn't too elementary. I've searched extensively for a solution but haven't discovered one yet.
I've recently begun using Jupyter Notebook with Sympy to take notes and do my homework in my Calculus II class (and what a HUGE BENEFIT this has been!).
However, my sole problem with it is that I'm unable to figure out how to configure the size (i.e. the dimensions in pixels) of the plot figure.
It's easy enough to do using matplotlib directly (matplotlib.pyplot.figure() specifically), but I'm using the Sympy.mpmath.plotmodule because Sympy works much better for the symbolic manipulation we're doing in this course. I know Sympy has its own plotting module, but the one in mpmath seems easier to use so far (with the exception of this one issue, of course).
However, I've looked through the mpmath documentation and have googled the problem repeatedly, without a solution.
How can you change the size of the image that results from plotting a function using the mpmath API?
You may try changing the size of sympy's plots via pyplot's rcParams:
import sympy
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = 10, 3
sympy.mpmath.plot([cos, sin], [-4, 4])

Pylab documentation

I visited the scipy site for PyLab. I could not find its documentation there. The matplotlib site also does not provide any information on it.
Where can I find a tutorial/documentation on PyLab?
Pylab is basically just Numpy and Matplotlib under a unified namespace. Learn about either of those and you will understand Pylab.
If you want to plot things in scripts it is generally preferred that you use import matplotlib.pyplot instead of import pylab, but really the choice is up to you.
If you want to have interactive plotting (for instance, by calling ipython --pylab) then pylab is the way to go. However pyplot can also be put in an interactive mode using pyplot.ion().
Some more information can be found here:
What is the difference between pylab and pyplot?
Exact semantics of Matplotlib's "interactive mode" (ion(), ioff())?

Categories

Resources