Auto remove "border" from texture?

In an application I’m building, I’m pulling images off the internet and using them as in-game textures. Since I’m doing this dynamically and at runtime, some images appear pretty awkward. I know I can’t solve every issue, but the biggest one is when an image has a “border”. See attached image. Is there a method in Panda or perhaps PIL to remove the border of a specific (solid) color?

yes, its not that hard, you can modify my picture analyzer code for it. i used pil for getting the pixel color.

#tracker 
#dirk hochegger 2008



import Image
from direct.directtools.DirectGeometry import LineNodePath 
from pandac.PandaModules import Point3,Vec3,Vec4 
from direct.gui.OnscreenText import OnscreenText 
from direct.gui.DirectGui import * 
from pandac.PandaModules import * 
import direct.directbase.DirectStart 
from direct.showbase import DirectObject 
from direct.gui.OnscreenImage import OnscreenImage 
from direct.gui.OnscreenText import OnscreenText 
from direct.gui.DirectGuiBase import DirectGuiBase 
from direct.gui import DirectGuiGlobals as DGG 
from pandac.PandaModules import PGMouseWatcherParameter 
import math 
import os
import time


image = Image.open("Sunset.jpg")
#image1 = OnscreenImage() 
#image1 = OnscreenImage(image = 'Sunset.jpg')    
#image1.setScale(1,1,1) 
#image1.setBin("fixed", 0)
size = image.size
resX = size[0]-1
resY = size[1]-1
checkENTRIES = 0
endTIME = 0

count = 0 

BCX = 0
BCY = 0
BCXG = 0
BCYG = 0
BCXB = 0
BCYB = 0

xx = 0
xd = 0
xxd = 0
xdG = 0
xxdG = 0
xdB = 0
xxdB = 0

rxx = 0
gxx = 0
bxx = 0

color = []
pos = []

threshold = 200
rangeR = 10
rangeG = 10
rangeB = 10
colorconnectR = 0
colorconnectG = 0
colorconnectB = 0


pixels = resX*resY
trackrange = 10

x = 0
y = 0

red = []
redX = []
green = []
greenX = []
blue = []
blueX = []

dx3 = 0
dy3 = 0

bx3 = 0
by3 = 0

BOUNDSredX = []
BOUNDSredY = []
BOUNDSgreenX = []
BOUNDSgreenY = []
BOUNDSblueX = []
BOUNDSblueY = []

startTIME = time.time()    
 #main loop
#get the pixel color
for i in range (pixels):
    count+=1
    y+=1
    xx-=1

    #array color and position     
    color.append([image.getpixel((x,y))])
    colorRANGE = image.getpixel((x,y))
    pos.append([x,y])
    r = colorRANGE[0]/255.0
    g = colorRANGE[1]/255.0
    b = colorRANGE[2]/255.0
    
    if colorRANGE[0] > threshold:
        px = 0.01*x
        py = 0.01*y
        BOX = LineNodePath(render2d,'RED',2,Vec4(r,g,b,0))      
        BOX.drawLines([[(px-.01,0,(-py+.01)),(px+.01,0,(-py+.01))]])
        BOX.setBin("fixed", 1)
        BOX.create()
        red.append([y])
        redX.append([x])
    if colorRANGE[1] > threshold:
        px1 = 0.01*x
        py1 = 0.01*y
        BOX1 = LineNodePath(render2d,'GREEN',2,Vec4(r,g,b,0))      
        BOX1.drawLines([[(px1-.01,0,(-py1+.01)),(px1+.01,0,(-py1+.01))]])
        BOX1.setBin("fixed", 2)
        BOX1.create()
        green.append([y])
        greenX.append([x])
    if colorRANGE[2] > threshold:
        px2 = 0.01*x
        py2 = 0.01*y
        BOX2 = LineNodePath(render2d,'BLUE',2,Vec4(r,g,b,0))     
        BOX2.drawLines([[(px2-.01,0,(-py2+.01)),(px2+.01,0,(-py2+.01))]])
        BOX2.setBin("fixed", 3)
        BOX2.create()
        blue.append([y])
        blueX.append([x])

        
        
    if y >= resY:
        x+=1
        y=0 
        
    
    #end process
    if count == pixels:
        print "done"
        endTIME = time.time()
        usedTIME = endTIME - startTIME
        print usedTIME
        
            
#print all values of the color array   
for ii in range (rangeR):  
    xxd+=2
    xd+=1
    
    if threshold < 255:
        ddy = str(red[xd])
        countR = len(ddy)
        if countR > 3:
            ddy1 = ddy[1:3]
            ddy2 = int(ddy1)
            rxx = 1
            
    if threshold < 255:
        ddy1 = str(red[ii])
        countR1 = len(ddy1)
        if countR1 > 3:
            dy1 = ddy1[1:3]
            dy2 = int(dy1)

    if rxx == 1:
        difR = dy2 -ddy2
        if (difR < colorconnectR):
            
            dx=str(redX[xd])
            countR = len(dx)
            if countR > 3:
                dx1 = dx[1:3]
                dx2 = int(dx1)
                
                dy=str(red[xd])
                dy1 = dy[1:3]
                dy2 = int(dy1)
                
                dx3 = 0.01*dx2
                dy3 = 0.01*dy2
                
                BOX3 = LineNodePath(render2d,'RED',2,Vec4(1,1,0,0))     
                BOX3.drawLines([[(dx3-.02,0,(-dy3+.02)),(dx3+.02,0,(-dy3+.02))]])
                BOX3.setBin("fixed", 4)
                BOX3.create()
                
                #bounding
                BOUNDSredX.append(dx3)
                BOUNDSredX.sort()
                BRXL=len(BOUNDSredX)
                BOUNDSredXmin = BOUNDSredX[0]*100.0
                BOUNDSredXmax = BOUNDSredX[BRXL-1]*100.0
                BOUNDScenterX = (BOUNDSredXmax+BOUNDSredXmin)/2   
      
                BCX = 0.01*BOUNDScenterX
                                
                BOUNDSredY.append(dy3)
                BOUNDSredY.sort()
                BRYL=len(BOUNDSredY)
                BOUNDSredYmin = BOUNDSredY[0]*100.0
                BOUNDSredYmax = BOUNDSredY[BRYL-1]*100.0
                BOUNDScenterY = (BOUNDSredYmax+BOUNDSredYmin)/2
                
                BCY = 0.01*BOUNDScenterY
                  
                
                                              
                if xd == rangeR:
                    BOUNDred = LineNodePath(render2d,'REDbound',10,Vec4(1,1,1,0))     
                    BOUNDred.drawLines([[(BCX-.02,0,(-BCY+.02)),(BCX+.02,0,(-BCY+.02))]])
                    BOUNDred.setBin("fixed", 5)
                    BOUNDred.create()

for ii in range (rangeG):  
    xxdG+=2
    xdG+=1    
                   
    if threshold < 255:
        aay = str(green[xdG])
        countG = len(aay)
        if countG > 3:
            aay1 = aay[1:3]
            aay2 = int(aay1)
   
    if threshold < 255:
        aay1 = str(green[ii])
        countG1 = len(aay1)
        if countG1 > 3:
            ay1 = aay1[1:3]
            ay2 = int(ay1)
            gxx = 1

    if gxx == 1: 
            difG = ay2 -aay2  
            if (difG < colorconnectG):
               # print difR
                
                gx=str(greenX[xdG])
                countG = len(gx)
                if countG > 3:
                    gx1 = gx[1:3]
                    gx2 = int(gx1)
                    
                    gy=str(green[xdG])
                    gy1 = gy[1:3]
                    gy2 = int(gy1)
                    
                    gx3 = 0.01*gx2
                    gy3 = 0.01*gy2
                    
                    
                    
                    BOX4 = LineNodePath(render2d,'GREEN',2,Vec4(1,1,0,0))     
                    BOX4.drawLines([[(gx3-.02,0,(-gy3+.02)),(gx3+.02,0,(-gy3+.02))]])
                    BOX4.setBin("fixed", 4)
                    BOX4.create()
                    
                    #bounding
                    BOUNDSgreenX.append(gx3)
                    BOUNDSgreenX.sort()
                    BGXL=len(BOUNDSgreenX)
                    BOUNDSgreenXmin = BOUNDSgreenX[0]*100.0
                    BOUNDSgreenXmax = BOUNDSgreenX[BGXL-1]*100.0
                    BOUNDScenterXg = (BOUNDSgreenXmax+BOUNDSgreenXmin)/2   
          
                    BCXG = 0.01*BOUNDScenterXg
                                    
                    BOUNDSgreenY.append(gy3)
                    BOUNDSgreenY.sort()
                    BGYL=len(BOUNDSgreenY)
                    BOUNDSgreenYmin = BOUNDSgreenY[0]*100.0
                    BOUNDSgreenYmax = BOUNDSgreenY[BGYL-1]*100.0
                    BOUNDScenterYg = (BOUNDSgreenYmax+BOUNDSgreenYmin)/2
                    
                    BCYG = 0.01*BOUNDScenterYg
                                                   
                    if xdG == rangeG:
                        BOUNDred = LineNodePath(render2d,'REDbound',10,Vec4(1,1,1,0))     
                        BOUNDred.drawLines([[(BCXG-.02,0,(-BCYG+.02)),(BCXG+.02,0,(-BCYG+.02))]])
                        BOUNDred.setBin("fixed", 5)
                        BOUNDred.create()
                    
for ii in range (rangeB):  
    xxdB+=2
    xdB+=1 
    if threshold < 255:
        bby = str(blue[xdB])
        countB = len(bby)
        if countB > 3:
            bby1 = bby[1:3]
            bby2 = int(bby1)
   
    if threshold < 255:
        bby1 = str(blue[ii])
        countB1 = len(bby1)
        if countB1 > 3:
            by1 = bby1[1:3]
            by2 = int(by1)
            bxx = 1
            
    if bxx == 1:
        difB = by2 -bby2
        if (difB < colorconnectB):
           # print difR
            
            bx=str(blueX[xdB])
            countB = len(dx)
            if countB > 3:
                bx1 = bx[1:3]
                bx2 = int(bx1)
                
                by=str(blue[xdB])
                by1 = by[1:3]
                by2 = int(by1)
                
                bx3 = 0.01*bx2
                by3 = 0.01*by2
                
                BOX5 = LineNodePath(render2d,'BLUE',2,Vec4(1,1,0,0))     
                BOX5.drawLines([[(bx3-.02,0,(-by3+.02)),(bx3+.02,0,(-by3+.02))]])
                BOX5.setBin("fixed", 4)
                BOX5.create()
                
                #bounding
                BOUNDSblueX.append(bx3)
                BOUNDSblueX.sort()
                BBXL=len(BOUNDSblueX)
                BOUNDSblueXmin = BOUNDSblueX[0]*100.0
                BOUNDSblueXmax = BOUNDSblueX[BBXL-1]*100.0
                BOUNDScenterXb = (BOUNDSblueXmax+BOUNDSblueXmin)/2   
          
                BCXB = 0.01*BOUNDScenterXb
                                    
                BOUNDSblueY.append(by3)
                BOUNDSblueY.sort()
                BBYL=len(BOUNDSblueY)
                BOUNDSblueYmin = BOUNDSblueY[0]*100.0
                BOUNDSblueYmax = BOUNDSblueY[BBYL-1]*100.0
                BOUNDScenterYb = (BOUNDSblueYmax+BOUNDSblueYmin)/2
                    
                BCYB = 0.01*BOUNDScenterYb
                                                   
                if xdB == rangeB:
                    BOUNDred = LineNodePath(render2d,'REDbound',10,Vec4(1,1,1,0))     
                    BOUNDred.drawLines([[(BCXB-.02,0,(-BCYB+.02)),(BCXB+.02,0,(-BCYB+.02))]])
                    BOUNDred.setBin("fixed", 5)
                    BOUNDred.create()
                    
                    checkENTRIES = rxx+gxx+bxx   
                    if checkENTRIES == 3:
                        if (bxx ==1)or(gxx == 1)or(rxx == 1):
                            TRI1 = LineNodePath(render2d,'BLUE',2,Vec4(1,1,1,0))     
                            TRI1.drawLines([[(BCX-.02,0,(-BCY+.02)),(BCXG-.02,0,(-BCYG+.02)),(BCXB-.02,0,(-BCYB+.02)),(BCX-.02,0,(-BCY+.02))]])
                            TRI1.setBin("fixed", 4)
                            TRI1.create()
                            print "hit" 
     
     
    
    
        

    


run()

good luck
dirk

Not sure if this is what you mean, but you should set the wrap mode to WMClamp (as described at the bottom of this page:)
panda3d.org/manual/index.php/T … Wrap_Modes

Thanks ditus. I figured there might be some function already coded for this, but it was pretty easy to code my own.

Here is my solution if anyone is interested. This only works for images that are centered within a border. In other words, it only detects top and left and then assumes right and bottom are the same width border.

http://dpaste.com/hold/148083/

import Image, time, os
start = time.time()

COLOR_RANGE = range(240,256)                        #Range of RGB values that define the border color. Use this to adjust color sensitivity. Currently set for WHITE.
DIR = "c:\\temp\\images\\"                          #Input directory with some images
OUT_DIR = "c:\\temp\\outImages\\"                   #Directory to output the image files
IMAGE_FORMATS = ('jpg', 'jpeg', 'bmp', 'png')       #Acceptable image formats to process
OUT_FORMAT = "JPEG"

for image in os.listdir(DIR):
    if image.split(".")[-1].lower() in IMAGE_FORMATS:
        im = Image.open(DIR + image)
        #If not RGB, convert
        if im.mode != "RGB":
            im = im.convert("RGB")
        data = im.getdata()

        resX = im.size[0]
        resY = im.size[1]
        xCut = 0
        yCut = 0

        #Find top/bottom
        for i in range(0,len(data)):
            if data[i][0] not in COLOR_RANGE and data[i][1] not in COLOR_RANGE and data[i][2] not in COLOR_RANGE:
                yCut = round(i/resX)
                break            
        #Find left/right
        stop = False
        for i in range(0,resX):
            for j in range(i,len(data), resX):
                if data[j][0] not in COLOR_RANGE and data[j][1] not in COLOR_RANGE and data[j][2] not in COLOR_RANGE:
                    xCut = j%resX                  
                    stop = True
                    break                    
            if stop:
                break

        im = im.crop((xCut,yCut,resX-xCut,resY-yCut))
        im.save(OUT_DIR + image, OUT_FORMAT)

stop = time.time()
print "Processed %d images in %f seconds..." % (len(os.listdir(DIR)), stop-start)