2017-12-22 14 views
0

Ich habe derzeit ein numpy Array oder RBG Tupel, die ich in ein PIL-Bild konvertieren und speichern möchte. Momentan mache ich folgendes: final = Image.fromarray(im_arr, mode='RGB'). Hier ist im_arr das numpy Array von Tupeln in der Form (R, G, B). Es scheint am Ende, die Tupel getrennt zu halten, wenn es das Bild wie unten gezeigt erzeugt.Numpy Array von Tupeln zu PIL Bild

enter image description here

Antwort

0

Probieren Sie diese Funktionen:

import numpy 
import Image 

def PIL2array(img): 
    return numpy.array(img.getdata(), 
        numpy.uint8).reshape(img.size[1], img.size[0], 3) 

def array2PIL(arr, size): 
    mode = 'RGBA' 
    arr = arr.reshape(arr.shape[0]*arr.shape[1], arr.shape[2]) 
    if len(arr[0]) == 3: 
     arr = numpy.c_[arr, 255*numpy.ones((len(arr),1), numpy.uint8)] 
    return Image.frombuffer(mode, size, arr.tostring(), 'raw', mode, 0, 1) 

def main(): 
    img = loadImage('foo.jpg') 
    arr = PIL2array(img) 
    img2 = array2PIL(arr, img.size) 
    img2.save('out.jpg') 

if __name__ == '__main__': 
    main() 

Credit: http://code.activestate.com/recipes/577591-conversion-of-pil-image-and-numpy-array/

Weitere Informationen: http://www.imagexd.org/tutorial/lessons/0_images_are_arrays.html

Falls es nicht, vielleicht funktioniert das Array nicht über die passende Form.