【问题标题】:insert text on image, python在图像上插入文本,python
【发布时间】:2018-05-15 14:08:43
【问题描述】:

我的项目是关于加载图像,当我点击一个按钮时,图像中会出现一个文本区域,我可以在上面写一个文本,然后保存写在图像和图像上的文本。为此,我使用了tkinter,但我将图像设置为背景并添加了一个文本框(text widget)并输入了文本,但显然我无法保存该图像(设置为背景的图像)和文本写在上面。我尝试使用PIL,但没有找到我想要的东西。 这是我使用tkinter 的代码:

from tkinter import *
from PIL import ImageTk
import cv2

#root = Tk()

image=cv2.imread("New_refImg.png")
width_1, height_1,channels = image.shape   
print(width_1)
print(height_1)

canvas = Canvas(width =height_1, height = width_1, bg = 'blue')
canvas.pack(expand = 1, fill = BOTH)

img = ImageTk.PhotoImage(file = "New_refImg.png")

canvas.create_image(0, 0, image = img, anchor = NW)

#Add text
entry = Entry(canvas, width=12)
entry.pack(side=BOTTOM,padx=43,pady=height_1-130) # "side" position button

def onok():    
    x= entry.get().split('x')
    print(x)

Button(canvas, text='OK', command=onok).pack(side=LEFT)

mainloop()

【问题讨论】:

    标签: python-3.x image python-imaging-library tkinter-canvas


    【解决方案1】:

    因此,您可以在画布中绘制文本 - Python: how to add text inside a canvas? - 然后将该画布转换为图像以进行保存 - How can I convert canvas content to an image?

    但是,我建议使用 PIL/Pillow。您可以在图像上绘制文本 - https://pillow.readthedocs.io/en/5.2.x/reference/ImageDraw.html#example-draw-partial-opacity-text。您可以将 Pillow 图像作为参数直接提供给 ImageTk.PhotoImage - ImageTk.PhotoImage(im)。您可以自然地将图像保存到文件 - http://pillow.readthedocs.io/en/5.2.x/reference/Image.html#PIL.Image.Image.save

    【讨论】:

      猜你喜欢
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 2021-09-02
      相关资源
      最近更新 更多