# Documentation for image upload and calculation

Issue #257 invalid
Former user created an issue

I appreciate I am probably jumping the gun here, but I have only limited time to demonstrate this. I have uploaded an image and performed a calculation on it - feels like a hack but is this the way to go?

1. Create a file upload test called 'Image' with macro name 'image'. Include a calculation procedure:
```import numpy as np
image=np.fromfile(FILE).shape[0]
```
1. This test doesn't display the required data since the green upload bar covers it. Therefore create a composite test called 'Image Shape' and assign this to image data
```image_shape = image
```

I could perform a bunch of calculations in the upload macro and export the results as a dict. However I am guessing the image is available as a numpy array, rather than file object?

UPDATE: I see this is your method in http://randlet.com/qatrack/admin/qa/testlist/15/

1. Account Deleted reporter
• edited description
2. Account Deleted reporter
• edited description
3. Account Deleted reporter
• edited description
4. Account Deleted reporter
• edited description

@rob_cole

You can do something similar to the following. Create a file upload test (check off Display Image) with something similar to this procedure:

```from scipy.misc import imread
from scipy.ndimage import gaussian_filter

homer = {

"max": float(data.max()),
"min": float(data.min()),
"mean": float(data.mean()),
}

blurred = gaussian_filter(data, 10)

write_file('blurred_homer.png', blurred)
```

And another composite test with macro_name mean_homer with calculation like:

mean_homer = homer['mean']

That write_file function should also be able to take e.g. a matplotlib axes or figure instance and generate an image with it too. So you can generate plots with matplotlib and have them displayed as images.

@rob_cole For Pylinac like you asked you will need to do e.g. `pf = pylinac.PicketFence(BIN_FILE.name)`

5. Account Deleted reporter

Thanks Randle, that was fast! However I keep getting image FAILED and it is failing at the import of imread (upload succeeds if I comment it out) I have scipy==0.18.1

Ah right...scipy.imread requires PIL I think. matplotlib.image.imread might work without PIL. Otherwise you can install Pillow (I guess at this point you would add it to your requirements.txt file for Docker).

6. Account Deleted reporter

Interesting, that works on my Mac but fails on QAtrack.. Will try Pillow next week

7. Account Deleted reporter

OK added pillow == 4.1.1 to the requirements and your code is now working :-)

I just want to say Randle that I am exceptionally impressed what you have achieved in this program package (QATrack+).

Thank you for building what you have built :) it is beautiful :)

Thanks for the kind words...I certainly can't take all the credit as I had lots of guidance about the way things should work from a clinical perspective especially from @xtal. Lots of warts still to be resolved and improvements to be made but we're getting there!

You're welcome :)

8. Account Deleted reporter

Hi all trying and failing to read a dicom file into QAtrack. Any ideas?

```import dicom
flatsym = 1
```

Strange, I thought that would have worked. If you try

do you still see the same error?

9. Account Deleted reporter

Unfortunately yes, SERVER ERROR

Could be an issue with the dicom file missing the dicom header? pydicom has a `force` option that will force dicom parsing regardless of whether the header is present in the file or not. Try my_dcm = dicom.read_file(BIN_FILE, force=True)

I'd also maybe just try using pydicom to open the file in a local python interpreter.

• changed status to open
10. Closing this because there will be many docs to be written before release including image upload/analysis.

Hi @randlet, @rob_cole I was following some snippet in this thread and trying to reproduce what you guys have done here. And when I tried to save the blurred image, I got a `permission denied` exception. For the moment I could not find the exact location where apache2 is trying to save the blurred image. I have checked the `media/uploads/` location, the permission looks fine so that might not be the image-saving location. Is there any systematic way to resolve this kind of issue?

Plus, I have not used the `write_file` method to save images and fall back on `scipy.misc.imsave`, since the `write_file` method is not recognizable for the python interpreter. Thank you all in advance.

Hi @ruoxi_wang1812 the `write_file` method and viewing the resulting images is only available in v0.3.0 which hasn't been released yet. Are you using v0.2.9 or working with v0.3.0 code? If you are trying to use scipy.misc.imsave, then you likely need to use the full path to save an image (e.g. `scipy.misc.imsave('/home/ruoxi_wang1812/web/qatrackplus/media/uploads/tmp/your_file.png', ....)`.

Hi @randlet Thank you for your quick response! I am using the v0.2.9. I think that I'll save the image with the absolute path for the moment! Is there any way to insert any breakpoints so that the location to save the file can be visualized?

Sorry there's no way to set break points at this time. Without an absolute path imsave just saves in the current directory the QATrack+ process is running from...usually that would be e.g. `/home/ruoxi/web/qatrackplus/` but I'm not certain.