Python provides a lot of useful tools and libraries to handle images.
If you want to modify an image you can use the PIL (Python Image Library) to apply filters, draw a line, draw a rect, draw an oval, draw an image, or draw a text on the image.
In the following few lines of code I’m going to show to you how to add a copyright mark onto your own images.
The file we want to mark is the following:
# Importing the needed library import PIL from PIL import ImageFont from PIL import Image from PIL import ImageDraw # Loading Fonts…. # Note the following line works on Ubuntu 12.04 # On other operating systems you should set the correct path # To the font you want to use. font = ImageFont.truetype("/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-B.ttf",25) # Opening the file gg.png imageFile = "gg.png" im1=Image.open(imageFile) # Drawing the text on the picture draw = ImageDraw.Draw(im1) draw.text((0, 0),"copyright/left xappsoftware.com",(255,255,0),font=font) draw = ImageDraw.Draw(im1) # Save the image with a new name im1.save("marked_image.png")
The program is really simple and self explanatory:
We load the PIL library
We load the image we want to modify
We draw the string onto the image
We save back the image with a different name.
After running this program the file will become as follows:
Gg1
Español:
Hola, muchas gracias por el aporte, me ha funcionado perfectamente el código que has dejado, he probado tantos y por fin este código funcionó.
Gracias nuevamente y saludos!!! 🙂
Inglés:
Hello, thank you very much for the contribution, it has perfectly worked for me the code that you left, I tried so many and so this code worked.
Thanks again and greetings !!! 🙂
Is there any way to change the size of the text?