Skip to content

Instantly share code, notes, and snippets.

@ChrisCates
Created January 3, 2017 00:42
Show Gist options
  • Save ChrisCates/d2fb2a5255b368f9ac4e381c12b3b230 to your computer and use it in GitHub Desktop.
Save ChrisCates/d2fb2a5255b368f9ac4e381c12b3b230 to your computer and use it in GitHub Desktop.
path = "path/to/your/image"
img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
x1 = 0
x2 = 0
y1 = 0
y2 = 0
fx1 = False
fx2 = False
fy1 = False
fy2 = False
cursorY = 0
matchesY = False
for matrix in img:
cursorX = 0
matchesX = False
for rgb in matrix:
if rgb[3] > 0:
matchesX = True
if fx1 == False and fy1 == False:
x1 = cursorX
y1 = cursorY
fx1 = True
fy1 = True
if fy1 == False:
y1 = cursorY
fy1 = True
elif rgb[3] == 0 and matchesX == True:
if fx2 == False:
x2 = cursorX - 1
fx2 = True
cursorX = cursorX + 1
if matchesY == False and matchesX == False and fx1 == True:
if fy2 == False:
y2 = cursorY - 1
fy2 = True
cursorY = cursorY + 1
img = img[ y1 : y1 + y2, x1 : x1 + x2 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment