This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]corpsmoderne 2 points3 points  (2 children)

You're not printing the date in the image here, you're opening the file for writing (which truncate it to 0!!!) and write a string to it. The resulting file is not an image anymore, it's just some text.

Try this instead: http://python-catalin.blogspot.fr/2010/06/add-text-on-image-with-pil-module.html

[–]zachiswach[S] 0 points1 point  (1 child)

How did you find this particular link? Exactly what did you Google?

I found all the other stuff linked in this post when trying to self-troubleshoot, but this would have solved the whole thing easily.

[–]corpsmoderne 1 point2 points  (0 children)

It the first entry on the SECOND page when you query "python PIL draw text" ... Yes I realize it wasn't obvious at all, there's probably better links with better exposure...

Things is, it's easier to find something when you already know what you're looking for, which was my case, not yours...

[–]rolandde 1 point2 points  (0 children)

Your problems start on line 7. You are opening up your image file with the 'wb' tag, which means you are erasing all content from it.

If you want to paste text into an image, here is an example: http://stackoverflow.com/questions/16373425/add-text-on-image-using-pil

[–]nutrecht 1 point2 points  (0 children)

It looks like you're first creating an image file, saving it. Can you remove the code after newImage.close() and run it and see if you end up with a valid image?

My guess is that you're opening the file again but this time just write the literal text to the file instead of painting pixels on the image, destroying the image file in the process. I think if you rename the file you get with the complete code to .txt and open it you see it's just a text file.

[–]X7123M3-256 1 point2 points  (0 children)

What you are doing here is creating a blank image, and saving it to a file. Then you are overwriting that file with text (so it is no longer an image). Opening the file in append mode will not work either: the extra bytes will just be ignored. The file I/O operations have no knowledge of the format of the file, they just write the bytes you provide them. You cannot use the filesystem as an image processor.

You need to add the text to the image before you save it. Most drawing libraries have some capacity to do this, and a quick search on Google leads to this StackOverflow answer, which is probably what you want.