Universitat Politècnica de Catalunya, 2023 > astronaut_grayscale = rgb2gray(astronaut) > astronaut = plt.imread('astronaut.png') Taking the astronaut image as an example: > from lor import rgb2gray In order to convert an RGB image to grayscale we can use the rgb2gray() function from the lor module which calculates the value of each pixel as the weighted sum of the corresponding red, green and blue pixels as: Y = 0.2125 R + 0.7154 G + 0.0721 B In order to visualize the image that we have just loaded we use plt.imshow(): > plt.imshow(moon, cmap='gray') Provided with a dependency on pillow (which comes installed with Anaconda), we can work with other formats such as JPG or BMP. It is relevant to mention that matplotlib can only read the PNG format natively. Given that it is two-dimensional, it represents a grayscale image. Provided with the function plt.imread() we obtain a numpy array of dimensions (512, 512) that represents our 'moon.png'. Indeed, along scikit-image, the main library that we will use is matplotlib. In order to illustrate the discussion of image processing, we have downloaded some classic images for signal processing from the scikit-image repository and saved them in our Python working directory. > my_recording = record() # Say something wise Hence, we are now able to do something like this: > sound(data, fs=4000) # The do note was recorded using a lower sampling frequency of 4000 Stream = p.open(format=pyaudio.paInt16, channels=1, rate=fs, input=True,Īrray = np.frombuffer(buffer, dtype='int16') When it comes to recording, the function below comes in handy: def record(duration=3, fs=8000): Stream = p.open(format=pyaudio.paInt16, channels=len(array.shape), rate=fs, output=True) Provided with this set of tools we can define the following function that allows us to play a numpy array of dtype=int16: import numpy as np Play audio by writing audio data to the stream using (), or read audio data from the stream using (). This sets up a pyaudio.Stream to play or record audio. To record or play audio, open a stream on the desired device with the desired audio parameters using (). To use PyAudio, first instantiate PyAudio using pyaudio.PyAudio(), which sets up the portaudio system. The main features outlined in its documentation are the following: In the case that it was recorded as a stereo sound signal it the shape of data would have been (8000, 2).įinally, in order to play or record audio, we can use the PyAudio package which can be installed by means of conda install pyaudio. The fact that the array only has one dimension means that 'do.wav' was a mono sound signal. wav file ($4000 Hz$ in this case) and a numpy array representing the data read with length 8000 which implies that the recording lasts $2s$. The wavfile.read() function returns both the sampling frequency of the. We can load it into Python as follows: > from scipy.io import wavfileĪrray(, dtype=int16) Your browser does not support the audio element. For instance, given the following recording of the note Do, The scipy.io.wavfile library allows us to deal with WAV files.
0 Comments
Leave a Reply. |