问题
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