Esempio n. 1
0
 def setBackImage(self, name):
     """Set a new image on the back material."""
     self.backmaterial.texture.imagename = name
     img = Image.open(name)
     if img.mode!="RGB" and img.mode!="RGBA":
         img = img.convert("RGB")
     self.backmaterial.texture.image = img
     w,h = img.size
     f = (4.0*h)/(3.0*w)
     if f>=1.0:
         S = mat4(1).scale(vec3(f,1,1))
         S.translate(vec3(-0.5+0.5/f,0,0))
         draw = ImageDraw.Draw(img)
         draw.line([(0,0), (0,h)], fill=(0,0,0))
         draw.line([(1,0), (1,h)], fill=(0,0,0))
         draw.line([(w-1,0), (w-1,h)], fill=(0,0,0))
         draw.line([(w-2,0), (w-2,h)], fill=(0,0,0))
     else:
         f = (3.0*w)/(4.0*h)
         S = mat4(1).scale(vec3(1,f,1))
         S.translate(vec3(0,-0.5+0.5/f,0))
         draw = ImageDraw.Draw(img)
         draw.line([(0,0), (w,0)], fill=(0,0,0))
         draw.line([(0,1), (w,1)], fill=(0,0,0))
         draw.line([(0,h-1), (w,h-1)], fill=(0,0,0))
         draw.line([(0,h-2), (w,h-2)], fill=(0,0,0))
     invY = mat4(1).translate(vec3(0,1,0)).scale(vec3(1,-1,1))
     self.backmaterial.texture.transform = invY*S
Esempio n. 2
0
 def loadTexData(self):
     """Load and apply texture file.
     """
     # No image data set? Then use the file name and load the image...
     if self.image==None:
         if self.imagename=="":
             return
         print 'Loading "%s"...'%self.imagename,
         fullname = os.path.join(self._path, self.imagename)
         try:
             img = Image.open(fullname)
             self._passPILImage(img)
         except IOError, e:
             print "failed"
             print e
             return
Esempio n. 3
0
 def loadTexData(self):
     """Load and apply texture file.
     """
     # No image data set? Then use the file name and load the image...
     if self.image==None:
         if self.imagename=="":
             return
         print 'Loading "%s"...'%self.imagename,
         fullname = os.path.join(self._path, self.imagename)
         try:
             img = Image.open(fullname)
             self._passPILImage(img)
         except IOError, e:
             print "failed"
             print e
             return
Esempio n. 4
0
def stitch(filename, removetiles=False, infostream=None):
    """Stitch several image tiles together.
    
    filename is the base name of the image that determines the file names
    of the tiles. filename is also the name of the output image.
    If removetiles is True, the individual image files will be deleted
    after the image has been stitched.
    If infostream is set to a file like object it is used to output
    status information about the stitching process.

    The name of an image tile must contain the crop information that was
    used to create the image. For example, the name of a tile for an
    image "out.tif" could look like this: "out_0.0_0.5_0.75_1.0.tif".
    The four values are the x1,x2,y1,y2 values of the crop window. Those
    values together with the resolution of the tile determine the resolution
    of the entire image. The position of the tile within that image is
    given by x1,y1.
    """
    
    inname,inext = os.path.splitext(filename)
    tilenames = glob.glob("%s*%s"%(inname,inext))

    # A list of tuples (tilename, coords, img)
    tiles = []
    mode = None
    # Filter tiles and fill the tiles list...
    for tilename in tilenames:
        # No tile at all?
        if not isTile(tilename):
            continue
        
        name,ext,coords = splitTileName(tilename)
        
        # A tile of another image?
        if name!=inname or ext!=inext:
            continue

        # Open the tile image...
        img = Image.open(tilename)

        # Set required mode
        if mode==None:
            mode = img.mode
        if img.mode!=mode:
            raise ValueError, "%s: Mode mismatch, %s instead of %s"%(tilename, img.mode, mode)

        tiles.append((tilename, coords, img))

    # No tiles? then exit
    if len(tiles)==0:
        if infostream!=None:
            print >>infostream, 'No image tiles found for image "%s"'%filename
        return


    # Create the output image...
    width, height, xposs, yposs = outputSpecs(tiles)
    mode = tiles[0][2].mode
    if infostream!=None:
        print >>infostream, "Final resolution: %dx%d, mode: %s"%(width, height, mode)
    outimg = Image.new(mode, (width, height))

    # Paste the tiles into the output image...
    for tilename, coords, img in tiles:
        x = xposs[coords[0]]
        y = yposs[coords[2]]
        outimg.paste(img, (x, y))

    # Delete the reference to the image
    img = None

    # Save the output image
    if infostream!=None:
        print >>infostream, "Stitched %d tiles"%len(tiles)
        print >>infostream, 'Saving "%s"...'%filename
    outimg.save(filename)

    # Remove tiles (if desired)
    if removetiles:
        if infostream!=None:
            print >>infostream, "Removing tiles..."
        # Get the tile names and delete the tiles list
        # so that the images are no longer referenced
        # (otherwise they couldn't be deleted)
        tilenames = map(lambda x: x[0], tiles)
        tiles = None
        for tilename in tilenames:
            os.remove(tilename)
Esempio n. 5
0
def stitch(filename, removetiles=False, infostream=None):
    """Stitch several image tiles together.
    
    filename is the base name of the image that determines the file names
    of the tiles. filename is also the name of the output image.
    If removetiles is True, the individual image files will be deleted
    after the image has been stitched.
    If infostream is set to a file like object it is used to output
    status information about the stitching process.

    The name of an image tile must contain the crop information that was
    used to create the image. For example, the name of a tile for an
    image "out.tif" could look like this: "out_0.0_0.5_0.75_1.0.tif".
    The four values are the x1,x2,y1,y2 values of the crop window. Those
    values together with the resolution of the tile determine the resolution
    of the entire image. The position of the tile within that image is
    given by x1,y1.
    """

    inname, inext = os.path.splitext(filename)
    tilenames = glob.glob("%s*%s" % (inname, inext))

    # A list of tuples (tilename, coords, img)
    tiles = []
    mode = None
    # Filter tiles and fill the tiles list...
    for tilename in tilenames:
        # No tile at all?
        if not isTile(tilename):
            continue

        name, ext, coords = splitTileName(tilename)

        # A tile of another image?
        if name != inname or ext != inext:
            continue

        # Open the tile image...
        img = Image.open(tilename)

        # Set required mode
        if mode == None:
            mode = img.mode
        if img.mode != mode:
            raise ValueError("%s: Mode mismatch, %s instead of %s" %
                             (tilename, img.mode, mode))

        tiles.append((tilename, coords, img))

    # No tiles? then exit
    if len(tiles) == 0:
        if infostream != None:
            print >> infostream, 'No image tiles found for image "%s"' % filename
        return

    # Create the output image...
    width, height, xposs, yposs = outputSpecs(tiles)
    mode = tiles[0][2].mode
    if infostream != None:
        print >> infostream, "Final resolution: %dx%d, mode: %s" % (
            width, height, mode)
    outimg = Image.new(mode, (width, height))

    # Paste the tiles into the output image...
    for tilename, coords, img in tiles:
        x = xposs[coords[0]]
        y = yposs[coords[2]]
        outimg.paste(img, (x, y))

    # Delete the reference to the image
    img = None

    # Save the output image
    if infostream != None:
        print >> infostream, "Stitched %d tiles" % len(tiles)
        print >> infostream, 'Saving "%s"...' % filename
    outimg.save(filename)

    # Remove tiles (if desired)
    if removetiles:
        if infostream != None:
            print >> infostream, "Removing tiles..."
        # Get the tile names and delete the tiles list
        # so that the images are no longer referenced
        # (otherwise they couldn't be deleted)
        tilenames = map(lambda x: x[0], tiles)
        tiles = None
        for tilename in tilenames:
            os.remove(tilename)