How to flip an image horizontally in python ?

Examples of how to flip an image horizontally in python: [TOC] ### Get a vertically flipped image using pillow To flip an image horizontally a solution is to use the function [mirror()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example: from PIL import Image from PIL import ImageOps im = Image.open("lena.png") im = ImageOps.mirror(im) im.save("lena_mirror.png") im.show() [images:458;lena-mirror dim:1*2 size:75 caption:How to flip an image horiz

How to flip an image vertically in python ?

Examples of how to flip an image vertically in python: [TOC] ### Get a vertically flipped image using pillow To flip an image vertically a solution is to use the function [rotate()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example: from PIL import Image im = Image.open("eiffel_tower.jpg") im = im.rotate(180) im.save("eiffel_tower_fliped_vertically.jpg") [images:eiffel-tower;eiffel-tower-fliped-vertically dim:1*2 size:75 caption:How to flip an imag

How to read a VIIRS L2 CLDPROP netcdf file using python ?

An example of how to read a VIIRS L2 CLDPROP netcdf file using python 3: [TOC] ### Download a VIIRS L2 CLDPROP netcdf file To download a VIIRS L2 CLDPROP netcdf file, a solution is to go on [LAADS DAAC](https://ladsweb.modaps.eosdis.nasa.gov/search/order/2/CLDPROP_L2_VIIRS_SNPP--5110/2019-01-01/DB/World). Example of granule selected randomly that will be used hereafter: CLDPROP_L2_VIIRS_SNPP.A2019001.1742.001.2019062201640.nc ### Read a netCDF file with python To read the C

How to calculate the inverse of a matrix in python using numpy ?

To calculate the inverse of a matrix in python, a solution is to use the linear algebra numpy method [linalg](https://docs.scipy.org/doc/numpy/reference/routines.linalg.html). Example \begin{equation} A = \left( \begin{array}{ccc} 1 & 3 & 3 \\\\ 1 & 4 & 3 \\\\ 1 & 3 & 4 \end{array}\right) \end{equation} inverse matrix A_inv \begin{equation} A^{-1} = \left( \begin{array}{ccc} 7 & -3 & -3 \\\\ -1 & 1 & 0 \\\\ -1 & 0 & 1 \end{array}\right) \end{equation} >>> import

How to invert the elements of a boolean array in python ?

Examples of how to invert the elements of a boolean array in python using the numpy function [invert()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.invert.html) >>> import numpy as np >>> a = np.array((True,True,False,True,False)) >>> b = np.invert(a) >>> b array([False, False, True, False, True], dtype=bool) Another example >>> import numpy as np >>> a = np.array((1,1,0,1,0), dtype=bool) >>> b = np.invert(a) >>> b array(

Comment inverser les éléments d'une matrice booléenne avec numpy de python ?

Exemples de comment inverser les éléments d'une matrice booléenne en utilisant la fonction numpy [invert()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.invert.html) >>> import numpy as np >>> a = np.array((True,True,False,True,False)) >>> b = np.invert(a) >>> b array([False, False, True, False, True], dtype=bool) Autre exemple: >>> import numpy as np >>> a = np.array((1,1,0,1,0), dtype=bool) >>> b = np.invert(a) >>> b arra

How to overlay / superimpose two images using python and pillow ?

Examples of how to overlay / superimpose two images using python and pillow [TOC] ### Overlay two images of same size To overlay two images in python, a solution is to use the pillow function [paste()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.paste), example: from PIL import Image import numpy as np img = Image.open("data_mask_1354_2030.png") background = Image.open("background_1354_2030.png") background.paste(img, (0, 0), img) ba

Comment superposer deux images avec le module python pillow ?

Exemples de comment superposer deux images avec le module python pillow: [TOC] ### Superposer deux images de même taille Pour superposer deux images de même taille on peut utiliser la fonction pillow [paste()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.paste), exemple: from PIL import Image import numpy as np img = Image.open("data_mask_1354_2030.png") background = Image.open("background_1354_2030.png") background.paste(img, (0, 0), i

How to mask an array using another array in python ?

Examples of how to mask/select elements of an array using another array in python [TOC] ### Mask an array from another array To mask an array, there are several approaches with numpy (see the module called [ma](https://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html)). A solution for example is to use [numpy.ma.masked_where](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ma.masked_where.html) to mask the elements of the array x if the elements of the ar

How to create a sitemap for a website based on Django ?

An example of how to create a sitemap for a website based on [Django](https://docs.djangoproject.com/en/2.2/ref/contrib/sitemaps/) [TOC] Let's assume a Django website with an application called "notebooks" with the following files: database.db manage.py myproject / settings.py urls.py notebooks / views.py models.py urls.py with models.py class Article(models.Model): full_path = models.TextField() date_created = models.DateTimeField(default=datetime

Comment créer un sitemap avec le web-framework Django ?

Exemple de comment créer un sitemap pour un site web basé sur [Django](https://docs.djangoproject.com/en/2.2/ref/contrib/sitemaps/) (version utilisée 1.11.10): [TOC] On va ici supposer un site avec une application intitulée "notebooks" avec l'organisation suivante: database.db manage.py myproject / settings.py urls.py notebooks / views.py models.py urls.py avec models.py class Article(models.Model): full_path = models.TextField() date_created = mod

Comment masquer les valeurs d'une matrice depuis une autre matrice sous python ?

Exemples de comment sélectionner ou masquer les valeurs d'une matrice d'après les valeurs d'une autre matrice sous python: [TOC] ### Masquer une matrice à partir d'une autre matrice Pour masquer une matrice à partir d'une autre matrice, il existe plusieurs possibilités avec numpy (voir le module intitulé [ma](https://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html)). On peut par exemple utiliser [numpy.ma.masked_where](https://docs.scipy.org/doc/numpy-1.13.0/reference/genera

How to save a dictionary in a json file with python ?

Examples of how to save a dictionary in a json (JavaScript Object Notation) file with python [TOC] See also: [pickle — Python object serialization](https://docs.python.org/3/library/pickle.html#module-pickle) and [marshal — Internal Python object serialization](https://docs.python.org/3/library/marshal.html#module-marshal) ### Save a python dictionary in a json file To save a dictionary in python to a json file, a solution is to use the json function [dump()](https://docs.python.org/

Comment sauvegarder un dictionnaire dans un fichier json avec python ?

Exemples de comment sauvegarder un dictionnaire dans un fichier json (JavaScript Object Notation) avec python: [TOC] Voir aussi: [pickle — Python object serialization](https://docs.python.org/3/library/pickle.html#module-pickle) et [marshal — Internal Python object serialization](https://docs.python.org/3/library/marshal.html#module-marshal) ### Sauvegarder un dictionnaire dans un fichier json Pour sauvegarder un dictionnaire avec python dans un fichier json, on peut utiliser la fonc

How to plot points in front of a line in matplotlib ?

By default in matplotlib, if a line and a scatter plot are plotted in a same figure, the points are placed behind the line, illustration (no matter if the scatter() is called before plot()): [image:scatter-points-order-01 size:50 caption:How to plot points in front of a line in matplotlib ?] import matplotlib.pyplot as plt x = [1,2,3,4,5,6,7,8,9] y = [1,2,3,4,5,6,7,8,9] plt.plot([0,10],[0,10]) plt.scatter(x,y,s=300,color='red') plt.title("Tracer un nuage de points devant u

How to add a legend to a scatter plot in matplotlib ?

Examples of how to add a legend to a scatter plot in matplotlib: [TOC] ### Add a simple legend to a scatter plot Using the pyplot function [legend()](http://stackoverflow.com/questions/17411940/matplotlib-scatter-plot-legend): [image:ScatterPlot-09 size:50 caption:How to add a legend for a scatter plot in matplotlib ?] import matplotlib.pyplot as plt x = [1,2,3,4] y = [4,1,3,6] plt.scatter(x, y, c='coral', label='Class 1') x = [5,6,7,8] y = [1,3,5,2] plt.scatt

How to increase the size of scatter points in matplotlib ?

Examples of how to increase the size of scatter points in matplotlib: [TOC] ### Increase the size of all points To increase the size of scatter points, a solution is to use the option "s" from the function [scatter()](http://matplotlib.org/api/pyplot_api.html#module-matplotlib.pyplot), example [image:ScatterPlot-07 size:50 caption:How to increase the size of scatter points in matplotlib ?] import matplotlib.pyplot as plt x = [1,2,3,4,5,6,7,8] y = [4,1,3,6,1,3,5,2] plt.s

How to create a scatter plot with several colors in matplotlib ?

Examples of how to create a scatter plot with several colors in matplotlib: [TOC] ### Combining two scatter plots with different colors To change the color of a scatter point in matplotlib, there is the option "c" in the function [scatter](http://matplotlib.org/api/pyplot_api.html#module-matplotlib.pyplot). First simple example that combine two scatter plots with different colors: [image:ScatterPlot-05 size:50 caption:How to create a scatter plot with several colors in matplotlib ? ]

How to add a number to each element of a list in python ?

Examples of how to add a number to each element of a list in python: [TOC] ### Add a number to a list of integers Add +10 to each integer of a list using a "list comprehension": >>> l2 = [i+10 for i in l] >>> l2 [13, 11, 15, 18, 14] Increment a list of integer numbers: >>> l = [3,1,5,8,4] >>> l2 = [i+1 for i in l] >>> l2 [4, 2, 6, 9, 5] ### Add a number to a list of reals Increment a list of real numbers: >>> import numpy as np

How to create a simple scatter plot using matplotlib ?

Simples examples of how to create a simple scatter plot using matplotlib [TOC] ### Scatter plot with matplotlib To plot a scatter plot with matplotlib, ta solution is to use the method [scatter](http://matplotlib.org/api/pyplot_api.html#module-matplotlib.pyplot) from the class pyplot, example: [image:418 size:50 caption:How to create a simple scatter plot using matplotlib ? ] import matplotlib.pyplot as plt x = [1,2,3,4,5,6,7,8] y = [4,1,3,6,1,3,5,2] plt.scatter(x,y)

How to add a base64 encoded image in a html canvas using javascript ?

Example of how to add a base64 encoded image in a html canvas using javascript. We want to include the following image (the base64 encoded image can be found [here](/Files/base64/) in a html canvas: [image:550 size:25 caption:How to add a base64 encoded image in a html canvas using javascript ?] Source code <!DOCTYPE html> <html lang="en"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> </head> <body> <canvas id="t

Comment afficher dans un canvas html une image en base64 avec javascript ?

Exemple de comment afficher dans un canvas html une image en base64 avec javascript. On dispose de l'image suivante dont le code en base 64 est disponible [ici](/Files/base64/): [image:550 size:25 caption:Comment afficher dans un canvas html une image en base64 avec javascript ? ] Page html pour afficher l'image ci-dessus: <!DOCTYPE html> <html lang="en"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> </head> <body>

How to reverse the order of list elements in python ?

Examples of how to reverse a list in python: [TOC] ### Inverse a list with reverse() To modify a list by reversing the order of the elements, a solution is to use the function [reverse()](https://docs.python.org/2/tutorial/datastructures.html), example >>> l = ['a','b','c','d','e','f'] >>> l.reverse() >>> l ['f', 'e', 'd', 'c', 'b', 'a'] ### Inverse a list with reversed() To iterate through a list in the reversed order, a solution is to use the function [re

How to test locally the microphone in chrome using javascript ?

An example of how to test locally the microphone in chrome using javascript: [TOC] The primary goal here was to develop and test locally the microphone with chrome using a javascript application for speech recognition. The problem was that it is not possible to use the microphone locally for security reasons since an https is required to use it. Here a solution is presented to test locally the microphone in chrome using javascript and python: ### Install openssl and create a self-signed

How to copy an array (matrix) in python ?

Examples of how to copy an array in python: [TOC] ### Copy an array with the numpy copy() function To copy an array in python, a simple solution is to use the numpy function called [copy()](http://docs.scipy.org/doc/numpy/reference/generated/numpy.copy.html), example: >>> import numpy as np >>> x = np.array([1, 2, 3, 4]) >>> y = np.copy(x) >>> y[1] = 7 >>> y array([1, 7, 3, 4]) >>> x array([1, 2, 3, 4]) This function will work for mos