Hướng dẫn add border to image python - thêm đường viền vào hình ảnh 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()

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()

Xem thảo luận

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

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo 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

    Hướng dẫn add border to image python - thêm đường viền vào hình ảnh python

    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:

    Hướng dẫn add border to image python - thêm đường viền vào hình ảnh python

    Làm cách nào để thêm một đường viền vào một hình ảnh trong Python?

    Để thêm đường viền vào hình ảnh OpenCV có gói copymakeborder giúp tạo đường viền xung quanh hình ảnh ...
    inputImage..
    topBorderWidth..
    bottomBorderWidth..
    leftBorderWidth..
    rightBorderWidth..
    CV2. Border_constant ..
    value = màu sắc của đường viền ..

    Làm cách nào để thêm một đường viền trắng vào một hình ảnh trong Python?

    Nếu đó là một trong hai cái đầu tiên, chúng tôi thêm đường viền của chúng tôi bằng cách gọi hàm mở rộng () ...
    Đường dẫn hình ảnh đầu vào ..
    Đường dẫn hình ảnh đầu ra ..
    Biên giới, có thể là một số nguyên hoặc một bộ số tối đa 4 số nguyên đại diện cho các pixel cho mỗi trong bốn mặt của hình ảnh ..

    Làm thế nào để bạn tạo một biên giới trong Python?

    Tạo một khung với phương thức frame ().Làm nổi bật đường viền của khung với màu, nổi bật trên mạng = "màu xanh".Sau đó, đặt độ dày của đường viền, điểm nổi bật = 2.Tiếp theo, tạo một số tiện ích bên trong khung.

    Làm thế nào để bạn tạo ra một biên giới trên opencv?

    Bước 1: Nhập thư viện và đọc hình ảnh.Trước tiên chúng ta hãy nhập thư viện CV2 và đọc hình ảnh bằng CV2.hàm imread ().....
    Bước 2: Tạo đường viền bằng CV2.Chức năng copymakeborder ().....
    Bước 3: Hiển thị hình ảnh.Bây giờ chúng tôi đã hoàn thành việc thêm đường viền vào hình ảnh ..