<?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: Convert blob/image to Python Image in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/convert-blob-image-to-python-image/m-p/1467083#M70587</link>
    <description>&lt;P&gt;I just noticed the &lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/export-attachments.htm" target="_self"&gt;ExportAttachments()&lt;/A&gt; function in 3.3. Maybe you could try that too if you're able to upgrade Pro.&lt;/P&gt;</description>
    <pubDate>Tue, 14 May 2024 18:21:28 GMT</pubDate>
    <dc:creator>BlakeTerhune</dc:creator>
    <dc:date>2024-05-14T18:21:28Z</dc:date>
    <item>
      <title>Convert blob/image to Python Image</title>
      <link>https://community.esri.com/t5/python-questions/convert-blob-image-to-python-image/m-p/1465843#M70584</link>
      <description>&lt;P&gt;Hi Community.&lt;/P&gt;&lt;P&gt;I´m currently working on a script that is able to send an email thru (arc)PY.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I´m using a searhcursor to find the relavant data including blob-data from the _ATTACH table.&lt;/P&gt;&lt;P&gt;But I´m missing a way to convert the blob data to an image (via the PIL framework)&lt;/P&gt;&lt;P&gt;I´ve tried something like this:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;with&lt;/SPAN&gt; &lt;SPAN&gt;arcpy&lt;/SPAN&gt;&lt;SPAN&gt;.da.SearchCursor(&lt;/SPAN&gt;&lt;SPAN&gt;_fcAttach&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;SPAN&gt;fieldsIn&lt;/SPAN&gt;&lt;SPAN&gt;) &lt;/SPAN&gt;&lt;SPAN&gt;as&lt;/SPAN&gt; &lt;SPAN&gt;imageCursor&lt;/SPAN&gt;&lt;SPAN&gt;:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV class="lia-indent-padding-left-30px"&gt;&lt;SPAN&gt;for&lt;/SPAN&gt; &lt;SPAN&gt;row&lt;/SPAN&gt; &lt;SPAN&gt;in&lt;/SPAN&gt; &lt;SPAN&gt;imageCursor&lt;/SPAN&gt;&lt;SPAN&gt;:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV class="lia-indent-padding-left-60px"&gt;&lt;SPAN&gt;tmpImg&lt;/SPAN&gt; &lt;SPAN&gt;=&lt;/SPAN&gt; &lt;SPAN&gt;row&lt;/SPAN&gt;&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;SPAN&gt;] #the 'DATA'-field&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;binary_data&lt;/SPAN&gt; &lt;SPAN&gt;=&lt;/SPAN&gt; &lt;SPAN&gt;base64&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;b64decode&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;tmpImg&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;ioData&lt;/SPAN&gt; &lt;SPAN&gt;=&lt;/SPAN&gt; &lt;SPAN&gt;io&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;BytesIO&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;binary_data&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;image&lt;/SPAN&gt; &lt;SPAN&gt;=&lt;/SPAN&gt; &lt;SPAN&gt;Image&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;open&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;ioData&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;But I get an error like:&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;PIL.UnidentifiedImageError: cannot identify image file &amp;lt;_io.BytesIO object at 0x0000011D99E7BE50&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Any suggestions or ideas?&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Kåre&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2024 14:04:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/convert-blob-image-to-python-image/m-p/1465843#M70584</guid>
      <dc:creator>KåreMølgaardRasmussen</dc:creator>
      <dc:date>2024-05-14T14:04:56Z</dc:date>
    </item>
    <item>
      <title>Re: Convert blob/image to Python Image</title>
      <link>https://community.esri.com/t5/python-questions/convert-blob-image-to-python-image/m-p/1465853#M70585</link>
      <description>&lt;P&gt;Sorry, I have not worked with images much, but you could try &lt;A href="https://wiki.python.org/moin/ImageMagick" target="_self"&gt;ImageMagick&lt;/A&gt; or &lt;A href="https://docs.wand-py.org/en/0.6.13/" target="_self"&gt;wand&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2024 14:17:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/convert-blob-image-to-python-image/m-p/1465853#M70585</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2024-05-14T14:17:00Z</dc:date>
    </item>
    <item>
      <title>Re: Convert blob/image to Python Image</title>
      <link>https://community.esri.com/t5/python-questions/convert-blob-image-to-python-image/m-p/1466990#M70586</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/675432"&gt;@KåreMølgaardRasmussen&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;The below sample will export the attachments to a directory:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os, arcpy

# Variables
tbl = r"C:\Temp\Python\Test.gdb\Graffiti__ATTACH"       # Path to attachment table
fldBLOB = 'DATA'                                        # Field name of Blob data type field in attachment table
fldAttName = 'ATT_NAME'                                 # Field name in attachment table that contains attachment name
outFolder = r"C:\Temp\Python\Attachments"               # Output folder to export attachments to

with arcpy.da.SearchCursor(tbl,[fldBLOB,fldAttName]) as cursor:
   for row in cursor:
      binaryRep = row[0]
      fileName = row[1]
      # save to disk
      open(outFolder + os.sep + fileName, 'wb').write(binaryRep.tobytes())

print('Finished')&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 14 May 2024 18:07:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/convert-blob-image-to-python-image/m-p/1466990#M70586</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2024-05-14T18:07:14Z</dc:date>
    </item>
    <item>
      <title>Re: Convert blob/image to Python Image</title>
      <link>https://community.esri.com/t5/python-questions/convert-blob-image-to-python-image/m-p/1467083#M70587</link>
      <description>&lt;P&gt;I just noticed the &lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/export-attachments.htm" target="_self"&gt;ExportAttachments()&lt;/A&gt; function in 3.3. Maybe you could try that too if you're able to upgrade Pro.&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2024 18:21:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/convert-blob-image-to-python-image/m-p/1467083#M70587</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2024-05-14T18:21:28Z</dc:date>
    </item>
    <item>
      <title>Re: Convert blob/image to Python Image</title>
      <link>https://community.esri.com/t5/python-questions/convert-blob-image-to-python-image/m-p/1474174#M70599</link>
      <description>&lt;P&gt;Last time I checked, the Data Access Cursor types return BLOB columns as memoryview objects. The docs for PIL's &lt;A href="https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.open" target="_self"&gt;Image.open&lt;/A&gt; function say it can open any file-like object that holds binary data. If you're lucky you can just shove the memoryview in there, if not you can do something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from io import BytesIO
from PIL import Image

with arcpy.da.SearchCursor(data, fields) as cur:
    for row in cur:
        data = row[index]
        photo = Image.open(BytesIO(data))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think the issue with the code above is you're decoding the binary data to base64 which is tripping up PIL, assuming your images were added to an attachment table through the standard ArcGIS methods the data is stored byte for byte without any special encoding.&lt;/P&gt;</description>
      <pubDate>Thu, 16 May 2024 21:17:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/convert-blob-image-to-python-image/m-p/1474174#M70599</guid>
      <dc:creator>DavidSolari</dc:creator>
      <dc:date>2024-05-16T21:17:55Z</dc:date>
    </item>
  </channel>
</rss>

