ImageFont的getsize()没有得到正确的文本大小? [英] ImageFont's getsize() does not get correct text size?
问题描述
我使用以下两种方法为.ttf字体文件生成文本预览图像
PIL方法:
$ $ p $
def make_preview(text,fontfile,imagefile,fontsize = 30):
try:
font = ImageFont.truetype fontfile,fontsize)
text_width,text_height = font.getsize(text)
img = Image.new('RGBA',(text_width,text_height))
draw = ImageDraw.Draw(img)
draw.text((0,0),text,font = font,fill =(0,0,0))
返回True
除了:
返回False
ImageMagick方法:
< pre $ def make_preview(text,fontfile,imagefile,fontsize = 30):
p = subprocess.Popen(['convert','-font',fontfile'-background' ,
'transparent','-gravity','center','-pointsize',str(fontsize),
'-trim','+ repage','label:%s'%te xt,image_file])
return p == 0
这两种方法都会创建正确的预览图像的时间,但在一些极少数情况下(<2%),font.getsize(文本)不能得到正确的文本大小,导致文本溢出提供画布。 ImageMagick也有同样的问题。
示例字体和预览: HANFORD.TTF NEWTOW.TTF MILF.TTF SWANSE.TTF 我查看了ImageMagick的文档并在 http://www.imagemagick.org/Usage/text/#overflow 。 是否有可能检测到这样的文本溢出并按照我们的预期绘制文本以符合画布? 在这种情况下,只需指定ImageMagick使用固定字体大小的较大的画布大小,并在保持其完整性的同时在指定的点大小处绘制文本。 如果您需要将文本放入指定的画布而不是使用固定的点大小,则可能需要在创建输出图像后重新调整大小。 I use the following two methods to to generate text preview image for a .ttf font file PIL method: ImageMagick method: Both methods create correct preview images most of time but in some rare cases (<2%), the font.getsize(text) just cannot get the correct text size which result in text overflowed provided canvas. ImageMagick has same problem. Sample fonts and previews: HANFORD.TTF
http://download.appfile.com/HANFORD.png NEWTOW.TTF
http://download.appfile.com/NEWTOW.png MILF.TTF
http://download.appfile.com/MILF.png SWANSE.TTF
http://download.appfile.com/SWANSE.png I have looked into ImageMagick's documentations and found the explanation of this problem at http://www.imagemagick.org/Usage/text/#overflow. Is it possible to detect such text overflows and draw text to fit the canvas as we expected? In this case, just specify ImageMagick to use a larger canvas size with a fixed font size and it will draw text at specified point size while keeping its integrity. If you need to fit text into specified canvas rather than using a fixed point size, you may need to resize the output image after it's created. PIL doesn't do this very well drawing exotic fonts, no matter what point size you specify to load a font, it always overflows text outside output image. 这篇关于ImageFont的getsize()没有得到正确的文本大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
http://download.appfile.com/HANFORD.png
http://download.appfile.com/NEWTOW.png
http:// download。 appfile.com/MILF.png
http://download.appfile.com/SWANSE.png
def make_preview(text,fontfile,imagefile,fontsize = 30):
p = subprocess.call(['convert','-font',fontfile,'-background',
'透明','重力','中心','尺寸','1500x300',
'-pointsize',str(字体大小),'-trim','+ repage','label:%s '%text,image_file])
return p == 0
PIL不能很好地绘制奇特的字体,不管你指定加载字体的大小是多少,它总是会溢出输出图像之外的文本。 > def make_preview(text, fontfile, imagefile, fontsize=30):
try:
font = ImageFont.truetype(fontfile, fontsize)
text_width, text_height = font.getsize(text)
img = Image.new('RGBA', (text_width, text_height))
draw = ImageDraw.Draw(img)
draw.text((0, 0), text, font=font, fill=(0, 0, 0))
return True
except:
return False
def make_preview(text, fontfile, imagefile, fontsize=30):
p = subprocess.Popen(['convert', '-font', fontfile, '-background',
'transparent', '-gravity', 'center', '-pointsize', str(fontsize),
'-trim', '+repage', 'label:%s' % text, image_file])
return p==0
def make_preview(text, fontfile, imagefile, fontsize=30):
p = subprocess.call(['convert', '-font', fontfile, '-background',
'transparent', '-gravity', 'center', '-size', '1500x300',
'-pointsize', str(fontsize), '-trim', '+repage', 'label:%s' % text, image_file])
return p==0