Salome HOME
Unwarningization under Win.
[tools/medcoupling.git] / src / MEDOP / tut / medcoupling / testpil.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 #
4 # This script illustrates how to create a matrix of pixels from data
5 # read in an image file. At the end, the cells of the matrix
6 # corresponds to the cells of a cartesian mesh that could hold a field
7 # whose value is the value of the pixel.
8 # (gboulant - 13/11/2011)
9
10 from PIL import Image
11 from PIL import ImageOps
12 import numpy
13
14 def image2matrix():
15     # Load the image
16     #img=Image.open("images/avatar.png")
17     img=Image.open("images/tests.pgm")
18     
19     # Get a grayscale version
20     imgbw=ImageOps.grayscale(img)
21     
22     # Save the image (optionnal)
23     imgbw.save(fp="testsbw.pgm")
24     
25     # Get the data
26     imgdata=imgbw.getdata()
27     width,height=imgbw.size
28     print list(imgdata)
29     print width,height
30
31     # Convert the data in a matrix using numpy
32     tab=numpy.array(imgdata,dtype='float64')
33     print list(tab)
34     print tab
35     nbRows=height
36     nbCols=width
37     matrix=numpy.reshape(tab,(nbRows,nbCols))
38     # Note that in the reshape function, the height (sizeY) of the image
39     # is specified first, because it corresponds to the number of rows.
40     print matrix
41     print list(matrix)
42
43 import MEDCoupling as MC
44 import MEDLoader as ML
45 def createMesh(meshname, sizeX, sizeY):
46     """
47     Creating a cartesian mesh with a grid of the size of the image.
48     sizeX and sizeY should be respectively the width and heigth of the
49     image.
50     """
51     # >>>
52     # WARNING: remember the problem of tics and spaces. The data values
53     # are considered as values defined on cells. With size values in a
54     # direction, we have to create size+1 mesh nodes in that direction.
55     # <<<
56     
57     # The mesh is created using MEDCoupling
58     cmesh=MC.MEDCouplingCMesh.New();
59     cmesh.setName(meshname)
60     
61     # We use an arbitrary step between cells (the value does not matter)
62     stepX = 0.1
63     nbNodesX = sizeX+1
64     arrX = [float(i * stepX) for i in range(nbNodesX)]
65     coordsX=MC.DataArrayDouble.New()
66     coordsX.setValues(arrX,nbNodesX,1)
67
68     # For the Y dimension, we have to reverse the coordinates (the
69     # first pixel is at y=height and not at y=0).
70     stepY = 0.1
71     nbNodesY = sizeY+1
72     lengthY = sizeY*stepY
73     arrY=[float(lengthY - i * stepY) for i in range(nbNodesY)]
74     coordsY=MC.DataArrayDouble.New()
75     coordsY.setValues(arrY,nbNodesY,1)
76     
77     cmesh.setCoords(coordsX,coordsY)
78     print "Imagem mesh dimension: %d"%cmesh.getSpaceDimension()
79     
80     # WARN: In the current state of development of MEDLoader, only
81     # unstructured meshes are supported for writting function in med
82     # files. We just have to convert the cartesian mesh in an unstructured
83     # mesh before creating the field.
84     umesh=cmesh.buildUnstructured();
85     umesh.setName("imagemesh")
86     
87     return umesh
88
89 def createField(fieldname, mesh, image):
90     """
91     Creating a scalar field on the mesh using image data
92     """    
93     # Create the field using MEDCoupling
94     field = MC.MEDCouplingFieldDouble.New(MC.ON_CELLS,MC.ONE_TIME);
95     field.setName(fieldname);
96     field.setMesh(mesh);
97     # OPTIONAL: We set an arbitrary time step for test purpose
98     field.setIteration(0);
99     field.setOrder(0)
100
101     imagedata=list(image.getdata())
102     width,height=image.size
103     nbCells = width*height
104     dataArray=MC.DataArrayDouble.New();
105     nbComponents=1 # For a scalar field
106     
107     dataArray.setValues(imagedata,nbCells,nbComponents)
108     field.setArray(dataArray);
109     
110     return field
111
112 def image2med():
113     img=Image.open("images/avatar.png")
114     #img=Image.open("images/irm.png")
115     imgbw=ImageOps.grayscale(img)
116     # We keep only the grayscale. Maybe, it could be usefull to get
117     # the RGB scales each on one component of the field.
118     
119     width,height=imgbw.size
120     mesh=createMesh("mesh",width,height)
121     field=createField("field",mesh,imgbw)
122     
123     createFromScratch=True
124     ML.MEDLoader.WriteField("image.med",field,createFromScratch)
125
126
127 # ===================================================================
128     
129 if __name__ == "__main__":
130     #image2matrix()
131     image2med()