Salome HOME
Merge remote-tracking branch 'origin/V8_3_BR' into gdd/python3_dev
[modules/med.git] / doc / tut / medcoupling / testmed_lena.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2011-2016  CEA/DEN, EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 # This use case illustrates the usage of PIL (Python Imaging Library)
23 # combined with the MEDCoupling and MEDLoader modules to save an image
24 # as a field in a med file.
25 # (gboulant - 27/06/2011)
26
27 import MEDCoupling as MC
28 import MEDLoader as ML
29
30 #
31 # ===============================================================
32 # We first get data from the test image to render as a field
33 # ===============================================================
34 #
35 import scipy, numpy
36 # The data field array may be created from the lena image
37 #image = scipy.lena()
38 # We could either read a real image using the PIL python package.
39 from scipy.misc import pilutil
40 image = pilutil.imread("images/avatar.png",True)
41
42
43 #from PIL import Image
44 #im=Image.open("images/irm.png")
45 #im=Image.open("images/lena.png")
46 #image=pilutil.fromimage(im,True)
47 #image=numpy.asarray(im)
48 # print(image)
49
50 dim  = len(image.shape)
51 print("Image space dimension = %d"%dim)
52 sizeX = image.shape[1]
53 sizeY = image.shape[0]
54
55 # The sizes defined the number of pixel in a direction, then the
56 # number of cells to create in the mesh in that direction.
57
58 # We must reshape the matrix of pixel in a 1D vector that concatenates
59 # all the rows, and then convert this vector in a simple list of
60 # double as required by the MEDCoupling field specification.
61 import numpy
62 imageDataNArray       = image.reshape(1,sizeX*sizeY)[0]
63 print(imageDataNArray)
64
65 imageDataNArrayDouble = numpy.array(imageDataNArray, dtype='float64')
66 imageDataArrayDouble  = list(imageDataNArrayDouble)
67
68 #
69 # ===============================================================
70 # Creating a cartesian mesh with a grid of the size of the image
71 # ===============================================================
72 #
73
74 # >>>
75 # WARNING: remember the problem of tics and spaces. The data values
76 # are considered as values defined on cells. With size values in a
77 # direction, we have to create size+1 mesh nodes in that direction.
78 # <<<
79
80 # The mesh is created using MEDCoupling
81 cmesh=MC.MEDCouplingCMesh.New();
82 cmesh.setName("imagemesh")
83
84 # We use an arbitrary step between cells (the value does not matter)
85 stepX = 0.1
86 nbNodesX = sizeX+1
87 arrX = [float(i * stepX) for i in range(nbNodesX)]
88 coordsX=MC.DataArrayDouble.New()
89 coordsX.setValues(arrX,nbNodesX,1)
90
91 stepY = 0.1
92 nbNodesY = sizeY+1
93 arrY=[float(i * stepY) for i in range(nbNodesY)]
94 coordsY=MC.DataArrayDouble.New()
95 coordsY.setValues(arrY,nbNodesY,1)
96
97 cmesh.setCoords(coordsX,coordsY)
98 print("Imagem mesh dimension: %d"%cmesh.getSpaceDimension())
99
100 # WARN: In the current state of development of MEDLoader, only
101 # unstructured meshes are supported for writting function in med
102 # files. We just have to convert the cartesian mesh in an unstructured
103 # mesh before creating the field.
104 umesh=cmesh.buildUnstructured();
105 umesh.setName("imagemesh")
106
107 #
108 # ===============================================================
109 # Creating a scalar field on the mesh using image data
110 # ===============================================================
111 #
112
113 # Create the field using MEDCoupling
114 field = MC.MEDCouplingFieldDouble.New(MC.ON_CELLS,MC.ONE_TIME);
115 field.setName("imagefield");
116 field.setMesh(umesh);
117 # OPTIONAL: We set an arbitrary time step for test purpose
118 field.setIteration(3);
119 field.setOrder(0)
120
121 dataArray=MC.DataArrayDouble.New();
122 nbCells = sizeX*sizeY
123 nbComponents=1 # For a scalar field
124
125 # This example shows haw to initialize all cell with the same
126 # value. Just create an array of size nbCells
127 # dataArray.setValues(nbCells*[3.4],nbCells,nbComponents)
128
129 dataArray.setValues(imageDataArrayDouble,nbCells,nbComponents)
130 field.setArray(dataArray);
131
132 # The MEDLoader can be used to save all the stuff in a med file. You
133 # just have to specify the field and the MEDLoader will save the
134 # underlying mesh.
135 createFromScratch=True
136 ML.MEDLoader.WriteField("fieldimage.med",field,createFromScratch)