Images.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 is used as collection of images which will be loaded from a given folder name</span>
  41. <span class="sd"> &quot;&quot;&quot;</span>
  42. <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>
  43. <span class="bp">self</span><span class="o">.</span><span class="n">__iterator_init__</span><span class="p">()</span> <span class="c1"># initialize my iterator</span>
  44. <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>
  45. <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>
  46. <span class="bp">self</span><span class="o">.</span><span class="n">__dicfiles</span> <span class="o">=</span> <span class="p">[]</span>
  47. <span class="k">try</span><span class="p">:</span>
  48. <span class="bp">self</span><span class="o">.</span><span class="n">__load</span><span class="p">()</span>
  49. <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>
  50. <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>
  51. <span class="k">def</span> <span class="nf">__load</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  52. <span class="c1"># check that the folder exists</span>
  53. <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>
  54. <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>
  55. <span class="c1"># store the file names</span>
  56. <span class="bp">self</span><span class="o">.</span><span class="n">__dicfiles</span> <span class="o">=</span> <span class="p">[]</span>
  57. <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>
  58. <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
  59. <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>
  60. <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>
  61. <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>
  62. <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>
  63. <span class="c1"># check the extension of the file</span>
  64. <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>
  65. <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>
  66. <span class="k">if</span> <span class="n">ext1</span> <span class="o">==</span> <span class="n">ext0</span><span class="p">:</span>
  67. <span class="k">return</span> <span class="kc">True</span>
  68. <span class="k">return</span> <span class="kc">False</span>
  69. <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>
  70. <span class="sd">&quot;&quot;&quot;</span>
  71. <span class="sd"> image item</span>
  72. <span class="sd"> &quot;&quot;&quot;</span>
  73. <span class="k">try</span><span class="p">:</span>
  74. <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>
  75. <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>
  76. <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>
  77. <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>
  78. <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>
  79. </pre></div>
  80. </div>
  81. </div>
  82. </div>
  83. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  84. <div class="sphinxsidebarwrapper">
  85. <h1 class="logo"><a href="../index.html">image features extraction</a></h1>
  86. <h3>Navigation</h3>
  87. <p class="caption"><span class="caption-text">Contents:</span></p>
  88. <ul>
  89. <li class="toctree-l1"><a class="reference internal" href="../intro.html">Introduction</a></li>
  90. <li class="toctree-l1"><a class="reference internal" href="../tutorial.html">Tutorial</a></li>
  91. <li class="toctree-l1"><a class="reference internal" href="../code.html">Classes/Modules</a></li>
  92. </ul>
  93. <div class="relations">
  94. <h3>Related Topics</h3>
  95. <ul>
  96. <li><a href="../index.html">Documentation overview</a><ul>
  97. <li><a href="index.html">Module code</a><ul>
  98. </ul></li>
  99. </ul></li>
  100. </ul>
  101. </div>
  102. <div id="searchbox" style="display: none" role="search">
  103. <h3>Quick search</h3>
  104. <form class="search" action="../search.html" method="get">
  105. <div><input type="text" name="q" /></div>
  106. <div><input type="submit" value="Go" /></div>
  107. <input type="hidden" name="check_keywords" value="yes" />
  108. <input type="hidden" name="area" value="default" />
  109. </form>
  110. </div>
  111. <script type="text/javascript">$('#searchbox').show(0);</script>
  112. </div>
  113. </div>
  114. <div class="clearer"></div>
  115. </div>
  116. <div class="footer">
  117. &copy;2017, Remigio Picone.
  118. |
  119. Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.3</a>
  120. &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
  121. </div>
  122. </body>
  123. </html>