I am trying to draw boxes on an image and saving it back to google bucket. I’m running into an issue when an image has highlighting on it (it works fine when images do not) where it is turning the image background black thus drowning out the text that is in black. I’ve tried a lot of solutions but cannot get it to save correctly. Any help greatly appreciated.
I have the following code:
def draw_boxes_on_image(image, box_coordinates, color=(0, 0, 0), thickness=-1):
"""Draw a boxes on the given image."""
start_point = (5, 5)
end_point = (220, 220)
color = (255, 0, 0)
thickness = 2
# Using cv2.rectangle() method
# Draw a rectangle of thickness of 2 px
new_image = cv2.rectangle(image, start_point, end_point, color, thickness)
# Convert back to PIL format
image_with_boxes = pilImage.fromarray(new_image)
return image_with_boxes
When an image has highlighting in it like this:
[Before Conversion](https://i.sstatic.net
It results in a converted image like this:
enter image description here
Again, this only happens when there is highlighting in the original document. I have tried the following with no success:
image_with_boxes = pilImage.fromarray(np.uint8(new_image * 255))
cv_image_copy = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv_image_copy = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
Ryan Tracy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.