2 # -*- coding: iso-8859-1 -*-
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)
11 from PIL import ImageOps
16 #img=Image.open("images/avatar.png")
17 img=Image.open("images/tests.pgm")
19 # Get a grayscale version
20 imgbw=ImageOps.grayscale(img)
22 # Save the image (optionnal)
23 imgbw.save(fp="testsbw.pgm")
26 imgdata=imgbw.getdata()
27 width,height=imgbw.size
31 # Convert the data in a matrix using numpy
32 tab=numpy.array(imgdata,dtype='float64')
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.
43 import MEDCoupling as MC
44 import MEDLoader as ML
45 def createMesh(meshname, sizeX, sizeY):
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
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.
57 # The mesh is created using MEDCoupling
58 cmesh=MC.MEDCouplingCMesh.New();
59 cmesh.setName(meshname)
61 # We use an arbitrary step between cells (the value does not matter)
64 arrX = [float(i * stepX) for i in range(nbNodesX)]
65 coordsX=MC.DataArrayDouble.New()
66 coordsX.setValues(arrX,nbNodesX,1)
68 # For the Y dimension, we have to reverse the coordinates (the
69 # first pixel is at y=height and not at y=0).
73 arrY=[float(lengthY - i * stepY) for i in range(nbNodesY)]
74 coordsY=MC.DataArrayDouble.New()
75 coordsY.setValues(arrY,nbNodesY,1)
77 cmesh.setCoords(coordsX,coordsY)
78 print "Imagem mesh dimension: %d"%cmesh.getSpaceDimension()
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")
89 def createField(fieldname, mesh, image):
91 Creating a scalar field on the mesh using image data
93 # Create the field using MEDCoupling
94 field = MC.MEDCouplingFieldDouble.New(MC.ON_CELLS,MC.ONE_TIME);
95 field.setName(fieldname);
97 # OPTIONAL: We set an arbitrary time step for test purpose
98 field.setIteration(0);
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
107 dataArray.setValues(imagedata,nbCells,nbComponents)
108 field.setArray(dataArray);
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.
119 width,height=imgbw.size
120 mesh=createMesh("mesh",width,height)
121 field=createField("field",mesh,imgbw)
123 createFromScratch=True
124 ML.MEDLoader.WriteField("image.med",field,createFromScratch)
127 # ===================================================================
129 if __name__ == "__main__":