Morph-M Python - Creation and Deletion » History » Version 4
Serge Koudoro, 10/26/2009 01:59 PM
1 | 2 | Serge Koudoro | h1. *Morph-M Python - Creation and Deletion* |
---|---|---|---|
2 | 1 | Serge Koudoro | |
3 | h3{color:#8B0000}. +Creation...+ |
||
4 | |||
5 | Images can either be created by type and size. |
||
6 | |||
7 | <pre><code class="ruby"> |
||
8 | im=createImage(dataCategory.dtScalar,scalarDataType.sdtUINT8) |
||
9 | im.setSize(256,256) |
||
10 | im.allocateImage() |
||
11 | </code></pre> |
||
12 | |||
13 | which type and size are this following value: |
||
14 | |||
15 | * *Type*: _sdtINT8, sdtINT16, sdtINT32, sdtINT64, sdtUINT8, sdtUINT16, sdtUINT32, sdtUINT64, sdtFloat, sdtDouble, sdtBIT, sdtOffset, sdtLabel, sdtSTR, sdtObject, sdtCompound, sdtList, sdtMap, sdtPair_. |
||
16 | * *size*: _dtScalar, dtAngular, dtComplex, dtPixel3, dtPixel4, dtOffsetList, dtPointer, dtArray, dtImage, dtNeighborhood, dtVariant, dtCVariant_. |
||
17 | |||
18 | h3{color:#8B0000}. +Load from file...+ |
||
19 | |||
20 | Images can be loaded from a file, and can be write to a file |
||
21 | |||
22 | <pre><code class="ruby"> |
||
23 | im2=pngFileRead("foo.png") |
||
24 | im2=bmpFileRead("foo.bmp") |
||
25 | im2=csvFileRead("foo.csv") |
||
26 | im2=jpegFileRead("foo.jpg") |
||
27 | pngFileWrite(im2,"foo.png") |
||
28 | bmpFileWrite(im2,"foo.bmp") |
||
29 | csvFileWrite(im2,"foo.csv") |
||
30 | jpegFileWrite(im2,"foo.jpg") |
||
31 | #generic function |
||
32 | im2=fileRead("foo.png") |
||
33 | fileWrite(im2, "foo.jpg") |
||
34 | </code></pre> |
||
35 | |||
36 | PNG is the best supported format. |
||
37 | |||
38 | 4 | Serge Koudoro | h3{color:#8B0000}. +Load from List...+ |
39 | |||
40 | im = createImage(dataCategory.dtScalar,scalarDataType.sdtUINT32 ) |
||
41 | im.setSize(3,3) |
||
42 | im.allocateImage() |
||
43 | |||
44 | L=[ 1,2,3,4,5,6,7,8,9] |
||
45 | im.fromList(L) |
||
46 | |||
47 | 1 | Serge Koudoro | h3{color:#8B0000}. +Features...+ |
48 | |||
49 | Images are Python objects and as such are reference-counted and automatically destroyed when leaving the scope. This code does not leak memory: |
||
50 | |||
51 | <pre><code class="ruby"> |
||
52 | while 1: |
||
53 | 3 | Serge Koudoro | im=pngFileRead("hop.png") |
54 | 1 | Serge Koudoro | |
55 | </code></pre> |
||
56 | |||
57 | Images are passed and copied by reference: |
||
58 | |||
59 | <pre><code class="ruby"> |
||
60 | im2=im1 |
||
61 | ImSetConstant(im1,255) |
||
62 | for pixel in im2: |
||
63 | 3 | Serge Koudoro | print pixel |
64 | 1 | Serge Koudoro | </code></pre> |
65 | |||
66 | The second image is also put to 255. A new image can be created by using getSame and ImCopy: |
||
67 | |||
68 | <pre><code class="ruby"> |
||
69 | im2=im1.getSame() #or im2=ImCreateSame(im1) |
||
70 | im3=im1.ImCreateSame(im1, "UINT16") #changing image type |
||
71 | ImCopy(im1,im2) |
||
72 | ImSetConstant(im1,255) |
||
73 | for pixel in im2: |
||
74 | 3 | Serge Koudoro | print pixel |
75 | 1 | Serge Koudoro | </code></pre> |
76 | |||
77 | The second image will have kept its values even while the first is uniformally set to 255. |