I was trying to generate some bar-code (using python-barcode) with product info I successfully generate bar-code but Not to remove under code and write my own text
My code:
import barcode
from barcode.writer import ImageWriter
barCode = "00000016901011900000"
barCodeImage = barcode.get('Code128', barCode, writer=ImageWriter())
barCodeImage.save("one")
Which gives me this barcode
But I want to remove that showing number under the bar-code and write some text (as product info or name)
Here's a method using OpenCV
After converting to grayscale, we Otsu's threshold to get a binary image
Now we dilate to connect the contours
From here we find contours and sort using contour area. The smaller contour will be the ROI of the number. Here's the detected number ROI
Now we "erase" the number by coloring in the ROI with white and write our desired text with cv2.putText()
. Here's the result
import cv2
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray,0,255,cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
dilate = cv2.dilate(thresh, kernel, iterations=3)
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea)
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
image[y:y+h, x:x+w] = [255,255,255]
cv2.putText(image, 'Super Spicy Example Text', (x,y), \
cv2.FONT_HERSHEY_SIMPLEX, .6, (0,0,0), 1)
break
cv2.imshow('thresh', thresh)
cv2.imshow('dilate', dilate)
cv2.imshow('image', image)
cv2.waitKey(0)