<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Count of different pixels in raster in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/count-of-different-pixels-in-raster/m-p/428019#M33626</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;After running the Build Raster Attribute Table tool, the attribute table of the raster will contain a field called Value representing the pixel value, and a field called Count representing how many pixels of that Value.&amp;nbsp; You can then use the following code to write these value to an output text file:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;lstRasters = arcpy.ListRasters("*")

logfile = open(r"c:\temp\pixels.txt", "w")

for raster in lstRasters:
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(raster)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; logfile.write("Value of " + str(row.value) + " = " + str(row.count) + " pixels" + "\n")

logfile.close()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 19:15:56 GMT</pubDate>
    <dc:creator>JakeSkinner</dc:creator>
    <dc:date>2021-12-11T19:15:56Z</dc:date>
    <item>
      <title>Count of different pixels in raster</title>
      <link>https://community.esri.com/t5/python-questions/count-of-different-pixels-in-raster/m-p/428018#M33625</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm trying to write a script to count of number different value of pixels in raster. Rasters contain only 0 or 1 value and all are in "Mask" catalog (there are about 200). This script must write it to text file, first name then number of pixels. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Like this : Name of raster&amp;nbsp;&amp;nbsp; number of pixel 0, number of pixel 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I wrote the script below:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from arcpy import env&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;env.workspace = "E:\\Mask"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;outfile = open("E:\\RastStats.txt", 'w')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rastlist = arcpy.ListRasters("*", "")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for raster in rastlist:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateStatistics_management(raster, "1", "1", "")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.BuildRasterAttributeTable_management(raster, "Overwrite")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.rasterStatistics = 'STATISTICS 1 1 (0)'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; z= arcpy.env.rasterStatistics&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rastname = str(raster)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile.write(rastname + " ")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; textstring = str(z)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile.write(textstring + "\n")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;outfile.close() &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And it's not working &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This form doesn't work:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.env.rasterStatistics = 'STATISTICS 1 1 (0)'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;How I can fix it?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 May 2011 19:17:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/count-of-different-pixels-in-raster/m-p/428018#M33625</guid>
      <dc:creator>LukaszB_</dc:creator>
      <dc:date>2011-05-03T19:17:50Z</dc:date>
    </item>
    <item>
      <title>Re: Count of different pixels in raster</title>
      <link>https://community.esri.com/t5/python-questions/count-of-different-pixels-in-raster/m-p/428019#M33626</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;After running the Build Raster Attribute Table tool, the attribute table of the raster will contain a field called Value representing the pixel value, and a field called Count representing how many pixels of that Value.&amp;nbsp; You can then use the following code to write these value to an output text file:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;lstRasters = arcpy.ListRasters("*")

logfile = open(r"c:\temp\pixels.txt", "w")

for raster in lstRasters:
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor(raster)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; logfile.write("Value of " + str(row.value) + " = " + str(row.count) + " pixels" + "\n")

logfile.close()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:15:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/count-of-different-pixels-in-raster/m-p/428019#M33626</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T19:15:56Z</dc:date>
    </item>
    <item>
      <title>Re: Count of different pixels in raster</title>
      <link>https://community.esri.com/t5/python-questions/count-of-different-pixels-in-raster/m-p/428020#M33627</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It works perfect ! Great thanks! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 May 2011 20:38:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/count-of-different-pixels-in-raster/m-p/428020#M33627</guid>
      <dc:creator>LukaszB_</dc:creator>
      <dc:date>2011-05-03T20:38:28Z</dc:date>
    </item>
  </channel>
</rss>

