vincent
vincent

Reputation: 1678

replace black by white and white by black in images

I have black and white images (png) as follow : l want to replace the black background font color into white and the written character in white to black. How to identify color and replace it ?

for that l have thousand of images. It s not possible to manually identify in which image the character is in black and the background is in white and vice versa. all what l need is for every image l check if the character is written in black if not change the character color to black and the background to white.

here is a link to my sample of data : https://drive.google.com/open?id=0B-QJnh0Uw96lbFVHbVBPclhycFk

enter image description here

enter image description here

Upvotes: 0

Views: 8377

Answers (2)

For the sample you provided, if the image dimensions are >~26 pixels wide, invert.

Upvotes: 1

Tobias Senst
Tobias Senst

Reputation: 2830

If the white content has the value 255 (if it is a binary mask 1) just subtract your image from 255. In python if your image is img:

cv2.subtract(255, img) 

Upvotes: 4

Related Questions