ImageDraw.text writes through the image borders

杀马特。学长 韩版系。学妹 提交于 2020-01-16 05:33:08

问题


I am trying to write text to an image. (It is part of a script that will hundreds of files and make hundreds of images)

The only problem is that the function I want to use to write text to an image, seems to ignore my string's new lines. I looked at the ImageDraw documentation in the PIL reference, but it does not even touch on this. Others seem to be using this function for small text like signatures and watermarks.

Unfortunately I need to be converting text to an image, as part of a larger Python script.

draw_image.text((0,0), formattedString, font=f)
image.save(open("image.png", "wb"), "PNG")

This above function works, it prints the string onto an image. But only on one long line that extends beyond the borders of the image. If I print this same string out onto the console, it prints with the newlines.

I need insight into a way to judge the edge of the image borders. I also have the inputFile's contents in a list format. So I need a loop through the list

fileContents = file.readlines()
for i in fileContents
    draw_image.text((0,j), i, fill='white')
    j = j + 20 #this is necessary for adequate spacing of lines

So at this point I need a function that knows to wrap the text when it reaches the image border.

来源:https://stackoverflow.com/questions/6061165/imagedraw-text-writes-through-the-image-borders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!