Thư viện hình ảnh Python hoặc PIL là một thư viện Python tuyệt vời được sử dụng để xử lý hình ảnh. Thư viện này cung cấp rất nhiều tính năng để làm việc trên hình ảnh bằng Python. Nó được sử dụng như một công cụ xử lý ảnh với các thư viện xử lý ảnh Python khác như OpenCV. Nếu bạn chưa từng sử dụng Thư viện hình ảnh Python (PIL) trước đây, thì bài viết này là dành cho bạn. Trong bài viết này, tôi sẽ giới thiệu với các bạn một bài hướng dẫn về Thư viện hình ảnh Python
Thư viện hình ảnh PIL hoặc Python thường bị nhầm lẫn với Gối. Gối là một nhánh của thư viện PIL trong Python, đó là lý do tại sao để cài đặt PIL, chúng tôi viết “pip install Gối”, thay vì “pip install PIL”. Một số tính năng quan trọng mà thư viện này cung cấp cho bạn để xử lý ảnh là
Thư viện Python này đã có sẵn trong thư viện chuẩn Python, nhưng để sử dụng phiên bản mới nhất, tôi khuyên bạn nên chạy lệnh cài đặt pip được đề cập bên dưới trước khi bắt tay vào Thư viện hình ảnh Python
Như đã đề cập trước đó, nó được sử dụng làm PIL nhưng được cài đặt làm Gối. Bây giờ trong phần bên dưới, tôi sẽ hướng dẫn bạn qua phần hướng dẫn về Thư viện hình ảnh Python
Tôi sẽ bắt đầu bằng cách đọc và hình dung hình ảnh mà tôi đang sử dụng cho hướng dẫn này
Xoay hình ảnh cũng là một tính năng tuyệt vời được cung cấp bởi thư viện này. Đây là cách xoay hình ảnh
Tính năng hữu ích nhất mà tôi thích ở thư viện này là bạn có thể thêm các bộ lọc vào hình ảnh của mình chỉ bằng một vài dòng mã. Nó có một số bộ lọc sẵn có mà bạn có thể áp dụng cho hình ảnh của mình. Ví dụ: hãy xem bộ lọc làm mờ gaussian mà tôi đã áp dụng cho hình ảnh bên dưới
Vì vậy, đây là cách bạn có thể sử dụng Thư viện hình ảnh PIL hoặc Python để làm việc trên bộ dữ liệu hình ảnh của mình. Bạn có thể khám phá thêm về thư viện này từ đây
Thư viện hình ảnh Python là một thư viện Python tuyệt vời được sử dụng để xử lý hình ảnh. Một số tính năng quan trọng mà thư viện này cung cấp cho bạn để xử lý ảnh là
Tôi hy vọng bạn thích bài viết này trong hướng dẫn về Thư viện hình ảnh Python. Vui lòng đặt câu hỏi có giá trị của bạn trong phần bình luận bên dưới
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. Nó được phát triển bởi Fredrik Lundh và một số cộng tác viên khác. Gối là ngã ba PIL thân thiện và thư viện dễ sử dụng được phát triển bởi Alex Clark và những người đóng góp khác. Chúng tôi sẽ làm việc với Gối
Chúng tôi sẽ làm việc với Mô-đun Hình ảnh ở đây cung cấp một lớp cùng tên và cung cấp rất nhiều chức năng để hoạt động trên hình ảnh của chúng tôi. Để nhập mô-đun Hình ảnh, mã của chúng tôi phải bắt đầu bằng dòng sau
- Mở một hình ảnh cụ thể từ một đường dẫn
#img = Image.open(path)
# On successful execution of this statement,
# an object of Image type is returned and stored in img variable)
try
:
8_______0
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
2sudo apt-get update
sudo apt-get install python-pip
3sudo apt-get update
sudo apt-get install python-pip
4sudo apt-get update
sudo apt-get install python-pip
5 sudo apt-get update
sudo apt-get install python-pip
6
sudo apt-get update
sudo apt-get install python-pip
8sudo apt-get update
sudo apt-get install python-pip
9 from PIL import Image
0 from PIL import Image
1 - Lấy kích thước của hình ảnh. Các thể hiện của lớp Hình ảnh được tạo có nhiều thuộc tính, một trong những thuộc tính hữu ích của nó là kích thước
from PIL import Image
2 from PIL import Image
3_______9_______4 from PIL import Image
5 from PIL import Image
6 from PIL import Image
7sudo apt-get update
sudo apt-get install python-pip
1 from PIL import Image
9#img = Image.open(path)
0_______8_______3#img = Image.open(path)
2
#img = Image.open(path)
4
sudo apt-get update
sudo apt-get install python-pip
1 #img = Image.open(path)
6#img = Image.open(path)
7
Một số thuộc tính khác là. Hình ảnh. chiều rộng, Hình ảnh. chiều cao, Hình ảnh. định dạng, Hình ảnh. thông tin v.v.
- Lưu các thay đổi trong hình ảnh. Để lưu bất kỳ thay đổi nào bạn đã thực hiện đối với tệp hình ảnh, chúng tôi cần cung cấp đường dẫn cũng như định dạng hình ảnh
#img = Image.open(path)
8#img = Image.open(path)
9# On successful execution of this statement,
0
# On successful execution of this statement,
1
# On successful execution of this statement,
2
- Xoay một hình ảnh. Xoay hình ảnh cần góc làm tham số để xoay hình ảnh
from PIL import Image
2 from PIL import Image
3_______9_______4 from PIL import Image
5 from PIL import Image
6# On successful execution of this statement,
8 # On successful execution of this statement,
9
try
# an object of Image type is returned and stored in img variable)
2
# an object of Image type is returned and stored in img variable)
3______196_______4
# an object of Image type is returned and stored in img variable)
3_______196_______6
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
2_______8_______3
0
1
2
3
# an object of Image type is returned and stored in img variable)
3____197_______5
# an object of Image type is returned and stored in img variable)
3_______196_______6
sudo apt-get update
sudo apt-get install python-pip
1
9try
0
2
3
3____198_______4
# an object of Image type is returned and stored in img variable)
3_______198_______6try
7try
8
sudo apt-get update
sudo apt-get install python-pip
5 sudo apt-get update
sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable)
3____8_______8
from PIL import Image
6:
5 :
6
sudo apt-get update
sudo apt-get install python-pip
1sudo apt-get update
sudo apt-get install python-pip
1 :
9# an object of Image type is returned and stored in img variable)
2
2
Ghi chú. Có một cờ mở rộng tùy chọn có sẵn dưới dạng một trong các đối số của phương thức xoay, cờ này nếu được đặt đúng, sẽ mở rộng hình ảnh đầu ra để làm cho nó đủ lớn để giữ toàn bộ hình ảnh được xoay
Như đã thấy trong đoạn mã trên, tôi đã sử dụng một đường dẫn tương đối nơi hình ảnh của tôi nằm trong cùng thư mục với tệp mã python của tôi, một đường dẫn tuyệt đối cũng có thể được sử dụng - Cắt ảnh. Hình ảnh. crop(box) lấy tọa độ pixel 4 bộ (trái, trên, phải, dưới) và trả về một vùng hình chữ nhật từ hình ảnh đã sử dụng
from PIL import Image
2 from PIL import Image
3_______9_______4 from PIL import Image
5 from PIL import Image
6# On successful execution of this statement,
8 # On successful execution of this statement,
9
try
# an object of Image type is returned and stored in img variable)
2
# an object of Image type is returned and stored in img variable)
3______196_______4
# an object of Image type is returned and stored in img variable)
3_______196_______6
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
2_______8_______3
0
1try
8# an object of Image type is returned and stored in img variable)
3_______194_______4
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
16
3
# an object of Image type is returned and stored in img variable)
3_______8_______19
sudo apt-get update
sudo apt-get install python-pip
1
0sudo apt-get update
sudo apt-get install python-pip
22sudo apt-get update
sudo apt-get install python-pip
23sudo apt-get update
sudo apt-get install python-pip
22sudo apt-get update
sudo apt-get install python-pip
25sudo apt-get update
sudo apt-get install python-pip
26sudo apt-get update
sudo apt-get install python-pip
27sudo apt-get update
sudo apt-get install python-pip
28sudo apt-get update
sudo apt-get install python-pip
26sudo apt-get update
sudo apt-get install python-pip
27try
8# an object of Image type is returned and stored in img variable)
3_______196_______6
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
35
3
# an object of Image type is returned and stored in img variable)
3____198_______4
# an object of Image type is returned and stored in img variable)
3_______198_______6
sudo apt-get update
sudo apt-get install python-pip
41
2
3
sudo apt-get update
sudo apt-get install python-pip
5 sudo apt-get update
sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable)
3____8_______8
from PIL import Image
6:
5 :
6
sudo apt-get update
sudo apt-get install python-pip
1sudo apt-get update
sudo apt-get install python-pip
1 :
9# an object of Image type is returned and stored in img variable)
2
2
- Thay đổi kích thước hình ảnh. Hình ảnh. thay đổi kích thước (kích thước) - Ở đây kích thước được cung cấp dưới dạng chiều rộng và chiều cao 2 bộ
from PIL import Image
2 from PIL import Image
3_______9_______4 from PIL import Image
5 from PIL import Image
6# On successful execution of this statement,
8 # On successful execution of this statement,
9
try
# an object of Image type is returned and stored in img variable)
2
3____196_______4
# an object of Image type is returned and stored in img variable)
3_______196_______6
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
2_______8_______3
0
1try
8# an object of Image type is returned and stored in img variable)
3_______194_______4
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
16
# an object of Image type is returned and stored in img variable)
3_______196_______6
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
86sudo apt-get update
sudo apt-get install python-pip
26sudo apt-get update
sudo apt-get install python-pip
27sudo apt-get update
sudo apt-get install python-pip
28sudo apt-get update
sudo apt-get install python-pip
26sudo apt-get update
sudo apt-get install python-pip
27sudo apt-get update
sudo apt-get install python-pip
92
3
# an object of Image type is returned and stored in img variable)
3____198_______4
# an object of Image type is returned and stored in img variable)
3_______198_______6
sudo apt-get update
sudo apt-get install python-pip
98
2
sudo apt-get update
sudo apt-get install python-pip
5 sudo apt-get update
sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable)
3____8_______8
from PIL import Image
6:
5 :
6
sudo apt-get update
sudo apt-get install python-pip
1sudo apt-get update
sudo apt-get install python-pip
1 :
9# an object of Image type is returned and stored in img variable)
2
2
- Dán một hình ảnh lên một hình ảnh khác. Đối số thứ hai có thể là 2 bộ (chỉ định góc trên cùng bên trái) hoặc 4 bộ (trái, trên, phải, dưới) – trong trường hợp này, kích thước của hình ảnh đã dán phải khớp với kích thước của vùng hộp này hoặc
from PIL import Image
2 from PIL import Image
3_______9_______4 from PIL import Image
5 from PIL import Image
6# On successful execution of this statement,
8 # On successful execution of this statement,
9
try
# an object of Image type is returned and stored in img variable)
2
# an object of Image type is returned and stored in img variable)
3______196_______4
# an object of Image type is returned and stored in img variable)
3_______9_______27
# an object of Image type is returned and stored in img variable)
3_______196_______6
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
2_______8_______3
0
1
2
3
# an object of Image type is returned and stored in img variable)
3______196_______4
# an object of Image type is returned and stored in img variable)
3_______9_______40
# an object of Image type is returned and stored in img variable)
3_______9_______42
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
2sudo apt-get update
sudo apt-get install python-pip
3
0 from PIL import Image
47
2# an object of Image type is returned and stored in img variable)
3_______9_______50
from PIL import Image
51sudo apt-get update
sudo apt-get install python-pip
23 from PIL import Image
51_______8_______92
3
# an object of Image type is returned and stored in img variable)
3____198_______4
# an object of Image type is returned and stored in img variable)
3_______198_______6
from PIL import Image
60try
8
3
sudo apt-get update
sudo apt-get install python-pip
5 sudo apt-get update
sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable)
3____8_______8
from PIL import Image
6:
5 :
6
sudo apt-get update
sudo apt-get install python-pip
1sudo apt-get update
sudo apt-get install python-pip
1 :
9# an object of Image type is returned and stored in img variable)
2
2
from PIL import Image
6 from PIL import Image
78 - Lấy biểu đồ của một hình ảnh. Điều này sẽ trả về biểu đồ của hình ảnh dưới dạng danh sách số lượng pixel, một cho mỗi pixel trong hình ảnh. (Biểu đồ của một hình ảnh là một biểu diễn đồ họa của sự phân bố tông màu trong một hình ảnh kỹ thuật số. Nó chứa tất cả các giá trị độ sáng có trong một hình ảnh là gì. Nó vẽ sơ đồ số pixel cho từng giá trị độ sáng. Nó giúp thực hiện các cài đặt phơi sáng. )
từ hình ảnh nhập PIL
# On successful execution of this statement,
8 # On successful execution of this statement,
9
try
# an object of Image type is returned and stored in img variable)
2
# an object of Image type is returned and stored in img variable)
3______196_______4
# an object of Image type is returned and stored in img variable)
3_______196_______6
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
2_______8_______3
0
1
2
3
# an object of Image type is returned and stored in img variable)
3_______9_______96
# an object of Image type is returned and stored in img variable)
3_______9_______98
from PIL import Image
99
3
sudo apt-get update
sudo apt-get install python-pip
5 sudo apt-get update
sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable)
3____8_______8
from PIL import Image
6:
5 :
6
sudo apt-get update
sudo apt-get install python-pip
1sudo apt-get update
sudo apt-get install python-pip
1 :
9# an object of Image type is returned and stored in img variable)
2
2
- Chuyển một hình ảnh. Tính năng này cho chúng ta hình ảnh phản chiếu của một hình ảnh
from PIL import Image
2 from PIL import Image
3 from PIL import Image
4 from PIL import Image
5 from PIL import Image
6# On successful execution of this statement,
8 # On successful execution of this statement,
9
try
# an object of Image type is returned and stored in img variable)
2
# an object of Image type is returned and stored in img variable)
3______196_______4
# an object of Image type is returned and stored in img variable)
3_______196_______6
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
2_______8_______3
0
1
2
3
# an object of Image type is returned and stored in img variable)
3_______194_______38
# an object of Image type is returned and stored in img variable)
3_______194_______40
sudo apt-get update
sudo apt-get install python-pip
1 #img = Image.open(path)
42
3
# an object of Image type is returned and stored in img variable)
3______194_______45
# an object of Image type is returned and stored in img variable)
3_______194_______47____194_______48try
8
sudo apt-get update
sudo apt-get install python-pip
5 sudo apt-get update
sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable)
3____8_______8
from PIL import Image
6:
5 :
6
sudo apt-get update
sudo apt-get install python-pip
1sudo apt-get update
sudo apt-get install python-pip
1 :
9# an object of Image type is returned and stored in img variable)
2
2
- Tách một hình ảnh thành các dải riêng lẻ. Tách một hình ảnh ở chế độ RGB, tạo ba hình ảnh mới, mỗi hình ảnh chứa một bản sao của các dải riêng lẻ ban đầu
from PIL import Image
2 from PIL import Image
3_______9_______4 from PIL import Image
5 from PIL import Image
6# On successful execution of this statement,
8 # On successful execution of this statement,
9
try
# an object of Image type is returned and stored in img variable)
2
# an object of Image type is returned and stored in img variable)
3______196_______4
# an object of Image type is returned and stored in img variable)
3_______196_______6
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
2_______8_______3
0
1
2
3
# an object of Image type is returned and stored in img variable)
3____194_______86
# an object of Image type is returned and stored in img variable)
3_______9_______98 #img = Image.open(path)
89
sudo apt-get update
sudo apt-get install python-pip
5 sudo apt-get update
sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable)
3____8_______8
from PIL import Image
6:
5 :
6
sudo apt-get update
sudo apt-get install python-pip
1sudo apt-get update
sudo apt-get install python-pip
1 :
9# an object of Image type is returned and stored in img variable)
2
2
- tobitmap. Chuyển đổi hình ảnh thành bitmap X11 (Định dạng hình ảnh nhị phân văn bản thuần túy). Nó trả về một chuỗi chứa bitmap X11, nó chỉ có thể được sử dụng cho các hình ảnh ở chế độ “1”, tôi. e. Hình ảnh đen trắng 1 bit pixel
từ hình ảnh nhập PIL
# On successful execution of this statement,
8 # On successful execution of this statement,
9
try
# an object of Image type is returned and stored in img variable)
2
# an object of Image type is returned and stored in img variable)
3______196_______4
# an object of Image type is returned and stored in img variable)
3_______196_______6
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
2_______8_______3
0
1
2# an object of Image type is returned and stored in img variable)
3_______9_______98 # On successful execution of this statement,
21
3
# an object of Image type is returned and stored in img variable)
3______195_______24
# an object of Image type is returned and stored in img variable)
3_______9_______98 # On successful execution of this statement,
27
3
# an object of Image type is returned and stored in img variable)
3_______9_______98 # On successful execution of this statement,
31# On successful execution of this statement,
32
sudo apt-get update
sudo apt-get install python-pip
5 sudo apt-get update
sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable)
3____8_______8
from PIL import Image
6:
5 :
6
sudo apt-get update
sudo apt-get install python-pip
1sudo apt-get update
sudo apt-get install python-pip
1 :
9# an object of Image type is returned and stored in img variable)
2
2
- Tạo hình thu nhỏ. Phương pháp này tạo một hình thu nhỏ của hình ảnh được mở. Nó không trả về một đối tượng hình ảnh mới, nó tự sửa đổi tại chỗ đối tượng hình ảnh hiện đang mở. Nếu bạn không muốn thay đổi đối tượng ảnh gốc thì tạo một bản sao rồi áp dụng cách này. Phương pháp này cũng đánh giá sự phù hợp để duy trì tỷ lệ khung hình của hình ảnh theo kích thước được thông qua
từ hình ảnh nhập PIL
# On successful execution of this statement,
8 # On successful execution of this statement,
9
try
# an object of Image type is returned and stored in img variable)
2
# an object of Image type is returned and stored in img variable)
3______196_______4
# an object of Image type is returned and stored in img variable)
3_______196_______6
sudo apt-get update
sudo apt-get install python-pip
1 sudo apt-get update
sudo apt-get install python-pip
2_______8_______3
0
1
2
3
# an object of Image type is returned and stored in img variable)
3____195_______64
# an object of Image type is returned and stored in img variable)
3_______195_______66# On successful execution of this statement,
67
sudo apt-get update
sudo apt-get install python-pip
23# On successful execution of this statement,
67# On successful execution of this statement,
70
3
# an object of Image type is returned and stored in img variable)
3_______198_______6# On successful execution of this statement,
74try
8
sudo apt-get update
sudo apt-get install python-pip
5 sudo apt-get update
sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable)
3____8_______8
from PIL import Image
6:
5 :
6
sudo apt-get update
sudo apt-get install python-pip
1sudo apt-get update
sudo apt-get install python-pip
1 :
9# an object of Image type is returned and stored in img variable)
2
2
Bài viết này được đóng góp bởi Mohit Agarwal. Nếu bạn thích GeeksforGeeks và muốn đóng góp, bạn cũng có thể viết một bài báo bằng cách sử dụng đóng góp. chuyên viên máy tính. org hoặc gửi bài viết của bạn tới donate@geeksforgeeks. tổ chức. Xem bài viết của bạn xuất hiện trên trang chính của GeeksforGeeks và trợ giúp các Geeks khác
Vui lòng viết bình luận nếu bạn thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề thảo luận ở trên
Đảm bảo bạn có thể chạy pip từ dòng lệnh
. p Run python get-pip.py . 2 Điều này sẽ cài đặt hoặc nâng cấp pip. Ngoài ra, nó sẽ cài đặt các công cụ thiết lập và bánh xe nếu chúng chưa được cài đặt. Hãy thận trọng nếu bạn đang sử dụng bản cài đặt Python do hệ điều hành của bạn hoặc trình quản lý gói khác quản lý.
Thao tác hình ảnh cơ bản với thư viện gối Python. Thư viện Gối Python là một nhánh của thư viện cũ hơn có tên là PIL. PIL là viết tắt của Python Imaging Library, và đó là thư viện ban đầu cho phép Python xử lý hình ảnh. PIL đã ngừng hoạt động vào năm 2011 và chỉ hỗ trợ Python 2 .
Hỗ trợ cho Thư viện hình ảnh Python đã ngừng hoạt động vào năm 2011, nhưng một dự án có tên gối đã tách dự án PIL ban đầu và thêm Python3. x hỗ trợ cho nó. Gối đã được công bố là sản phẩm thay thế cho PIL để sử dụng trong tương lai