Skip to content

Instantly share code, notes, and snippets.

@dheshanm
Created March 23, 2021 14:05
Show Gist options
  • Save dheshanm/414be5d2b02e3a9afebe8f99b2be0a0c to your computer and use it in GitHub Desktop.
Save dheshanm/414be5d2b02e3a9afebe8f99b2be0a0c to your computer and use it in GitHub Desktop.
Image Tilimg
import cv2
import math
import os
# Source: https://stackoverflow.com/questions/37842476/image-tiling-in-loops-using-python-opencv
# credit: https://stackoverflow.com/users/2561733/telepinu
Path = "FullImage.tif";
filename, file_extension = os.path.splitext(Path)
image = cv2.imread(Path, 0)
tileSizeX = 256;
tileSizeY = 256;
numTilesX = math.ceil(image.shape[1]/tileSizeX)
numTilesY = math.ceil(image.shape[0]/tileSizeY)
makeLastPartFull = True; # in case you need even siez
for nTileX in range(numTilesX):
for nTileY in range(numTilesY):
startX = nTileX*tileSizeX
endX = startX + tileSizeX
startY = nTileY*tileSizeY
endY = startY + tileSizeY;
if(endY > image.shape[0]):
endY = image.shape[0]
if(endX > image.shape[1]):
endX = image.shape[1]
if( makeLastPartFull == True and (nTileX == numTilesX-1 or nTileY == numTilesY-1) ):
startX = endX - tileSizeX
startY = endY - tileSizeY
currentTile = image[startY:endY, startX:endX]
cv2.imwrite(filename + '_%d_%d' % (nTileY, nTileX) + file_extension, currentTile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment