Skip to content

Commit 99df5f7

Browse files
committed
Image Processing -Drawing Text and shapes on image using python
1 parent 487d766 commit 99df5f7

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from PIL import Image, ImageDraw
2+
3+
im = Image.new(
4+
"RGBA", (200, 200), "white"
5+
) # or open an image using "Image.open(in_file)"
6+
draw = ImageDraw.Draw(im)
7+
draw.line(
8+
[(0, 0), (199, 0), (199, 199), (0, 199), (0, 0)], fill="black"
9+
) # line(xy, fill, width) for line
10+
draw.rectangle(
11+
(20, 30, 60, 60), fill="blue"
12+
) # rectangle(xy, fill, outline) for rectangle
13+
draw.ellipse((120, 30, 160, 60), fill="red") # ellipse(xy, fill, outline) for ellipse
14+
draw.polygon(
15+
((57, 87), (79, 62), (94, 85), (120, 90), (103, 113)), # polygon(xy, fill, outline)
16+
fill="brown",
17+
)
18+
for i in range(100, 200, 10):
19+
draw.line([(i, 0), (200, i - 100)], fill="green")
20+
im.save("drawing.png")
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from PIL import Image
2+
from PIL import ImageFont
3+
from PIL import ImageDraw
4+
import sys
5+
6+
in_file, out_file, text = sys.argv[1:]
7+
8+
img = Image.open(in_file)
9+
draw = ImageDraw.Draw(img)
10+
font = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 20)
11+
draw.text((0, 0), text, (255, 255, 255), font=font)
12+
img.save(out_file)

0 commit comments

Comments
 (0)