python - How to present numpy array into pygame surface? -
i'm writing code part of reading image source , displaying on screen user interact with. need sharpened image data. use following read data , display in pygame
def image_and_sharpen_array(file_name): #read image data , return it, sharpened image image = misc.imread(file_name) blurred = ndimage.gaussian_filter(image,3) edge = ndimage.gaussian_filter(blurred,1) alpha = 20 out = blurred + alpha*(blurred - edge) return image,out #get image data scan,sharpen = image_and_sharpen_array('foo.jpg') w,h,c = scan.shape #setting pygame pygame.init() screen = pygame.display.set_mode((w,h)) pygame.surfarray.blit_array(screen,scan) pygame.display.update()
and image displayed on screen rotated , inverted. due differences between misc.imread
, pygame
? or due wrong in code?
is there other way this? majority of solution read involved saving figure , reading ``pygame''.
every lib has own way of interpreting image arrays. 'rotated' suppose mean transposed. that's way pygame shows numpy arrays. there many ways make 'correct'. there many ways show array, gives full control on channel representation , on. in pygame version 1.9.2, fastest array rendering ever achieve. (note earlier version not work!). function fill surface array:
def put_array(surface, myarr): # put array surface bv = surface.get_view("0") bv.write(myarr.tostring())
if not working, use this, should work everywhere:
# put array data pygame surface def put_arr(surface, myarr): bv = surface.get_buffer() bv.write(myarr.tostring(), 0)
you still not want, transposed or have swapped color channels. idea is, manage arrays in form, suites surface buffer. find out correct channel order , axes order, use opencv
library (cv2.imread(filename)). opencv open images in bgr order standard, , has lot of conversion functions. if remember correctly, when writing directly surface buffer, bgr correct order 24 bit , bgra 32 bit surface. can try put image array out of file function , blit screen.
there other ways draw arrays e.g. here whole set of helper functions http://www.pygame.org/docs/ref/surfarray.html
not recommend using it, since surfaces not direct pixel manipulating, lost in references. small tip: 'signalling test' use picture, this. see if wrong, load array , try render.
Comments
Post a Comment