Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- def invert_font_color(path):
- image = cv2.imread(path, cv2.IMREAD_UNCHANGED)
- b, g, r, a = cv2.split(image)
- contours, hierarchy = cv2.findContours(a, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
- for contour in contours:
- xOffset, yOffset, width, height = cv2.boundingRect(contour)
- result = True
- # go through every pixel and check if it contains colors that are not transparent
- for x in range(width):
- for y in range(height):
- xpos = xOffset + x
- ypos = yOffset + y
- if (image[ypos][xpos][0] <= 250 and image[ypos][xpos][1] <= 250 and image[ypos][xpos][2] >= 250) and \
- image[ypos][xpos][3] != 0:
- result = False
- # if only white, non transparent colors --> Change color to black
- if result:
- for x in range(width - 1):
- for y in range(height - 1):
- xpos = xOffset + x
- ypos = yOffset + y
- if image[ypos][xpos][0] >= 250 and image[ypos][xpos][1] >= 250 and image[ypos][xpos][2] >= 250 and \
- image[ypos][xpos][3] != 0:
- image[ypos][xpos][0] = 0
- image[ypos][xpos][1] = 0
- image[ypos][xpos][2] = 0
- cv2.imwrite("out.png", image)
- if __name__ == '__main__':
- invert_font_color(".\\in.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement