title: Is it possible to read a DICOM file from a BytesIO object?
body: <p>I am using python 3.6 on Windows to fetch a DICOM file from a database as a string of bytes. Now, I would like to use the <code>Pydicom</code> library to read the content of the file. The <code>dcmread()</code> method takes a file-like object as main argument, however the following piece of code raises an error when trying to access the stored images :</p>
  <pre class="lang-py prettyprint-override"><code>import sqlite3 as sql, io
  
  ...
  dicom_as_bytes = cursor.fetchone()[0] # read bytes from sqlite3 request
  dicom_as_bytes = io.BytesIO(dicom_as_bytes) # convert to file-like format
  
  dcm_file = dcmread(dicom_as_bytes)
  images = dcm_file.pixel_array # PermissionError: [Errno 13] Permission denied: 'C:\\Users\\&lt;User&gt;\\AppData\\Local\\Temp\\tmpfhvgc2oo'
  </code></pre>
  <p>There is no problem with the sql request in itself because I was able to write to a persistent file and read it :</p>
  <pre class="lang-py prettyprint-override"><code>tmp_file = open(&quot;tmp&quot;, &quot;wb&quot;)
  tmp_file.write(dicom_as_bytes)
  tmp_file.close()
  
  dcm_file = dcmread(&quot;tmp&quot;)
  images = dcm_file.pixel_array # no error
  </code></pre>
  <p>Free anonymized DICOM files can be found on the Internet with a bit of search. As I am not sure I have the right to link the one I used, I won't post a link here - sorry.</p>
  <p>Here is the <code>pydicom</code> documentation of the <code>dcmread</code> method : <a href="https://pydicom.github.io/pydicom/dev/reference/generated/pydicom.filereader.dcmread.html" rel="nofollow noreferrer">link</a></p>
  <p>The above method is working. However, I would prefer to avoid creating a temporary file in the project folder like that. I also took a look at python's <a href="https://docs.python.org/3/library/tempfile.html" rel="nofollow noreferrer">tempfile library</a> which doesn't seem to work for me as well, for one reason or another :</p>
  <pre><code>import tempfile
  
  tmp_file = tempfile.NamedTemporaryFile()
  temp_file.write(dicom_as_bytes)
  temp_file.seek(0)
  
  dcm_file = dcmread(temp_file)
  images = dcm_file.pixel_array # TypeError: GDCM could not read DICOM image
  
  temp_file.close()
  </code></pre>
  <p>In the end, I would like to know if it is possible to use <code>BytesIO</code> or <code>tempfile</code> to load the images of a DICOM file from <code>sqlite3</code> bytes of data corresponding exactly to <code>sqlite3.Binary(open(&quot;file.dcm&quot;, &quot;rb).read())</code> with <code>&quot;file.dcm&quot;</code> a DICOM file storing more than one image. I would prefer to not use a temporary file explicitely because I should have no reason to use temporary files (no lack of memory, storage space, I am not working on inter-process communication or crash recovery either).</p>
  <p>I am wondering if there is a need to somehow encode the received bytes before creating a new <code>BytesIO</code> object or writing to a <code>TemporaryFile</code>. However, this doesn't seem to be the case because I can just write them directly in a file with <code>open(filename, mode).write(dicom_bytes)</code> or <code>TemporaryFile().write(dicom_bytes)</code>. It's just that on the one hand, I can access the image array but not on the other (although I can access basic information such as <code>dcm_file.filename</code> without an issue). Maybe converting a <code>BytesIO</code> object to a <code>FileIO</code> object would work, although I have no idea how to do that without creating a new file with <code>open</code>.</p>