Images.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Images &#8212; Image Features Extraction 1.0.0 documentation</title>
  7. <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
  8. <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: '../',
  12. VERSION: '1.0.0',
  13. COLLAPSE_INDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: true,
  16. SOURCELINK_SUFFIX: '.txt'
  17. };
  18. </script>
  19. <script type="text/javascript" src="../_static/jquery.js"></script>
  20. <script type="text/javascript" src="../_static/underscore.js"></script>
  21. <script type="text/javascript" src="../_static/doctools.js"></script>
  22. <link rel="index" title="Index" href="../genindex.html" />
  23. <link rel="search" title="Search" href="../search.html" />
  24. <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
  25. <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
  26. </head>
  27. <body>
  28. <div class="document">
  29. <div class="documentwrapper">
  30. <div class="bodywrapper">
  31. <div class="body" role="main">
  32. <h1>Source code for Images</h1><div class="highlight"><pre>
  33. <span></span><span class="ch">#!/usr/bin/env python3</span>
  34. <span class="kn">import</span> <span class="nn">os</span>
  35. <span class="kn">from</span> <span class="nn">image_features_extraction</span> <span class="k">import</span> <span class="n">Image</span>
  36. <span class="kn">from</span> <span class="nn">image_features_extraction</span> <span class="k">import</span> <span class="n">my_iterator</span>
  37. <span class="kn">from</span> <span class="nn">image_features_extraction</span> <span class="k">import</span> <span class="n">MyException</span>
  38. <div class="viewcode-block" id="Images"><a class="viewcode-back" href="../code.html#Images.Images">[docs]</a><span class="k">class</span> <span class="nc">Images</span><span class="p">(</span><span class="n">my_iterator</span><span class="o">.</span><span class="n">my_iterator</span><span class="p">):</span>
  39. <span class="sd">&quot;&quot;&quot;</span>
  40. <span class="sd"> This class loads a collection of images for extraction of features </span>
  41. <span class="sd"> :param folder_name: folder containing images</span>
  42. <span class="sd"> :type folder_name: string</span>
  43. <span class="sd"> :param image_file_ext: images file extensions (default=[&#39;tif&#39;,&#39;tiff&#39;])</span>
  44. <span class="sd"> :type folder_name: List of strings</span>
  45. <span class="sd"> :returns: an instance of the object Images</span>
  46. <span class="sd"> :rtype: object</span>
  47. <span class="sd"> :example:</span>
  48. <span class="sd"> &gt;&gt;&gt; import image_features_extraction as fe</span>
  49. <span class="sd"> &gt;&gt;&gt; imgs = fe.Images(&#39;my_folder&#39;, image_file_ext=[&#39;tif&#39;,&#39;tiff&#39;,&#39;jpeg&#39;])</span>
  50. <span class="sd"> &gt;&gt;&gt; num_images = imgs.count()</span>
  51. <span class="sd"> &quot;&quot;&quot;</span>
  52. <span class="c1"># to implement the class as a collection this object inherits the abstract class my_iterator</span>
  53. <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">folder_name</span><span class="p">,</span> <span class="n">image_file_ext</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;tif&#39;</span><span class="p">,</span> <span class="s1">&#39;tiff&#39;</span><span class="p">]):</span>
  54. <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
  55. <span class="c1">#self.__iterator_init__() # initializes my_iterator</span>
  56. <span class="bp">self</span><span class="o">.</span><span class="n">__folder_name</span> <span class="o">=</span> <span class="n">folder_name</span>
  57. <span class="bp">self</span><span class="o">.</span><span class="n">__image_file_ext</span> <span class="o">=</span> <span class="n">image_file_ext</span>
  58. <span class="bp">self</span><span class="o">.</span><span class="n">__dicfiles</span> <span class="o">=</span> <span class="p">[]</span>
  59. <span class="k">try</span><span class="p">:</span>
  60. <span class="c1"># load the files image into a list</span>
  61. <span class="bp">self</span><span class="o">.</span><span class="n">__load</span><span class="p">()</span>
  62. <span class="k">except</span> <span class="n">MyException</span><span class="o">.</span><span class="n">MyException</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  63. <span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">)</span>
  64. <span class="k">def</span> <span class="nf">__load</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  65. <span class="sd">&quot;&quot;&quot;</span>
  66. <span class="sd"> load the files image into a list</span>
  67. <span class="sd"> &quot;&quot;&quot;</span>
  68. <span class="c1"># check that the folder exists</span>
  69. <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__folder_name</span><span class="p">)</span> <span class="o">==</span> <span class="kc">False</span><span class="p">:</span>
  70. <span class="k">raise</span> <span class="n">MyException</span><span class="o">.</span><span class="n">MyException</span><span class="p">(</span><span class="s2">&quot;Error: folder name does not exist&quot;</span><span class="p">)</span>
  71. <span class="c1"># store the file names</span>
  72. <span class="bp">self</span><span class="o">.</span><span class="n">__dicfiles</span> <span class="o">=</span> <span class="p">[]</span>
  73. <span class="n">files</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__folder_name</span><span class="p">)</span>
  74. <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
  75. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__is_imagefile</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
  76. <span class="bp">self</span><span class="o">.</span><span class="n">__dicfiles</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
  77. <span class="bp">self</span><span class="o">.</span><span class="n">count_update</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__dicfiles</span><span class="p">))</span>
  78. <span class="k">def</span> <span class="nf">__is_imagefile</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">file_name</span><span class="p">):</span>
  79. <span class="sd">&quot;&quot;&quot;</span>
  80. <span class="sd"> checks that the file is an image</span>
  81. <span class="sd"> &quot;&quot;&quot;</span>
  82. <span class="c1"># check the extension of the file</span>
  83. <span class="n">ext0</span> <span class="o">=</span> <span class="n">file_name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
  84. <span class="k">for</span> <span class="n">ext1</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__image_file_ext</span><span class="p">:</span>
  85. <span class="k">if</span> <span class="n">ext1</span> <span class="o">==</span> <span class="n">ext0</span><span class="p">:</span>
  86. <span class="k">return</span> <span class="kc">True</span>
  87. <span class="k">return</span> <span class="kc">False</span>
  88. <div class="viewcode-block" id="Images.item"><a class="viewcode-back" href="../code.html#Images.Images.item">[docs]</a> <span class="k">def</span> <span class="nf">item</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
  89. <span class="sd">&quot;&quot;&quot;</span>
  90. <span class="sd"> returns the i-th image</span>
  91. <span class="sd"> :param i: the i-th image</span>
  92. <span class="sd"> :type i: int</span>
  93. <span class="sd"> :returns: :class:`Image`</span>
  94. <span class="sd"> :rtype: object</span>
  95. <span class="sd"> :example:</span>
  96. <span class="sd"> &gt;&gt;&gt; import image_features_extraction as fe</span>
  97. <span class="sd"> &gt;&gt;&gt; imgs = fe.Images(folder_name)</span>
  98. <span class="sd"> &gt;&gt;&gt; img = imgs.item(1)</span>
  99. <span class="sd"> &quot;&quot;&quot;</span>
  100. <span class="k">try</span><span class="p">:</span>
  101. <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="p">():</span>
  102. <span class="k">raise</span> <span class="n">MyException</span><span class="o">.</span><span class="n">MyException</span><span class="p">(</span><span class="s2">&quot;error: index out of bound&quot;</span><span class="p">)</span>
  103. <span class="k">return</span> <span class="n">Image</span><span class="o">.</span><span class="n">Image</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__folder_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__dicfiles</span><span class="p">[</span><span class="n">i</span><span class="p">]))</span>
  104. <span class="k">except</span> <span class="n">MyException</span><span class="o">.</span><span class="n">MyException</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  105. <span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">)</span></div></div>
  106. </pre></div>
  107. </div>
  108. </div>
  109. </div>
  110. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  111. <div class="sphinxsidebarwrapper">
  112. <h1 class="logo"><a href="../index.html">Image Features Extraction</a></h1>
  113. <h3>Navigation</h3>
  114. <ul>
  115. <li class="toctree-l1"><a class="reference internal" href="../tutorial.html">Tutorial</a></li>
  116. <li class="toctree-l1"><a class="reference internal" href="../code.html">Classes/Modules</a></li>
  117. </ul>
  118. <div class="relations">
  119. <h3>Related Topics</h3>
  120. <ul>
  121. <li><a href="../index.html">Documentation overview</a><ul>
  122. <li><a href="index.html">Module code</a><ul>
  123. </ul></li>
  124. </ul></li>
  125. </ul>
  126. </div>
  127. <div id="searchbox" style="display: none" role="search">
  128. <h3>Quick search</h3>
  129. <form class="search" action="../search.html" method="get">
  130. <div><input type="text" name="q" /></div>
  131. <div><input type="submit" value="Go" /></div>
  132. <input type="hidden" name="check_keywords" value="yes" />
  133. <input type="hidden" name="area" value="default" />
  134. </form>
  135. </div>
  136. <script type="text/javascript">$('#searchbox').show(0);</script>
  137. </div>
  138. </div>
  139. <div class="clearer"></div>
  140. </div>
  141. <div class="footer">
  142. &copy;2017, Remigio Picone.
  143. |
  144. Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.3</a>
  145. &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
  146. </div>
  147. </body>
  148. </html>