簡體   English   中英

Python:任何不使用 PIL 在圖像上繪制文本的想法

[英]Python : any ideas without using PIL to draw the text on a image

我使用的是 Python 3.3,我已經安裝了(只執行下載的文件 .exe,不需要任何設置?)從下面的鏈接中的 aspell PIL,不幸的是我導入 PIL 是錯誤未解決的 PIL。 為什么?

http://www.lfd.uci.edu/~gohlke/pythonlibs/

import PIL
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
font = ImageFont.truetype("C:/arial.ttf",25)
img=Image.new("RGBA", (200,200),(120,20,20))
draw = ImageDraw.Draw(img)
draw.text((0, 0),"This is a test",(255,255,0),font=font)
draw = ImageDraw.Draw(img)
draw = ImageDraw.Draw(img)
img.save("a_test.png")

或者有什么想法而不使用 PIL 在圖像上繪制文本?

因為它還沒有在 3.3 中發布。

“當前的免費版本是 PIL 1.1.7。此版本支持 Python 1.5.2 及更新版本,包括 2.5 和 2.6。稍后將發布 3.X 版本。” - 取自 PILs 主頁。

如果要使用 PIL,請使用 Python 2.6。 Python 的 3.X 版本對 python 進行了一些更改,因此它不完全向后兼容。

我建議您使用舊版本的 Python 而不是 3.X,除非您有充分的理由使用 3.X。

Pillow是 Python Imaging Library 的延續,該庫已更新為可與 Python 3 一起使用。這應該可以解決您未解決的 PIL 問題。

在最近的 Python 中,存在在圖像上繪制文本的其他選項:

  • Pygame是一個 SDL 1.2 包裝器,從 Pygame 1.9.2 開始就支持 Python 3。
  • Wand是一個基於 ctypes 的 ImageMagick 包裝器。
  • gdmodule包裝了 GD,但它的安裝程序似乎只支持 Linux。 它不會在 Windows 下幫助你(參見"C:/arial.ttf" ),但它可能會幫助其他有同樣問題的人。

使用 opencv

import cv2
font = cv2.FONT_HERSHEY_SIMPLEX 
orgin = (50, 50) 
fontScale = 1
color = (255, 0, 0) 
thickness = 2

使用 cv2.putText() 方法

image = cv2.putText(image, 'OpenCV', org, font, fontScale, color, thickness, cv2.LINE_AA) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM