I'm writing a code that part of it is reading an image source and displaying it on the screen for the user to interact with. I also need the sharpened image data. I use the following to read the data and display it in
#read the image data and return it, with the 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)
#get image data
scan,sharpen = image_and_sharpen_array('foo.jpg')
w,h,c = scan.shape
#setting up pygame
screen = pygame.display.set_mode((w,h))
And the image is displayed on the screen only rotated and inverted. Is this due to differences between
pyGame? Or is this due to something wrong in my code?
Is there other way to do this? The majority of solution I read involved saving the figure and then reading it with ``pyGame''.
Best How To :
Every lib has its own way of interpreting image arrays. By 'rotated' I suppose you mean transposed. That's the way PyGame inflates numpy arrays in its surface buffer. There are many ways to make it look 'correct'. Actually there are many ways even to show up the array, which gives you full control over channel representation and so on, also depending on version of PyGame. I can only tell how I draw arrays in pygame 1.9.2, and this is the fastest array rendering that I could ever achieve. (Note for earlier version this will not work!). This function will fill the surface with array:
def put_array(Dest, myarr): # put array into surface
bv = Dest.get_view("0")
Now what is important, you still get probably not what you want, so it is transposed or have swapped color channels. The idea is, manage your arrays in that form, which suites this surface buffer. To find out what is correct channel order and axes order, use
openCV library (cv2.imread(filename)). With openCV you open images in BGR order as standard, and it has a lot of conversion functions. If I remember correctly, when writing directly to surface buffer, BGR is the correct order. So you can try to put the image array which you get out of file with this function (if you have pygame 1.9.2) and blit to the screen. Also aware of what surface format you use, it is very important.
There are other ways to draw arrays e.g. here is whole set of helper functions http://www.pygame.org/docs/ref/surfarray.html
So just experiment to find out what is best for you - performance or specific array representation form. Small tip: To do 'signalling test' use a picture, like this. So you will immediately see if something is wrong, just load as array and try to render.