Làm cách nào để vẽ một hình ảnh bằng Python?

Hình dạng hình học là một hình bao gồm hai chiều hoặc ba chiều có các thuộc tính cụ thể như chiều dài, chiều rộng và chiều cao. Các hình dạng hình học là cơ sở cho các hình và cấu trúc phức tạp hơn trong hình học và thường được sử dụng trong cuộc sống hàng ngày để mô tả và phân tích thế giới xung quanh chúng ta

Ví dụ về hình dạng hình học bao gồm Hình vuông, Hình tam giác, Hình tròn, Hình chữ nhật, Hình lục giác, Hình ngũ giác, Hình bát giác, Hình thập giác, Hình bình hành, Hình elip, Hình bầu dục, Hình thoi, Hình thang, Cánh diều, Ngôi sao, Hình nón, Hình trụ, Hình cầu, Khối lập phương, Hình khối, Lăng kính, Kim tự tháp, Hình xuyến

PIL là Thư viện hình ảnh Python cung cấp trình thông dịch python với khả năng chỉnh sửa hình ảnh. Mô-đun ImageDraw cung cấp đồ họa 2D đơn giản cho các đối tượng Hình ảnh. Bạn có thể sử dụng mô-đun này để tạo hình ảnh mới, chú thích hoặc chỉnh sửa hình ảnh hiện có và tạo đồ họa nhanh chóng để sử dụng trên web

________ 10 Vẽ hình chữ nhật

Cú pháp. PIL. hình ảnhvẽ. Vẽ tranh. hình chữ nhật[xy, fill=None, outline=None]
Thông số.

xy – Bốn điểm để xác định hộp giới hạn. Chuỗi của [[x0, y0], [x1, y1]] hoặc [x0, y0, x1, y1]. Điểm thứ hai nằm ngay bên ngoài hình chữ nhật đã vẽ.
đường viền – Màu để sử dụng cho đường viền.
fill – Màu dùng để tô.

trả lại. Một đối tượng Image trong hình chữ nhật




#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
1

#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
2

#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
3

#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
4
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
5

#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
6
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
7
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
4
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
00

#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
2

#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
02
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
03
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
04
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
05
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
06

#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
07
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
03
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
09_______110
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
05
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
10
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
13
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
14
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
15
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
16
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
14
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
15
#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]
19

Mô-đun 'ImageDraw' cung cấp hỗ trợ đồ họa 2D đơn giản cho Đối tượng hình ảnh. Nói chung, chúng tôi sử dụng mô-đun này để tạo hình ảnh mới, chú thích hoặc chỉnh sửa hình ảnh hiện có và tạo đồ họa nhanh chóng để sử dụng trên web

Các lệnh đồ họa hỗ trợ vẽ hình và chú thích văn bản

  • Một hình ảnh có thể được coi là một mảng pixel hai chiều [các phần tử hình ảnh]. Pixel là chấm màu nhỏ nhất được hỗ trợ

  • Nguồn gốc của hệ tọa độ hai chiều được sử dụng bởi ImageDraw, nằm ở góc trên bên trái của hình ảnh

  • Phối màu gối chúng tôi sử dụng là RGB. Sự thể hiện và hỗ trợ màu RGB được cung cấp bởi mô-đun ImageColor

  • bitmap, OpenType hoặc TrueType là những phông chữ được chấp nhận cho chú thích văn bản

  • Hầu hết các lệnh vẽ có thể yêu cầu tham số hộp giới hạn chỉ định khu vực trên hình ảnh mà lệnh sẽ được áp dụng

  • Một dãy tọa độ có thể được biểu diễn dưới dạng [ [x0, y0], [x1, y1],…[xn, yn]]

  • Đối với một số lệnh vẽ, chúng tôi yêu cầu các giá trị góc

Ví dụ

Ví dụ về python sau đây vẽ một đường ngang qua hình ảnh đã cho -

#Import required libraries
import sys
from PIL import Image, ImageDraw

#Create Image object
im = Image.open["images/logo.jpg"]

#Draw line
draw = ImageDraw.Draw[im]
draw.line[[0, 0] + im.size, fill=128]
draw.line[[0, im.size[1], im.size[0], 0], fill=128]

#Show image
im.show[]

đầu ra

Nếu bạn lưu chương trình trên dưới dạng Ví dụ. py và thực thi, nó vẽ một đường ngang qua hình ảnh và hiển thị nó bằng tiện ích hiển thị PNG tiêu chuẩn, như sau -

Tranh sơn dầu

  • ImageDraw là một bề mặt có thể vẽ được bằng Gối [i. e. , canvas] của một Hình ảnh

  • hình ảnhvẽ. Draw[img] trả về một biểu diễn canvas có thể vẽ được của tham số Hình ảnh img. Nền của canvas là hình ảnh "img"

Ví dụ

Ví dụ về python sau đây vẽ văn bản trên hình ảnh đã cho -

#Import required modules from Pillow package
from PIL import Image, ImageDraw, ImageFont

# get an image
base = Image.open['images/boy.jpg'].convert['RGBA']

# make a blank image for the text, initialized to transparent text color
txt = Image.new['RGBA', base.size, [255,255,255,0]]

# get a font
fnt = ImageFont.truetype['E:/PythonPillow/Fonts/Pacifico.ttf', 40]

# get a drawing context
d = ImageDraw.Draw[txt]

# draw text, half opacity
d.text[[14,14], "Tutorials", font=fnt, fill=[255,255,255,128]]

# draw text, full opacity
d.text[[14,60], "Point", font=fnt, fill=[255,255,255,255]]
out = Image.alpha_composite[base, txt]

#Show image
out.show[]

đầu ra

Vẽ hình bằng mô-đun 'ImageDraw'

Mô-đun ImageDraw cho phép chúng tôi tạo các hình dạng khác nhau bằng cách trước tiên tạo một đối tượng vẽ với hình ảnh bạn muốn làm việc và sau đó áp dụng nó. Một số hình dạng phổ biến mà chúng ta có thể vẽ bằng mô-đun ‘ImageDraw’ như sau –

Dòng

Sau đây là cú pháp để vẽ một đường bằng gối python -

draw.line[xy, fill=None, width=0]

Phương thức line[] vẽ một đường từ góc trên bên trái xuống góc dưới bên phải của hộp giới hạn xy và canvas. Dòng được tô bằng cách tô màu. Các giá trị mặc định của Không có và 0 tương ứng dành cho các tham số điền và chiều rộng là tùy chọn

Ví dụ

from PIL import Image, ImageDraw

img = Image.new['RGB', [500, 300], [125, 125, 125]]
draw = ImageDraw.Draw[img]
draw.line[[200, 100, 300, 200], fill=[0, 0, 0], width=10]

img.show[]

đầu ra

nhật thực

Sau đây là cú pháp để vẽ một hình elip bằng gối python -

draw.ellipse[xy, fill=None, outline=None]

Phương thức ellipse[] vẽ hình elip được bao quanh bởi hộp giới hạn xy khi vẽ. Hình dạng được tô bằng cách sử dụng màu tô và chu vi trong đường viền màu. Các giá trị mặc định của Không dành cho các tham số điền và chiều rộng là tùy chọn

Ví dụ

from PIL import Image, ImageDraw

img = Image.new['RGB', [500, 300], [125, 125, 125]]
draw = ImageDraw.Draw[img]

draw.ellipse[[200, 125, 300, 200], fill=[255, 0, 0], outline=[0, 0, 0]]
img.show[]

đầu ra

hình chữ nhật

Sau đây là cú pháp để vẽ một hình chữ nhật bằng gối python -

draw.rectangle[xy, fill=None, outline=None]

Phương thức hình chữ nhật [] vẽ hình chữ nhật cho hộp giới hạn xy trên bản vẽ. Hình dạng được tô bằng cách sử dụng màu tô và chu vi trong đường viền màu. Các giá trị mặc định của Không dành cho các tham số điền và chiều rộng là tùy chọn

from PIL import Image, ImageDraw

img = Image.new['RGB', [500, 300], [125, 125, 125]]
draw = ImageDraw.Draw[img]

draw.rectangle[
   [200, 125, 300, 200],
   fill=[255, 0, 0],
   outline=[0, 0, 0]]
img.show[]

đầu ra

đa giác

Sau đây là cú pháp để vẽ một hình chữ nhật bằng gối python -

________số 8

Phương thức đa giác [] vẽ một đa giác kết nối với các đường thẳng các vị trí chuỗi tọa độ seq khi vẽ. Các tọa độ đầu tiên và cuối cùng trong seq cũng được kết nối bằng một đường thẳng. Hình dạng được tô bằng cách sử dụng màu tô và chu vi trong đường viền màu. Các tham số điền và phác thảo là tùy chọn với các giá trị mặc định Không có

Chúng ta có thể vẽ hình ảnh bằng Python không?

Chúng ta có thể vẽ các hình và số liệu trên Hình ảnh bằng phương thức Draw bằng cách tạo đối tượng Draw trước tiên .

Làm cách nào để vẽ đồ họa bằng Python?

Tạo đồ họa trong Python bao gồm một số bước cơ bản và cố định. .
Nhập tất cả các thư viện từ Lớp đồ họa
Tạo một đối tượng Cửa sổ
Vẽ các đối tượng trong cửa sổ đã tạo
Đóng cửa sổ

Làm cách nào để vẽ đối tượng trong Python?

Lập trình hướng đối tượng trong Python. Tạo trò chơi phiêu lưu của riêng bạn .
từ nhập hình dạng Giấy, Hình tam giác, Hình chữ nhật, Hình bầu dục
giấy = Giấy[]
rect1 = Hình chữ nhật[]
trực tràng1. set_width[200] rect1. set_height[100] rect1. .
trực tràng1. vẽ tranh[]
giấy. trưng bày[]
# đưa code tạo Rectangle thứ 2 vào đây. trưng bày[]

Bạn có thể hiển thị ảnh bằng Python không?

Nếu bạn đang tìm kiếm một thư viện giúp bạn hiển thị hình ảnh trong Python, bạn thật may mắn. Python có nhiều thư viện khác nhau có thể dùng để hiển thị hình ảnh . Ba trong số các thư viện phổ biến nhất là Gối, Matplotlib và OpenCV.

Chủ Đề