Search code examples
pythonimagenumpyimage-processingpixel

convert data pixels to image


I have a list of X-ray output data that I want to convert to an image. These numbers are diode data. data But with the methods I test, I do not get the right image with Python. My data is as follows:

 [[(2590),(2375),(2521),(2231),(2519),(2683),(2431),(2515)..(2555)],
 [(2443),(2468),(2227),(2428),(2623),(2419),(2469),(2336),..(2435)],

.. [(2133),(2381),(2291),(2346),(2303),(2403),(2434),(2336),..(2374)] ]

I put pixels into numpy array. For example:1000*500

The size of each row and column is equal to the number of pixels in width and height. Each of number is a pixel data. Please help me how to convert?

Edit: Preliminary data:

enter image description here

Edit2: This is the output data guide. Do I have to do any special preprocessing?

enter image description here


Solution

  • Well first of all I am not sure what you mean by saving as a pixel data. If you ave a matrix say 2d numpy array you can already save it as an image file. Thus each element of the matrix will be a pixel value. I created my own small example:

    import numpy as np
    import matplotlib.pyplot as plt
    stf = [[(2590),(2375)], [(2443),(2468)]]
    stf_arr = np.array(stf) # converting the list of lists to numpy array
    stf_arr_rescaled = (255.0 / stf_arr.max() * (stf_arr - stf_arr.min())).astype(np.uint8)
    plt.imshow(stf_arr) # visualization with matplotlib
    # for saving the matrix itself
    from PIL import Image
    im = Image.fromarray(stf_arr_rescaled)
    im.save("stf_arr.png")