Làm thế nào để bạn vẽ một đối tượng trong python?

Python đi kèm với một mô-đun cho Rùa. Nó bao gồm một màn hình phác thảo bằng bìa cứng và một con rùa (bút). Để tạo bất cứ thứ gì trên màn hình, hãy di chuyển con rùa (bút). Các hoạt động khác, chẳng hạn như tiến () và lùi (), có thể di chuyển rùa

Về điều này, chúng ta có thể vẽ bằng các kỹ thuật vẽ khác nhau giống như trên bảng vẽ. Các lệnh cơ bản quản lý chuyển động thực tế của bút phác thảo. Bắt đầu với một chương trình rất đơn giản, chúng tôi chuyển sang sử dụng Python Turtle để tạo một đối tượng chuyển động

Di chuyển đối tượng hoặc hoạt ảnh

Về cơ bản, tất cả hoạt ảnh (và tất cả ảnh chuyển động) chỉ là một loạt ảnh được hiển thị đủ nhanh để tạo ảo giác về chuyển động liên tục. Mỗi hình ảnh được gọi là một khung. Từ "hoạt hình" như trong "làm sống động" đề cập đến quá trình thay đổi từng khung hình một cách đáng kể so với khung hình trước đó và nhanh chóng hiển thị chúng lần lượt để tạo ra sự xuất hiện của chuyển động liên tục

Mặc dù có một phương pháp đơn giản hơn, nhưng chúng tôi đã không bắt đầu theo cách đó bởi vì tốt hơn hết là bạn nên tìm hiểu những nền tảng ngay khi có thể. Phương pháp đơn giản hơn là sử dụng hình ảnh làm hình con rùa thay vì tự vẽ hình

Ghi chú. Tốt hơn là sử dụng tĩnh. ảnh gif thay vì ảnh động vì rùa sẽ chỉ sử dụng khung hình đầu tiên và bỏ qua các khung hình khác

Đối tượng chuyển động - Một quả bóng

Phương pháp được sử dụng

Nhập gói rùa từ thư viện Python để sử dụng nó trong dự án của chúng tôi

Sau đó, chúng tôi đã tạo một phương thức gọi là mov_obj() để di chuyển đối tượng của chúng tôi. Điều này bao gồm tô màu cho quả bóng bằng phương thức fillcolor(), thiết lập bán kính của hình tròn của quả bóng bằng phương thức circle() và hướng dẫn bắt đầu và kết thúc tô màu bằng các phương thức begin_fill() và end_fill()

Sau đó, chúng tôi đã tạo một đối tượng màn hình nơi chúng tôi sẽ vẽ đối tượng được yêu cầu bằng phương thức Screen(). Sau đó, chúng tôi thiết lập kích thước của màn hình bằng phương thức setup(), đặt màu của màn hình bằng phương thức bgcolor() và cập nhật màn hình bằng phương thức tracer()

Sau đó chúng ta tạo đối tượng con rùa để tạo bút vẽ. Chúng tôi cũng đặt màu của đối tượng - quả bóng, sử dụng phương thức color(). Chúng tôi cũng đặt tốc độ của rùa bằng phương thức speed(). Chúng tôi cũng đặt chiều rộng cho con rùa bằng phương thức width() và cuối cùng ẩn nó bằng phương thức hideturtle()

Tiếp theo, chúng tôi nhấc bút rùa bằng phương pháp penup() và di chuyển đến tọa độ (-350,0) bằng phương pháp goto(), nơi chúng tôi sử dụng phương pháp pendown() để thả nó xuống

Cuối cùng, chúng tôi đã sử dụng vòng lặp while để di chuyển đối tượng được tạo cho đến bây giờ. Chúng tôi đã xóa màn hình bằng cách sử dụng phương thức clear() cho chuyển động và gọi hàm move_obj() để thực thi nó và bắt đầu di chuyển đối tượng đã tạo. Một lần nữa, xóa màn hình như đã thực hiện trước đó và sau đó thiết lập chuyển động tịnh tiến của quả bóng/đối tượng bằng phương thức forward()

Như vậy là đã hoàn thành code và thực thi nó

Hoàn thành mã

đầu ra

Ảnh chụp màn hình của đầu ra được tạo ra được đính kèm bên dưới để tham khảo

Làm thế nào để bạn vẽ một đối tượng trong python?

Làm thế nào để bạn vẽ một đối tượng trong python?

Làm thế nào để bạn vẽ một đối tượng trong python?

Đối tượng chuyển động - Một quả bóng (lên xuống)

Mật mã để di chuyển quả bóng từ đường chéo, tôi. e. từ góc trên bên trái sang góc dưới bên phải vẫn tương tự như di chuyển một quả bóng từ trái sang phải, chỉ là tọa độ được thay đổi và thêm một góc rẽ phải 45 độ để di chuyển về phía trước theo đường chéo. Sau đây là những bổ sung được thực hiện

Gối cung cấp một mô-đun vẽ có tên là

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
5 mà bạn có thể sử dụng để tạo đồ họa 2D đơn giản trên các đối tượng
# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
6 của mình. Theo tài liệu của Gối, "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. "

Nếu bạn cần các khả năng vẽ nâng cao hơn những gì có trong Gối, bạn có thể nhận một gói riêng gọi là aggdraw

Bạn sẽ tập trung vào những gì đi kèm với Gối trong bài viết này. Cụ thể, bạn sẽ tìm hiểu về những điều sau đây

  • Thông số chung
  • vẽ đường
  • Vẽ vòng cung
  • Vẽ hợp âm
  • Vẽ hình elip
  • Vẽ lát bánh
  • Vẽ đa giác
  • Vẽ hình chữ nhật

Khi vẽ bằng Gối, nó sử dụng cùng một hệ tọa độ mà bạn đã sử dụng với phần còn lại của Gối. Góc trên bên trái vẫn là (0,0) chẳng hạn. Nếu bạn vẽ ra ngoài giới hạn hình ảnh, những pixel đó sẽ bị loại bỏ

Nếu bạn muốn chỉ định một màu, bạn có thể sử dụng một dãy số hoặc bộ dữ liệu như khi sử dụng

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
7. Đối với hình ảnh “1”, “L” và “I”, hãy sử dụng số nguyên. Đối với hình ảnh “RGB”, hãy sử dụng bộ 3 chứa các giá trị số nguyên. Bạn cũng có thể sử dụng tên màu được Gối hỗ trợ mà bạn đã học ở chương 2

Thông số chung

Khi bạn sử dụng các phương pháp vẽ khác nhau, bạn sẽ phát hiện ra rằng chúng có rất nhiều tham số chung mà chúng chia sẻ. Thay vì giải thích các thông số giống nhau trong mọi phần, bạn sẽ tìm hiểu về chúng ngay từ đầu

xy

Hầu hết các phương thức vẽ đều có tham số

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
8 đặt vùng hình chữ nhật để vẽ hình trong đó. Điều này có thể được định nghĩa theo hai cách sau

  • ((phía trên bên trái x, phía trên bên trái y), (phía dưới bên phải x, phía dưới bên phải y)) hoặc đơn giản ((x1, y1), (x2, y2))
  • Bộ hộp của (x1, y1, x2, y2)

Khi cần vẽ một đường thẳng, đa giác hoặc điểm, nhiều tọa độ được chỉ định theo một trong hai cách này

  • # draw_line.py
    
    import random
    from PIL import Image, ImageDraw
    
    
    def line(image_path, output_path):
        image = Image.open(image_path)
        draw = ImageDraw.Draw(image)
        colors = ["red", "green", "blue", "yellow",
                  "purple", "orange"]
    
        for i in range(0, 100, 20):
            draw.line((i, 0) + image.size, width=5, 
                      fill=random.choice(colors))
    
        image.save(output_path)
    
    if __name__ == "__main__":
        line("madison_county_bridge_2.jpg", "lines.jpg")
    9
  • # draw_jointed_line.py
    
    from PIL import Image, ImageDraw
    
    
    def line(output_path):
        image = Image.new("RGB", (400, 400), "red")
        points = [(100, 100), (150, 200), (200, 50), (400, 400)]
        draw = ImageDraw.Draw(image)
        draw.line(points, width=15, fill="green", joint="curve")
        image.save(output_path)
    
    if __name__ == "__main__":
        line("jointed_lines.jpg")
    0

Phương thức

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
1 sẽ vẽ một đường thẳng, nối từng điểm.
# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
2 sẽ vẽ một đa giác trong đó mỗi điểm được kết nối. Cuối cùng,
# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
3 sẽ vẽ một điểm 1 pixel tại mỗi điểm

lấp đầy

Tham số,

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
4, được sử dụng để đặt màu tô cho hình dạng. Cách bạn đặt
# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
4 được xác định bởi chế độ của hình ảnh

  • # draw_jointed_line.py
    
    from PIL import Image, ImageDraw
    
    
    def line(output_path):
        image = Image.new("RGB", (400, 400), "red")
        points = [(100, 100), (150, 200), (200, 50), (400, 400)]
        draw = ImageDraw.Draw(image)
        draw.line(points, width=15, fill="green", joint="curve")
        image.save(output_path)
    
    if __name__ == "__main__":
        line("jointed_lines.jpg")
    6. Đặt từng giá trị màu (0-255) bằng cách sử dụng (R, G, B) hoặc tên màu
  • # draw_jointed_line.py
    
    from PIL import Image, ImageDraw
    
    
    def line(output_path):
        image = Image.new("RGB", (400, 400), "red")
        points = [(100, 100), (150, 200), (200, 50), (400, 400)]
        draw = ImageDraw.Draw(image)
        draw.line(points, width=15, fill="green", joint="curve")
        image.save(output_path)
    
    if __name__ == "__main__":
        line("jointed_lines.jpg")
    7 (thang độ xám). Đặt giá trị (0-255) làm số nguyên

Mặc định là

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
8 hoặc không điền

đề cương

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
9 đặt màu đường viền cho bản vẽ của bạn. Thông số kỹ thuật của nó giống như thông số bạn sử dụng cho
# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
4

Giá trị mặc định là

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
8, có nghĩa là không có đường viền

Bây giờ bạn đã biết về các tham số phổ biến, bạn có thể tiếp tục và tìm hiểu cách bắt đầu vẽ

vẽ đường

Kiểu vẽ đầu tiên bạn sẽ học là cách vẽ các đường thẳng trong Gối. Tất cả các hình dạng được tạo thành từ các đường. Trong trường hợp của Gối, một đường được vẽ bằng cách cho Gối biết tọa độ đầu và cuối để vẽ đường giữa. Ngoài ra, bạn có thể chuyển vào một loạt tọa độ XY và Gối sẽ vẽ các đường để kết nối các điểm

Sau đây là định nghĩa phương thức

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
1

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
8

Bạn có thể thấy rằng nó chấp nhận một số tham số khác nhau. Bạn đã biết ý nghĩa của một số tham số này trong phần trước. Tham số

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
93 được sử dụng để kiểm soát độ rộng của các dòng

Trước khi học cách sử dụng

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
94, bạn nên học cách vẽ các đường mà không có nó. Nhưng trước tiên, bạn sẽ cần một hình ảnh để vẽ lên. Bạn sẽ sử dụng hình ảnh này của một trong những cây cầu của Quận Madison

Làm thế nào để bạn vẽ một đối tượng trong python?

Cầu có mái che Quận Madison

Bây giờ, hãy mở trình chỉnh sửa Python của bạn và tạo một tệp mới có tên

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
95  rồi thêm mã này vào đó

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")

Tại đây, bạn mở hình ảnh trong Gối và sau đó chuyển đối tượng

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
6 cho
# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
97, đối tượng này trả về đối tượng
# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
5. Bây giờ bạn có thể vẽ các đường trên hình ảnh của mình. Trong trường hợp này, bạn sử dụng vòng lặp
# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
99 để vẽ năm đường trên hình ảnh. Hình ảnh bắt đầu bắt đầu tại (0,0) trong vòng lặp đầu tiên. Sau đó, vị trí X thay đổi trong mỗi lần lặp. Điểm cuối là kích thước của hình ảnh

Bạn sử dụng mô-đun

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
70 để chọn một màu ngẫu nhiên từ danh sách các màu. Khi bạn chạy mã này, đầu ra sẽ giống như thế này

Làm thế nào để bạn vẽ một đối tượng trong python?

Các đường được vẽ trên một hình ảnh

Bây giờ bạn có thể thử tạo một loạt các điểm và vẽ các đường theo cách đó. Tạo một tệp mới có tên

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
71 và đặt mã này vào tệp của bạn

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")

Lần này bạn tạo một hình ảnh bằng Gối thay vì vẽ trên một hình ảnh của riêng bạn. Sau đó, bạn tạo một danh sách các điểm. Để làm cho các đường kết nối trông đẹp hơn, bạn có thể đặt tham số

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
94 thành "curve". Nếu bạn xem mã nguồn của phương thức
# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
1, bạn sẽ thấy rằng "đường cong" là giá trị hợp lệ duy nhất để cung cấp cho nó ngoài
# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
8. Điều này có thể thay đổi trong phiên bản Gối trong tương lai

Khi bạn chạy mã này, hình ảnh của bạn sẽ trông như thế này

Làm thế nào để bạn vẽ một đối tượng trong python?

Vẽ các đường nối

Bây giờ hãy thử xóa tham số

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
94 khỏi mã của bạn và chạy lại ví dụ. Đầu ra của bạn bây giờ sẽ trông như thế này

Làm thế nào để bạn vẽ một đối tượng trong python?

Đường không khớp

Bằng cách đặt

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
94 thành "đường cong", đầu ra sẽ dễ nhìn hơn một chút

Bây giờ bạn đã sẵn sàng tìm hiểu về cách vẽ các cung bằng Gối

Vẽ vòng cung

Một cung là một đường cong. Bạn có thể vẽ các vòng cung với Pillow top. Đây là đặc tả phương pháp

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
77

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
9

Một

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
77 cũng có thể được thực hiện bằng cách sử dụng các điểm
# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
8. Tham số
# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
90 xác định góc bắt đầu, tính bằng độ. Tham số
# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
91 cho Gối biết góc kết thúc là gì, góc này cũng tính bằng độ. Hai tham số khác là những tham số đã được giới thiệu

Để xem bạn có thể vẽ một cung như thế nào, hãy tạo một tệp mới có tên là

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
92 và thêm mã này vào đó

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
7

Trong mã này, bạn tạo một hình ảnh mới với nền trắng. Sau đó, bạn tạo đối tượng

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
93 của mình. Tiếp theo, bạn tạo hai vòng cung khác nhau. Vòng cung đầu tiên sẽ được lấp đầy bằng màu xanh lá cây. Vòng cung thứ hai sẽ được tô màu vàng, nhưng chiều rộng đường của nó sẽ là 5. Khi bạn vẽ một cung, phần tô có nghĩa là màu đường của cung. Bạn không tự lấp đầy vòng cung

Khi bạn chạy mã này, hình ảnh đầu ra của bạn sẽ trông như thế này

Làm thế nào để bạn vẽ một đối tượng trong python?

Vẽ vòng cung

Hãy thử thay đổi một số tham số và chạy lại mã để xem cách bạn có thể tự thay đổi các cung

Bây giờ chúng ta hãy tiếp tục và tìm hiểu về cách vẽ hợp âm

Vẽ hợp âm

Gối cũng hỗ trợ khái niệm về hợp âm. Một hợp âm giống như một cung ngoại trừ các điểm cuối được kết nối với một đường thẳng

Đây là định nghĩa phương thức của

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
94

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
9

Sự khác biệt duy nhất ở đây là bạn cũng có thể thêm một màu

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
9. Màu này có thể được chỉ định theo bất kỳ cách nào mà bạn có thể chỉ định màu
# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
4

Tạo một tệp mới và đặt tên là

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
97. Sau đó, thêm mã này để bạn có thể xem cách bạn tự tạo hợp âm

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
9

Ví dụ này sẽ vẽ hai hợp âm trên một hình ảnh màu xanh lá cây. Hợp âm đầu tiên được tô màu đỏ. Hợp âm thứ hai được tô màu vàng nhưng được viền màu xanh lam. Đường viền màu xanh có chiều rộng là 5

Khi bạn chạy mã này, bạn sẽ tạo ra hình ảnh sau

Làm thế nào để bạn vẽ một đối tượng trong python?

Vẽ hợp âm

Điều đó có vẻ khá tốt. Hãy tiếp tục và chơi xung quanh với ví dụ này. Bạn sẽ sớm thành thạo việc tạo hợp âm với Gối với một chút luyện tập

Bây giờ chúng ta hãy tiếp tục và tìm hiểu về cách vẽ hình elip

Vẽ hình elip

Một hình elip hoặc hình bầu dục được vẽ trong Gối bằng cách đặt cho nó một hộp giới hạn (xy). Bạn đã thấy điều này vài lần khác trong các phần trước

Đây là định nghĩa phương thức

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
98

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
1

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
98 cho phép bạn tô màu, thêm viền màu (
# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
9) và thay đổi
# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
93 của
# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
9 đó

Để xem cách bạn có thể tạo một

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
98, hãy tạo một tệp mới có tên là
# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
94 và thêm mã này vào đó

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
8

Trong mã này, bạn tạo một hình ảnh đẹp màu trắng thông qua phương thức

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
95. Sau đó, bạn vẽ một hình elip màu đỏ trên đó. Cuối cùng, bạn vẽ một hình elip thứ hai được tô màu vàng và viền màu đen trong đó chiều rộng đường viền được đặt thành 5

Khi bạn chạy đoạn mã này, hình ảnh mà nó tạo ra sẽ như thế này

Làm thế nào để bạn vẽ một đối tượng trong python?

Vẽ hình elip

Bạn có thể tạo hình bầu dục và hình tròn bằng cách sử dụng

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
98. Hãy dùng thử và xem bạn có thể làm gì với nó

Bây giờ chúng ta cùng tìm hiểu cách tạo các lát bánh

Vẽ lát bánh

Một lát bánh giống như

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
97, nhưng cũng vẽ các đường thẳng giữa các điểm cuối và tâm của hộp giới hạn

Đây là cách định nghĩa phương thức

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
98

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
3

Bạn đã sử dụng tất cả các tham số này trong các bản vẽ khác. Để xem lại,

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
4 thêm màu vào bên trong của
# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
98 trong khi
# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
9 thêm đường viền màu vào hình

Để bắt đầu thực hành hình dạng này, hãy tạo một tệp mới có tên

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
12 và thêm mã này vào tệp của bạn

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
0

Trong mã này, bạn tạo một hình ảnh màu xám để vẽ trên đó. Sau đó, bạn tạo hai lát bánh.

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
98 đầu tiên được tô màu xanh lá cây. Cái thứ hai không được điền, nhưng nó có màu vàng
# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
9. Lưu ý rằng mỗi
# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
98 có mức độ bắt đầu và kết thúc khác nhau

Khi bạn chạy mã này, bạn sẽ nhận được hình ảnh sau

Làm thế nào để bạn vẽ một đối tượng trong python?

Vẽ lát bánh

Với một chút công việc, bạn có thể tạo biểu đồ hình tròn bằng Gối. Bạn nên thử với mã của mình một chút và thay đổi một số giá trị. Bạn sẽ nhanh chóng học được cách làm những lát bánh đẹp mắt của riêng mình

Bây giờ hãy cùng tìm hiểu cách vẽ đa giác bằng Gối

Vẽ đa giác

Đa giác là một hình dạng hình học có số điểm (đỉnh) và số đoạn thẳng hoặc cạnh bằng nhau. Hình vuông, hình tam giác và hình lục giác đều là các loại đa giác. Gối cho phép bạn tạo đa giác của riêng mình. Tài liệu của Gối định nghĩa một đa giác như thế này. Đường viền đa giác bao gồm các đường thẳng giữa các tọa độ đã cho, cộng với một đường thẳng giữa tọa độ cuối cùng và đầu tiên

Đây là định nghĩa mã của phương thức

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
2

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
1

Tất cả các thông số này giờ đã quen thuộc với bạn. Hãy tiếp tục và tạo một tệp Python mới và đặt tên là

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
17. Sau đó thêm mã này

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
2

Đoạn mã này sẽ tạo ra một hình ảnh màu xám giống như ví dụ cuối cùng trong phần trước. Sau đó, nó sẽ tạo ra một đa giác được tô màu xanh lục. Sau đó, nó sẽ tạo ra một đa giác thứ hai và phác thảo nó bằng màu vàng mà không cần tô màu

Trong cả hai hình vẽ, bạn đang cung cấp ba điểm. Điều đó sẽ tạo ra hai hình tam giác

Khi bạn chạy mã này, bạn sẽ nhận được kết quả này

Làm thế nào để bạn vẽ một đối tượng trong python?

Vẽ đa giác

Hãy thử thay đổi mã bằng cách thêm các điểm bổ sung vào một hoặc nhiều đa giác trong mã ở trên. Với một chút luyện tập, bạn sẽ có thể tạo các đa giác phức tạp một cách nhanh chóng với Gối

Vẽ hình chữ nhật

Phương thức

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
18 cho phép bạn vẽ hình chữ nhật hoặc hình vuông bằng Gối. Đây là cách
# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
18 được định nghĩa

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
3

Bạn có thể chuyển vào hai bộ dữ liệu xác định tọa độ đầu và cuối để vẽ hình chữ nhật. Hoặc bạn có thể cung cấp bốn tọa độ dưới dạng bộ hộp (bộ 4 mục). Sau đó, bạn có thể thêm một

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
9,
# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
4 với màu sắc và thay đổi đường viền của
# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
93

Tạo một tệp mới và đặt tên là

# draw_jointed_line.py

from PIL import Image, ImageDraw


def line(output_path):
    image = Image.new("RGB", (400, 400), "red")
    points = [(100, 100), (150, 200), (200, 50), (400, 400)]
    draw = ImageDraw.Draw(image)
    draw.line(points, width=15, fill="green", joint="curve")
    image.save(output_path)

if __name__ == "__main__":
    line("jointed_lines.jpg")
83. Sau đó điền mã này vào để bạn có thể bắt đầu vẽ hình chữ nhật

# draw_line.py

import random
from PIL import Image, ImageDraw


def line(image_path, output_path):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    colors = ["red", "green", "blue", "yellow",
              "purple", "orange"]

    for i in range(0, 100, 20):
        draw.line((i, 0) + image.size, width=5, 
                  fill=random.choice(colors))

    image.save(output_path)

if __name__ == "__main__":
    line("madison_county_bridge_2.jpg", "lines.jpg")
4

Mã này sẽ tạo ra một hình ảnh màu xanh có kích thước 400x400 pixel. Sau đó, nó sẽ vẽ hai hình chữ nhật. Hình chữ nhật đầu tiên sẽ được tô màu đỏ. Cái thứ hai sẽ có màu xanh lục và viền màu vàng

Khi bạn chạy mã này, bạn sẽ nhận được hình ảnh này dưới dạng đầu ra

Làm thế nào để bạn vẽ một đối tượng trong python?

Vẽ hình chữ nhật

Không phải là những hình chữ nhật đáng yêu? . Bạn cũng có thể sửa đổi chiều rộng đường viền mà bạn thêm vào hình chữ nhật

kết thúc

Bạn có thể sử dụng Gối để thêm hình dạng vào hình ảnh của mình. Điều này có thể hữu ích khi thêm đường viền vào hình ảnh của bạn, làm nổi bật một hoặc nhiều phần hình ảnh của bạn, v.v.

Trong bài viết này, bạn đã tìm hiểu về các chủ đề sau

  • Thông số chung
  • vẽ đường
  • Vẽ vòng cung
  • Vẽ hợp âm
  • Vẽ hình elip
  • Vẽ lát bánh
  • Vẽ đa giác
  • Vẽ hình chữ nhật

Bạn có thể làm rất nhiều với các hình dạng được cung cấp bởi Gối. Bạn nên lấy những ví dụ này và sửa đổi chúng để kiểm tra chúng bằng ảnh của chính bạn. Hãy thử và xem những gì bạn có thể nghĩ ra

Việc sử dụng một đối tượng trong Python là gì?

Một đối tượng là thực thể thời gian chạy được sử dụng để cung cấp chức năng cho lớp Python. Các thuộc tính hoặc thuộc tính được định nghĩa bên trong lớp chỉ được truy cập bằng cách sử dụng các đối tượng của lớp đó. Ngoài ra, các chức năng do người dùng xác định được truy cập bằng cách sử dụng đối tượng

Làm thế nào để bạn tạo một đối tượng từ một lớp trong Python?

Tạo đối tượng. Bây giờ chúng ta có thể sử dụng lớp có tên MyClass để tạo các đối tượng. Thí dụ. Tạo một đối tượng có tên p1 và in giá trị của x. p1 = MyClass () in(p1. x) Tự mình thử »

Làm cách nào để vẽ đa giác trong Pygame?

Vẽ trong PyGame được tích hợp sẵn, vì vậy điều đó đủ dễ dàng. Chúng tôi có thể vẽ hình tròn, hình vuông, hình chữ nhật hoặc đa giác tùy chỉnh theo lựa chọn của chúng tôi với tọa độ. Hàm này nhận các điểm bắt đầu x, y, các biến chiều rộng và chiều cao, và cuối cùng là một màu. Từ đó, chúng tôi sử dụng pygame. vẽ tranh. rect() để vẽ đa giác theo thông số kỹ thuật của chúng tôi

Làm thế nào để vẽ một hình dạng trong Python rùa?

Hãy bắt đầu vẽ hình đầu tiên… Bắt đầu từ (0,0). Nằm trong góc phần tư thứ nhất (trục x và y dương). Có cạnh 200 pixel. Chúng ta hãy xem qua chương trình đơn giản ở trên… Sau khi nhập mô-đun Rùa, chúng ta đặt màu bút (cho đường viền của hình) và màu tô (để tô cho hình) bằng cách sử dụng hàm color ()