Hướng dẫn how do i add a border to an image in python? - làm cách nào để thêm đường viền vào hình ảnh trong python?

from PIL import Image
from PIL import ImageOps
img = Image.open["dem.jpg"].convert["RGB"]

Phần này sẽ thêm đường viền màu đen ở hai bên [10% chiều rộng]

img_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],0,int[0.1*img.size[0]],0], fill=[0,0,0]]
img_side.save["sunset-sides.jpg"] 

Phần này sẽ thêm đường viền màu đen ở phía dưới và trên cùng [10% chiều cao]

img_updown = ImageOps.expand[img, border=[0,int[0.1*img.size[1]],0,int[0.1*img.size[1]]], fill=[0,0,0]]
img_updown.save["sunset-top_bottom.jpg"]

Phần này sẽ thêm đường viền màu đen ở phía dưới, trên cùng và hai bên [10% chiều cao chiều cao]

img_updown_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],int[0.1*img.size[1]],int[0.1*img.size[0]],int[0.1*img.size[1]]], fill=[0,0,0]]
img_updown_side.save["sunset-all_sides.jpg"]
img.close[]
img_side.close[]
img_updown.close[]
img_updown_side.close[]

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luậnOpenCV that can be used for the same. So before adding borders let’s see a small introduction about OpenCV.

    Xử lý hình ảnh là một lĩnh vực thú vị trong thời đại ngày nay của trí tuệ nhân tạo và học máy. Chúng ta có thể thấy các ứng dụng xử lý hình ảnh trong cuộc sống hàng ngày của chúng ta, như bất cứ khi nào chúng ta áp dụng bộ lọc qua bất kỳ hình ảnh nào [selfie] hoặc khi chúng ta muốn áp dụng một số hiệu ứng như làm mờ hình ảnh, v.v. & nbsp;[Open Source Computer Vision Library]

    • Trong bài viết này, chúng tôi sẽ thảo luận về cách thêm đường viền vào hình ảnh bằng Python. Python cung cấp một mô -đun gọi là OpenCV có thể được sử dụng cho cùng. Vì vậy, trước khi thêm biên giới, hãy để xem một phần giới thiệu nhỏ về OpenCV.
    • OpenCV [Thư viện tầm nhìn máy tính nguồn mở]
    • Nó là một thư viện nguồn mở.Numpyalong with MATLABstyle syntax.

    Được thiết kế để giải quyết các vấn đề về tầm nhìn máy tính.OpenCV has a package copyMakeBorder which helps to make a border around the image.

    Nó sử dụng một thư viện được tối ưu hóa cao cho các hoạt động số là Numpyalong với cú pháp Matlabstyle.cv2.copyMakeBorder[]

    Để thêm đường viền vào hình ảnh OpenCV có một gói copymakeborder giúp tạo đường viền xung quanh hình ảnh.

    • Cú pháp: CV2.copymakeborder []
    • Các tham số của Copymakeborder:
    • InputImage
    • Topberderwidth
    • đóng chai
    • cv2.BORDER_CONSTANT
    • Leftbandderwidth

    RightBanderWidth

    Example:

    Python3

    value = màu của đường viền

    Hình ảnh đầu vào:

    import cv2

    img_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],0,int[0.1*img.size[0]],0], fill=[0,0,0]]
    img_side.save["sunset-sides.jpg"] 
    
    5
    img_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],0,int[0.1*img.size[0]],0], fill=[0,0,0]]
    img_side.save["sunset-sides.jpg"] 
    
    6
    img_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],0,int[0.1*img.size[0]],0], fill=[0,0,0]]
    img_side.save["sunset-sides.jpg"] 
    
    7
    img_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],0,int[0.1*img.size[0]],0], fill=[0,0,0]]
    img_side.save["sunset-sides.jpg"] 
    
    8
    img_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],0,int[0.1*img.size[0]],0], fill=[0,0,0]]
    img_side.save["sunset-sides.jpg"] 
    
    7
    img_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],0,int[0.1*img.size[0]],0], fill=[0,0,0]]
    img_side.save["sunset-sides.jpg"] 
    
    8
    img_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],0,int[0.1*img.size[0]],0], fill=[0,0,0]]
    img_side.save["sunset-sides.jpg"] 
    
    7
    img_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],0,int[0.1*img.size[0]],0], fill=[0,0,0]]
    img_side.save["sunset-sides.jpg"] 
    
    8
    img_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],0,int[0.1*img.size[0]],0], fill=[0,0,0]]
    img_side.save["sunset-sides.jpg"] 
    
    7
    img_updown = ImageOps.expand[img, border=[0,int[0.1*img.size[1]],0,int[0.1*img.size[1]]], fill=[0,0,0]]
    img_updown.save["sunset-top_bottom.jpg"]
    
    4=
    img_updown = ImageOps.expand[img, border=[0,int[0.1*img.size[1]],0,int[0.1*img.size[1]]], fill=[0,0,0]]
    img_updown.save["sunset-top_bottom.jpg"]
    
    6
    img_updown = ImageOps.expand[img, border=[0,int[0.1*img.size[1]],0,int[0.1*img.size[1]]], fill=[0,0,0]]
    img_updown.save["sunset-top_bottom.jpg"]
    
    7
    img_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],0,int[0.1*img.size[0]],0], fill=[0,0,0]]
    img_side.save["sunset-sides.jpg"] 
    
    8
    img_updown = ImageOps.expand[img, border=[0,int[0.1*img.size[1]],0,int[0.1*img.size[1]]], fill=[0,0,0]]
    img_updown.save["sunset-top_bottom.jpg"]
    
    7
    img_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],0,int[0.1*img.size[0]],0], fill=[0,0,0]]
    img_side.save["sunset-sides.jpg"] 
    
    8
    img_updown_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],int[0.1*img.size[1]],int[0.1*img.size[0]],int[0.1*img.size[1]]], fill=[0,0,0]]
    img_updown_side.save["sunset-all_sides.jpg"]
    
    1
    img_updown_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],int[0.1*img.size[1]],int[0.1*img.size[0]],int[0.1*img.size[1]]], fill=[0,0,0]]
    img_updown_side.save["sunset-all_sides.jpg"]
    
    2

    img_updown_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],int[0.1*img.size[1]],int[0.1*img.size[0]],int[0.1*img.size[1]]], fill=[0,0,0]]
    img_updown_side.save["sunset-all_sides.jpg"]
    
    3
    img_updown_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],int[0.1*img.size[1]],int[0.1*img.size[0]],int[0.1*img.size[1]]], fill=[0,0,0]]
    img_updown_side.save["sunset-all_sides.jpg"]
    
    4
    img_updown_side = ImageOps.expand[img, border=[int[0.1*img.size[0]],int[0.1*img.size[1]],int[0.1*img.size[0]],int[0.1*img.size[1]]], fill=[0,0,0]]
    img_updown_side.save["sunset-all_sides.jpg"]
    
    5

    Output:

    Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều

    FrompilimportImage, ImageOps PIL import Image, ImageOps
    # mở hình ảnh
    img = Image.open ["test_image.jpg"] = Image.open["test_image.jpg"]
    # Màu sắc
    color="green" = "green"
    # trên cùng, phải, dưới cùng, bên trái
    Border = [20, 10, 20, 10] = [20, 10, 20, 10]
    new_img = ImageOps.Expand [IMG, Border = Border, Fill = Color] = ImageOps.expand[img, border=border, fill=color]
    # Lưu hình ảnh mới
    new_img.save ["test_image_result.jpg"].save["test_image_result.jpg"]
    # Hiển thị hình ảnh viền mới trong bản xem trước
    new_img.show [].show[]

    Bạn có thể thêm một đường viền trong ảnh?

    Chỉ cần nhấp vào nút Chỉnh sửa một hình ảnh từ trang chủ để bắt đầu.Sau đó tải lên ảnh bạn muốn chỉnh sửa.Sau khi ảnh được tải lên, nhấp vào khung hình trên bên trái của bảng điều khiển và bạn sẽ thấy một tấn đường viền và khung ảnh.Bạn có thể thêm các đường viền màu, đường viền có hoa văn và thậm chí đường viền phác thảo.You can add colored borders, patterned borders, and even outline borders.

    Bạn có thể thêm đường viền vào hình ảnh trong CSS không?

    Với thuộc tính hình ảnh biên giới CSS, bạn có thể đặt một hình ảnh được sử dụng làm đường viền xung quanh một phần tử..

    Bài Viết Liên Quan

    Chủ Đề