regomodo
June 9th, 2008, 02:20 PM
I've just noticed a weird thing. I'm doing basic integer manipulation on images and it's a little slow. At some point i set my image array dtype to float. All of a sudden they've sped up no end. Why?
Here's a bit of simple code where float arithmetic isn't necessary.
#! /usr/bin/env python
import Image,numpy
im = Image.open("test.png").convert("L")
dimx = im.size[0]
dimy = im.size[1]
# use dtype = float for speed up
im_1 = numpy.asarray(im,dtype = float) # image as an array. READ-ONLY
def threshold(n):
im_m = numpy.zeros( (dimy,dimx) ,dtype = "uint8" )
for x in range(im.size[0]):
for y in range(im.size[1]):
if im_1[y,x] > n:
im_m[y,x] = 255
# not needed, they're already zero
# else:
# im_m[y,x] = 0
Image.fromarray(im_m).show()
threshold(10)
Here's a bit of simple code where float arithmetic isn't necessary.
#! /usr/bin/env python
import Image,numpy
im = Image.open("test.png").convert("L")
dimx = im.size[0]
dimy = im.size[1]
# use dtype = float for speed up
im_1 = numpy.asarray(im,dtype = float) # image as an array. READ-ONLY
def threshold(n):
im_m = numpy.zeros( (dimy,dimx) ,dtype = "uint8" )
for x in range(im.size[0]):
for y in range(im.size[1]):
if im_1[y,x] > n:
im_m[y,x] = 255
# not needed, they're already zero
# else:
# im_m[y,x] = 0
Image.fromarray(im_m).show()
threshold(10)