Selection based on UV and Texture Edit : The script work, I was using the wrong texture.

following my previous question, I'm working on selecting vertices in edit mode based on the UV coordinate and a grey scale image.

Here is my script at th moment :

import bpy
import bmesh
import math 

obj = bpy.context.active_object
me = obj.data
bm = bmesh.from_edit_mesh(me)

bpy.ops.mesh.select_all(action="SELECT")
bpy.ops.uv.select_all(action='SELECT')

uv_layer = bm.loops.layers.uv.verify()

uv_name = "MyTexture.png"   
tex = bpy.data.images.get(uv_name)
pixs = tex.pixels[:]
texSize = tex.size[0];
# adjust uv coordinates
for face in bm.faces:
    for loop in face.loops:
        loop_uv = loop[uv_layer]
        x = math.trunc(loop_uv.uv[0]*texSize)
        y = math.trunc(loop_uv.uv[1]*texSize)
        if ( x &gt;= 512) : x = 511
        if ( y &gt;= 512) : y = 511
        value = pixs[((y * texSize ) + x)*4]
        if(value &lt; 0.5):
            loop.vert.select = 0
            loop_uv.select = 0

bmesh.update_edit_mesh(me)


For some reason it lacks accuracy as some vertex who are too close from white pixel but are on black pixel are selected :



I thought it was the trunc operation but changing it to a ceil or round didn't change the lack of accuracy.

Any help is welcome.
I was using the wrong texture, the script works fine.

I was loading the wrong texture in the script and looking at another texture in the background of my UV editor.