I can't seem to find any image rotation code that is fast enough for general image viewing purposes written in python. The only one that I can find is from PyComicsViewer project (Rotate.py), which is quite slow. It seems that most of the programs written in python tend to avoid using internal rotation code, while most of them resort to using external libraries. It there any faster way to rotate an image purely in python? (porting a C code to python is a daunting task for me since I only know Python and bash scripting though I understand a tiny bit of C).
You cannot do this quickly in a non-compiled language, and I don't mean bytecode.
Unless you do it with popen and imagemagick. Actually, there's probably a Python wrapper around that.
I've tried Imagemagick's "convert" before with popen, and it is no where close to GQView's speed, and somehow it's closer to the speed of PyComicsViewer's python code. I haven't tried PythonMagick wrapper yet to test the speed.
>>1
If you can't do it fast enough, maybe you can precompute the rotated images? Should work pretty well if it's only the generic "Rotate right/left" stuff.
Afaik GQView also preloads and prescales the next image while viewing, but I don't know about rotation.
Rotation by 90 degree increments, or arbitrary?
>>4
Preloading the pixbuf? For the currently rotated image, I already have it loaded in the memory should the user want to rotate it further more. However it's not speedy enough too. I do not preload the next image, but the speed for the preloaded vs newly loaded image for rotation is almost the same, though preloading improves performance for next image viewing and scaling quite nicely.
>>5
I want to be able to rotate CW/CCW by 90 degree only.
It's worth repeating what others already said: Even for simple 90 degree rotations, you are never going to be able to do it anywhere near even the slowest C code in pure Python. You will have to use an external library written in C.
The single trick that comes to mind is using OpenGL for all your drawing, and uploading images as textures, and then just drawing them rotated. That would be fast in any language, but I am not sure if you can do this in Python.
What >>7 (and >>2) said. Python is at least an order of magnitude slower at something like this, and a lot more difficult to micro-optimize.
If you just have to have it in Python, here's a clumsy stab at it:
def wtot(wordlen):
if wordlen == 4:
return 'L'
elif wordlen == 2:
return 'H'
elif wordlen == 1:
return 'B'
def mirror(source, wordlen):
arr = array(wtot(wordlen), source)
arr.reverse()
return arr.tostring()
def rotateLeft(old, maxX, maxY, wordlen):
type = wtot(wordlen)
oldArr = array(type, old)
newArr = array(type, '\0' * (maxX * maxY * wordlen))
maxXY = maxX * maxY
for y in xrange(maxY):
oldloc = maxX * y + maxX - 1
for newloc in xrange(y, maxXY + y, maxY):
newArr[newloc] = oldArr[oldloc]
oldloc -= 1
return newArr.tostring()
I don't know if the above even works (it sure won't with 24-bit data!), but it demonstrates how far you should go to move invariants out of the innermost loop, and whatever functionality feasible into the built-in functions.