8

I know how to do this with OpenCV and PIL. I can't use OpenCV in this project and if I use PIL I have to convert in between image PIL Image and numpy array. I don't want to do that. I'm already using skimage so...

How do I write text on top of an image using skimage?

I've looked at the skimage draw functions, but they seem to only handle shapes and lines not text. Maybe I'm searching the wrong words, but I don't see anything in the docs.

noel
  • 2,257
  • 3
  • 24
  • 39
  • "if I use PIL I have to convert in between image PIL Image and numpy array. I don't want to do that." Why? Usually, a view to the memory location is used and no data is copied: https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.fromarray – moi Feb 06 '23 at 12:56
  • Oh, apparently, I'm wrong: https://uploadcare.com/blog/fast-import-of-pillow-images-to-numpy-opencv-arrays/ Still, you could use PIL to generate a text image and paste that into the existing one using skimage/numpy. This way, you at least avoid copying the original image. – moi Feb 06 '23 at 13:30

2 Answers2

2
%matplotlib inline
from skimage.draw import *
import numpy as np
import matplotlib.pyplot as plt 
rr, cc = rectangle(start=(0, 0), extent=(1, 4))
img = np.zeros((5, 5, 3), dtype=np.int32)
img[rr, cc, 0] = 0
img[rr, cc, 1] = 255
img[rr, cc, 2] = 255
plt.text(2, 2, "some text", dict(color='red', va='center', ha='center'))
plt.imshow(img)
plt.axis('off')

You can use matplotlib to draw text, since scikit-image is also using matplotlib as backend

Sanhu Li
  • 402
  • 5
  • 11
0

I agree with Sarim. You should use opencv here

import cv2
font = cv2.FONT_HERSHEY_SIMPLEX

#img is your image in below line
cv2.putText(img,'Text on image goes here',(10,500), font, 1,(255,255,255),2)
#Display the image
cv2.imshow("img",img)

cv2.waitKey(0)
Jimmy
  • 37
  • 4