Q:

OpenGL Texturing - some jpg's are being distorted in a strange way

I am trying to draw a textured square using Python, OpenGL and GLFW.
Here are all the images I need to show you.
Sorry for the way of posting images, but I don't have enough reputation to post more than 2 links (and I can't even post a photo).
I am getting this:
[the second image from the album]
Instead of that:
[the first image from the album]
BUT if I use some different jpg files:

some of them are being displayed properly,
some of them are being displayed properly until I rotate them 90 degrees (I mean using numpy rot90 function on an array with RGB components) and then send them to the GPU. And it looks like that (colors don't change, I only get some distortion):

Before rotation:
[the third image from the album]
After rotation:
[the fourth image from the album]
It all depends on a file. 
Does anybody know what I do wrong? Or see anything that I don't see?
Code:
First, I do the thing with initializing glfw, creating a window, etc.
if __name__ == '__main__':
    import sys
    import glfw
    import OpenGL.GL as gl
    import numpy as np

    from square import square
    from imio import imread,rgb_flag,swap_rb
    from txio import tx2gpu,txrefer

    glfw.glfwInit()
    win =glfw.glfwCreateWindow(800,800,"Hello")
    glfw.glfwMakeContextCurrent(win)

    glfw.glfwSwapInterval(1)
    gl.glClearColor(0.75,0.75,0.75,1.0)

Then I load an image using OpenCV imread function and I remember about swapping red with blue. Then I send the image to gpu - I will describe tx2gpu in a minute.
    image = imread('../imtools/image/ummagumma.jpg')
    if not rgb_flag: swap_rb(image)
    #image = np.rot90(image)
    tx_id = tx2gpu(image)

The swap_rb() function (defined in a different file, imported):
def swap_rb(mat):
    X = mat[:,:,2].copy()
    mat[:,:,2] = mat[:,:,0]
    mat[:,:,0] = X
    return mat

Then comes the main loop (in a while I will describe txrefer and square):
while not glfw.glfwWindowShouldClose(win):
    gl.glClear(gl.GL_COLOR_BUFFER_BIT)
    txrefer(tx_id); square(2); txrefer(0)  
    glfw.glfwSwapBuffers(win)
    glfw.glfwPollEvents()

And here is the end of the main function:
glfw.glfwDestroyWindow(win)
glfw.glfwTerminate()

NOW IMPORTANT THINGS:
A function that defines a square looks like that:
def square(scale=1.0,color=None,solid=True):
    s = scale*.5
    if type(color)!=type(None):
        if solid: 
            gl.glBegin(gl.GL_TRIANGLE_FAN)
        else:
            gl.glBegin(gl.GL_LINE_LOOP)
        gl.glColor3f(*color[0][:3]); gl.glVertex3f(-s,-s,0)
        gl.glColor3f(*color[1][:3]); gl.glVertex3f(-s,s,0)
        gl.glColor3f(*color[2][:3]); gl.glVertex3f(s,s,0)
        gl.glColor3f(*color[3][:3]); gl.glVertex3f(s,-s,0)

    else:    
        if solid: 
            gl.glBegin(gl.GL_TRIANGLE_FAN)
        else:
            gl.glBegin(gl.GL_LINE_LOOP)
        gl.glTexCoord2f(0,0); gl.glVertex3f(-s,-s,0)
        gl.glTexCoord2f(0,1); gl.glVertex3f(-s,s,0)
        gl.glTexCoord2f(1,1); gl.glVertex3f(s,s,0)
        gl.glTexCoord2f(1,0); gl.glVertex3f(s,-s,0)
    gl.glEnd()

And texturing functions look like that:
import OpenGL.GL as gl

unit_symbols = [
    gl.GL_TEXTURE0,gl.GL_TEXTURE1,gl.GL_TEXTURE2,
    gl.GL_TEXTURE3,gl.GL_TEXTURE4,
    gl.GL_TEXTURE5,gl.GL_TEXTURE6,gl.GL_TEXTURE7,
    gl.GL_TEXTURE8,gl.GL_TEXTURE9,
    gl.GL_TEXTURE10,gl.GL_TEXTURE11,gl.GL_TEXTURE12,
    gl.GL_TEXTURE13,gl.GL_TEXTURE14,
    gl.GL_TEXTURE15,gl.GL_TEXTURE16,gl.GL_TEXTURE17,
    gl.GL_TEXTURE18,gl.GL_TEXTURE19,
    gl.GL_TEXTURE20,gl.GL_TEXTURE21,gl.GL_TEXTURE22,
    gl.GL_TEXTURE23,gl.GL_TEXTURE24,
    gl.GL_TEXTURE25,gl.GL_TEXTURE26,gl.GL_TEXTURE27,
    gl.GL_TEXTURE28,gl.GL_TEXTURE29,
    gl.GL_TEXTURE30,gl.GL_TEXTURE31]

def tx2gpu(image,flip=True,unit=0):
    gl.glActiveTexture(unit_symbols[unit])
    texture_id = gl.glGenTextures(1)
    gl.glBindTexture(gl.GL_TEXTURE_2D,texture_id)

    gl.glTexParameteri(gl.GL_TEXTURE_2D,gl.GL_TEXTURE_WRAP_S,gl.GL_REPEAT)
    gl.glTexParameteri(gl.GL_TEXTURE_2D,gl.GL_TEXTURE_WRAP_T,gl.GL_REPEAT)
    gl.glTexParameteri(gl.GL_TEXTURE_2D,gl.GL_TEXTURE_MAG_FILTER,gl.GL_LINEAR)
    gl.glTexParameteri(gl.GL_TEXTURE_2D,gl.GL_TEXTURE_MIN_FILTER,gl.GL_LINEAR)        

    yres,xres,cres = image.shape

    from numpy import flipud
    gl.glTexImage2D(gl.GL_TEXTURE_2D,0,gl.GL_RGB,xres,yres,0,gl.GL_RGB,gl.GL_UNSIGNED_BYTE,flipud(image))

    gl.glBindTexture(gl.GL_TEXTURE_2D,0)
    return texture_id

def txrefer(tex_id,unit=0):
    gl.glColor4f(1,1,1,1);
    gl.glActiveTexture(unit_symbols[unit])
    if tex_id!=0:
        gl.glEnable(gl.GL_TEXTURE_2D)
        gl.glBindTexture(gl.GL_TEXTURE_2D,tex_id)
    else:
        gl.glBindTexture(gl.GL_TEXTURE_2D,0)
        gl.glDisable(gl.GL_TEXTURE_2D)

A:

The problem you have there are alignment issues. OpenGL initial alignment setting for "unpacking" images is that each row starts on a 4 byte boundary. This happens if the image width is not a multiple of 4 or if there are not 4 bytes per pixel. But it's easy enough to change this:
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)

would probably do the trick for you. Call it right before glTex[Sub]Image.
Another thing: Your unit_symbols list is completely unnecessary. The OpenGL specification explicitly says that GL_TEXTUREn = GL_TEXTURE0 + n. You can simply do glActiveTexture(GL_TEXTURE0 + n). However when loading a texture image the unit is completely irrelevant; the only thing it may matter is, that loading a texture only goes with binding one, which happens in a texture unit; a texture can be bound in any texture unit desired.
Personally I use the highest texture unit for loading images, to avoid accidently clobbering required state.

