This article was originally published in: Walker AI

In our work, we will meet the requirements of adding text and random codes to images in batches. When the number of data codes is large, the WORKLOAD of UI will be very heavy. At this time, we can use Python to help us improve work efficiency.

1. Requirements analysis

We have an image, and we need to add a bunch of random codes to the fixed position of the image.

2. The method of OpenCV

Adding text to an image looks easy, but using OpenCV to do so is a hassle. There is no function to use custom font files in OpenCV, which not only means we can’t use our own fonts, but it also means it can’t display Chinese characters. It’s still a very deadly matter. And the position of the text it displays is not easy to control, as in the following code:

import cv2
import numpy as np
from pylab import *
Use the default font
font=cv2.FONT_HERSHEY_SIMPLEX
# Create a new image
im=np.zeros((50.50.3),np.uint8)
# add text, 1.2 for font size, (0,40) for initial position, (255,255,255) for color, and 2 for thickness
img=cv2.putText(im,'walker AI', (0.40),font,1.2, (255.255.255),2)
imshow(img)
Copy the code

In practice, it is not easy to grasp the position of the text. By default, the initial coordinate is the coordinate in the lower left corner, and it is not convenient to grasp the actual position and size of the text after it is displayed. But it also has the convenience that we can change its thickness at will without changing the font.

3. The PIL method

PIL method can be used for more detailed configuration of font, color and other parameters.

from PIL import Image, ImageDraw, ImageFont
Open the original image
image = Image.open(filepath)
# Create a drawing object
draw = ImageDraw.Draw(image)
image.show()
# Configurable font size color
draw.text(size, text, font=setFont, fill=fillColor, direction=None)
# save file
pic_save(image, filename)
Copy the code

We can find that PIL supports the use of custom font files, and can provide detailed information about the font position, we can accurately determine the text position, especially useful in the application. The only downside is that he can’t change the font thickness (it’s a template, after all).

4. PyPDF2 method

PIL method is found to be more convenient after use, which can quickly determine the position of text, but we need to print the picture, which involves the problem of clarity. Neither of the two methods can guarantee the picture clarity and the UI design draft is consistent, so we choose to directly install the UI design draft into PDF format. Then use pYPDF to add text to the PDF to ensure clarity. The code is as follows:

from PyPDF2 import PdfFileWriter, PdfFileReader
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch, mm
import io

packet = io.BytesIO()
Create a new PDF using Reportlab
can = canvas.Canvas(packet, pagesize=letter)
Set the font and size
can.setFont(font, size)
# set color
can.setFillColorRGB(1.1.1)
can.rotate(rotate)
# More color configurations
# can. SetStrokeColorRGB (0.2, 0.5, 0.3)
# can.setFillColorRGB(255, 20, 147)
# can.setStrokeColorRGB(220, 20, 60)

can.drawString(x * mm, y * mm, text) # RTL
can.showPage()
can.save()
# buffer starts at offset 0
packet.seek(0)
new_pdf = PdfFileReader(packet)
Read the existing PDF
existing_pdf = PdfFileReader(open(old_pdf, "rb"))
output = PdfFileWriter()
Operate on the first page of the PDF
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
# Finally, write the PDF of the target
outputStream = open(out_new_pdf, "wb")
output.write(outputStream)
outputStream.close()
Copy the code

5. To summarize

PIL is more convenient if the image definition is not high, and PyPDF2 is recommended if high definition printing is required.


PS: more dry technology, pay attention to the public, | xingzhe_ai 】, and walker to discuss together!