PDF:
from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=letter)
can.drawString(148, 345, "made in china")
can.save()
packet.seek(0)
new_pdf = PdfFileReader(packet)
existing_pdf = PdfFileReader(open("1.pdf", "rb"))
output = PdfFileWriter()
page = existing_pdf.getPage(0)
page2 = new_pdf.getPage(0)
page.mergePage(page2)
output.addPage(page)
outputStream = open("2.pdf", "wb")
output.write(outputStream)
outputStream.close()
图片:
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
base = Image.open('1.png').convert('RGBA')
txt = Image.new('RGBA', base.size, (255, 255, 255, 0))
fnt = ImageFont.truetype("ship_api/Arial.ttf", 40)
draw = ImageDraw.Draw(txt)
fillcolor = "#000000" # 字体颜色
draw.text((280, 1145), "made in china", font=fnt, fill=fillcolor)
Image.alpha_composite(base, txt).save('2.png')