Giter Club home page Giter Club logo

Comments (5)

Rothfeld avatar Rothfeld commented on June 18, 2024

in infer.py, change Line 148 from
Image.fromarray(final).save(save_path)

to
Image.fromarray(final.squeeze()).save(save_path)

from adabins.

NitishJaiswal avatar NitishJaiswal commented on June 18, 2024

Thanks for sharing comment @Rothfeld , np.squeeze resolves this issue

from adabins.

NitishJaiswal avatar NitishJaiswal commented on June 18, 2024

I used .squeeze() function earlier for the same issue on a single PIL image and it worked perfectly and I assumed it should resolve when trying to do for the whole image directory. But, unexpectedly, .squeeze() function doesn't resolve the issue for that part, any ideas?
Input

# predict depths of images stored in a directory and store the predictions in 16-bit format in a given separate dir
infer_helper.predict_dir("test_imgs", "test_imgs_results")  #test_imgs_results is a new directory to store output

Output

KeyError                                  Traceback (most recent call last)
D:\anaconda3\lib\site-packages\PIL\Image.py in fromarray(obj, mode)
   2750         try:
-> 2751             mode, rawmode = _fromarray_typemap[typekey]
   2752         except KeyError as e:

KeyError: ((1, 1, 480, 640), '<u2')

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-7-36b5885687fe> in <module>
      1 # predict depths of images stored in a directory and store the predictions in 16-bit format in a given separate dir
----> 2 infer_helper.predict_dir("test_imgs", "test_imgs_results")

D:\anaconda3\lib\site-packages\torch\autograd\grad_mode.py in decorate_context(*args, **kwargs)
     24         def decorate_context(*args, **kwargs):
     25             with self.__class__():
---> 26                 return func(*args, **kwargs)
     27         return cast(F, decorate_context)
     28 

<private_repo>\infer.py in predict_dir(self, test_dir, out_dir)
    146             save_path = os.path.join(out_dir, basename + ".png")
    147 
--> 148             Image.fromarray(final.squeeze()).save(save_path)  #added .squeeze() to shrink size from 1,1,480,640 to 480,640
    149 
    150 

D:\anaconda3\lib\site-packages\PIL\Image.py in fromarray(obj, mode)
   2751             mode, rawmode = _fromarray_typemap[typekey]
   2752         except KeyError as e:
-> 2753             raise TypeError("Cannot handle this data type: %s, %s" % typekey) from e
   2754     else:
   2755         rawmode = mode

TypeError: Cannot handle this data type: (1, 1, 480, 640), <u2

from adabins.

INF800 avatar INF800 commented on June 18, 2024

@NitishJaiswal it was working in #13

from adabins.

younas-aziz avatar younas-aziz commented on June 18, 2024

Need Help:
def convert_nifti_to_png(input_path, output_path):
img = nib.load(input_path)
data = img.get_fdata()
data_normalized = (data - np.min(data)) / (np.max(data) - np.min(data))
data_scaled = (data_normalized * 255).astype(np.uint8)
data_3d = np.squeeze(data_scaled)
data_uint8 = data_scaled.astype(np.uint8)
image = Image.fromarray(data_uint8)
image.save(output_path, 'PNG')

KeyError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/PIL/Image.py in fromarray(obj, mode)
2834 try:
-> 2835 mode, rawmode = _fromarray_typemap[typekey]
2836 except KeyError as e:

KeyError: ((1, 1, 10, 30), '|u1')

The above exception was the direct cause of the following exception:

TypeError Traceback (most recent call last)
3 frames
/usr/local/lib/python3.10/dist-packages/PIL/Image.py in fromarray(obj, mode)
2835 mode, rawmode = _fromarray_typemap[typekey]
2836 except KeyError as e:
-> 2837 raise TypeError("Cannot handle this data type: %s, %s" % typekey) from e
2838 else:
2839 rawmode = mode

TypeError: Cannot handle this data type: (1, 1, 10, 30), |u1

from adabins.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.