I have an application that loads an Image and when the user clicks it, a text area appears for this Image (using jquery
), where user can write some text on the
Even more minimal example (draws "Hello world!" in black and with the default font in the top-left of the image):
...
from PIL import ImageDraw
...
ImageDraw.Draw(
image # Image
).text(
(0, 0), # Coordinates
'Hello world!', # Text
(0, 0, 0) # Color
)
First install pillow
pip install pillow
Example
from PIL import Image, ImageDraw, ImageFont
image = Image.open('devnote-open.png')
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('sans-serif.ttf', size=40)
(x, y) = (60, 60)
message = "Happy Birthday!"
color = 'rgb(0, 0, 0)' # font color
draw.text((x, y), message, fill=color, font=font)
(x, y) = (150, 150)
name = 'Devnote'
color = 'rgb(255, 0, 0)' # font color
draw.text((x, y), name, fill=color, font=font)
image.save('devnote.png')
# optional parameters like optimize and quality
image.save('optimized.png', optimize=True, quality=50)
To add text on an image file, just copy/paste the code below
<?php
$source = "images/cer.jpg";
$image = imagecreatefromjpeg($source);
$output = "images/certificate".rand(1,200).".jpg";
$white = imagecolorallocate($image,255,255,255);
$black = imagecolorallocate($image,7,94,94);
$font_size = 30;
$rotation = 0;
$origin_x = 250;
$origin_y = 450;
$font = __DIR__ ."/font/Roboto-Italic.ttf";
$text = "Dummy";
$text1 = imagettftext($image,$font_size,$rotation,$origin_x,$origin_y,$black,$font,$text);
imagejpeg($image,$output,99);
?> <img src="<?php echo $output; ?>"> <a href="<?php echo $output; ?>" download="<?php echo $output; ?>">Download Certificate</a>
With Pillow, you can also draw on an image using the ImageDraw module. You can draw lines, points, ellipses, rectangles, arcs, bitmaps, chords, pieslices, polygons, shapes and text.
from PIL import Image, ImageDraw
blank_image = Image.new('RGBA', (400, 300), 'white')
img_draw = ImageDraw.Draw(blank_image)
img_draw.rectangle((70, 50, 270, 200), outline='red', fill='blue')
img_draw.text((70, 250), 'Hello World', fill='green')
blank_image.save('drawn_image.jpg')
we create an Image object with the new() method. This returns an Image object with no loaded image. We then add a rectangle and some text to the image before saving it.
I think ImageFont module available in PIL
should be helpful in solving text font size problem. Just check what font type and size is appropriate for you and use following function to change font values.
# font = ImageFont.truetype(<font-file>, <font-size>)
# font-file should be present in provided path.
font = ImageFont.truetype("sans-serif.ttf", 16)
So your code will look something similar to:
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
img = Image.open("sample_in.jpg")
draw = ImageDraw.Draw(img)
# font = ImageFont.truetype(<font-file>, <font-size>)
font = ImageFont.truetype("sans-serif.ttf", 16)
# draw.text((x, y),"Sample Text",(r,g,b))
draw.text((0, 0),"Sample Text",(255,255,255),font=font)
img.save('sample-out.jpg')
You might need to put some extra effort to calculate font size. In case you want to change it based on amount of text user has provided in TextArea
.
To add text wrapping (Multiline thing) just take a rough idea of how many characters can come in one line, Then you can probably write a pre-pprocessing function for your Text, Which basically finds the character which will be last in each line and converts white space before this character to new-line.
You can make a directory "fonts" in a root of your project and put your fonts (sans_serif.ttf) file there. Then you can make something like this:
fonts_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'fonts')
font = ImageFont.truetype(os.path.join(fonts_path, 'sans_serif.ttf'), 24)