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
Cài đặt
- Linux. Trên thiết bị đầu cuối linux gõ như sau.
pip install Pillow
Cài đặt pip qua thiết bị đầu cuối
sudo apt-get update sudo apt-get install python-pip
- các cửa sổ. gói Gối thích hợp theo phiên bản python của bạn. Đảm bảo tải xuống theo phiên bản python bạn có
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
from PIL import Image
Thao tác với hình ảnh
- 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
:
sudo apt-get update sudo apt-get install python-pip
0_______1_______1sudo 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
5sudo apt-get update sudo apt-get install python-pip
6sudo apt-get update sudo apt-get install python-pip
8sudo apt-get update sudo apt-get install python-pip
9from PIL import Image
0from 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
2from PIL import Image
3_______2_______4from PIL import Image
5from PIL import Image
6from PIL import Image
7sudo apt-get update sudo apt-get install python-pip
1from PIL import Image
9#img = Image.open[path]
0sudo apt-get update sudo apt-get install python-pip
3#img = Image.open[path]
2#img = Image.open[path]
4_______1_______1#img = Image.open[path]
6#img = Image.open[path]
7Mộ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____187_______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
2from PIL import Image
3_______2_______4from PIL import Image
5from PIL import Image
6# On successful execution of this statement,
8# On successful execution of this statement,
9try
# 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# an object of Image type is returned and stored in img variable]
4# an object of Image type is returned and stored in img variable]
3# an object of Image type is returned and stored in img variable]
6sudo apt-get update sudo apt-get install python-pip
1sudo apt-get update sudo apt-get install python-pip
2_______1_______3# an object of Image type is returned and stored in img variable]
3____190_______5# an object of Image type is returned and stored in img variable]
3# an object of Image type is returned and stored in img variable]
6sudo apt-get update sudo apt-get install python-pip
1try
0# an object of Image type is returned and stored in img variable]
3try
6try
7try
8sudo apt-get update sudo apt-get install python-pip
5sudo apt-get update sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable]
3sudo apt-get update sudo apt-get install python-pip
8from PIL import Image
6:
5:
6sudo 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
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
2from PIL import Image
3_______2_______4from PIL import Image
5from PIL import Image
6# On successful execution of this statement,
8# On successful execution of this statement,
9try
# 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# an object of Image type is returned and stored in img variable]
4# an object of Image type is returned and stored in img variable]
3# an object of Image type is returned and stored in img variable]
6sudo apt-get update sudo apt-get install python-pip
1sudo apt-get update sudo apt-get install python-pip
2_______1_______3try
8# an object of Image type is returned and stored in img variable]
3____187_______4sudo apt-get update sudo apt-get install python-pip
1sudo apt-get update sudo apt-get install python-pip
16# an object of Image type is returned and stored in img variable]
3sudo apt-get update sudo apt-get install python-pip
19sudo apt-get update sudo apt-get install python-pip
1sudo 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____189_______6sudo apt-get update sudo apt-get install python-pip
1sudo apt-get update sudo apt-get install python-pip
35# an object of Image type is returned and stored in img variable]
3try
4# an object of Image type is returned and stored in img variable]
3try
6sudo apt-get update sudo apt-get install python-pip
41sudo apt-get update sudo apt-get install python-pip
5sudo apt-get update sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable]
3sudo apt-get update sudo apt-get install python-pip
8from PIL import Image
6:
5:
6sudo 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 - 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
2from PIL import Image
3_______2_______4from PIL import Image
5from PIL import Image
6# On successful execution of this statement,
8# On successful execution of this statement,
9try
# 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]
4# an object of Image type is returned and stored in img variable]
3# an object of Image type is returned and stored in img variable]
6sudo apt-get update sudo apt-get install python-pip
1sudo apt-get update sudo apt-get install python-pip
2_______1_______3try
8# an object of Image type is returned and stored in img variable]
3____187_______4sudo apt-get update sudo apt-get install python-pip
1sudo apt-get update sudo apt-get install python-pip
16# an object of Image type is returned and stored in img variable]
3# an object of Image type is returned and stored in img variable]
6sudo apt-get update sudo apt-get install python-pip
1sudo 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# an object of Image type is returned and stored in img variable]
3try
4# an object of Image type is returned and stored in img variable]
3try
6sudo apt-get update sudo apt-get install python-pip
98sudo apt-get update sudo apt-get install python-pip
5sudo apt-get update sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable]
3sudo apt-get update sudo apt-get install python-pip
8from PIL import Image
6:
5:
6sudo 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 - 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
2from PIL import Image
3_______2_______4from PIL import Image
5from PIL import Image
6# On successful execution of this statement,
8# On successful execution of this statement,
9try
# 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# an object of Image type is returned and stored in img variable]
4# an object of Image type is returned and stored in img variable]
3from PIL import Image
27# an object of Image type is returned and stored in img variable]
3# an object of Image type is returned and stored in img variable]
6sudo apt-get update sudo apt-get install python-pip
1sudo apt-get update sudo apt-get install python-pip
2_______1_______3# an object of Image type is returned and stored in img variable]
3# an object of Image type is returned and stored in img variable]
4# an object of Image type is returned and stored in img variable]
3_______2_______40# an object of Image type is returned and stored in img variable]
3from PIL import Image
42sudo apt-get update sudo apt-get install python-pip
1sudo apt-get update sudo apt-get install python-pip
2_______1_______3from PIL import Image
47# an object of Image type is returned and stored in img variable]
3from PIL import Image
50from PIL import Image
51sudo apt-get update sudo apt-get install python-pip
23from PIL import Image
51sudo apt-get update sudo apt-get install python-pip
92# an object of Image type is returned and stored in img variable]
3try
4# an object of Image type is returned and stored in img variable]
3try
6from PIL import Image
60try
8sudo apt-get update sudo apt-get install python-pip
5sudo apt-get update sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable]
3sudo apt-get update sudo apt-get install python-pip
8from PIL import Image
6:
5:
6sudo 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]
2from PIL import Image
6from 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,
9try
# 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# an object of Image type is returned and stored in img variable]
4# an object of Image type is returned and stored in img variable]
3# an object of Image type is returned and stored in img variable]
6sudo apt-get update sudo apt-get install python-pip
1sudo apt-get update sudo apt-get install python-pip
2_______1_______3# an object of Image type is returned and stored in img variable]
3_______2_______96# an object of Image type is returned and stored in img variable]
3____2_______98from PIL import Image
99sudo apt-get update sudo apt-get install python-pip
5sudo apt-get update sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable]
3sudo apt-get update sudo apt-get install python-pip
8from PIL import Image
6:
5:
6sudo 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 - 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
2from PIL import Image
3from PIL import Image
4from PIL import Image
5from PIL import Image
6# On successful execution of this statement,
8# On successful execution of this statement,
9try
# 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# an object of Image type is returned and stored in img variable]
4# an object of Image type is returned and stored in img variable]
3# an object of Image type is returned and stored in img variable]
6sudo apt-get update sudo apt-get install python-pip
1sudo apt-get update sudo apt-get install python-pip
2_______1_______3# an object of Image type is returned and stored in img variable]
3#img = Image.open[path]
38# an object of Image type is returned and stored in img variable]
3#img = Image.open[path]
40sudo apt-get update sudo apt-get install python-pip
1#img = Image.open[path]
42# an object of Image type is returned and stored in img variable]
3#img = Image.open[path]
45# an object of Image type is returned and stored in img variable]
3____187_______47#img = Image.open[path]
48try
8sudo apt-get update sudo apt-get install python-pip
5sudo apt-get update sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable]
3sudo apt-get update sudo apt-get install python-pip
8from PIL import Image
6:
5:
6sudo 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 - 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
2from PIL import Image
3_______2_______4from PIL import Image
5from PIL import Image
6# On successful execution of this statement,
8# On successful execution of this statement,
9try
# 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# an object of Image type is returned and stored in img variable]
4# an object of Image type is returned and stored in img variable]
3# an object of Image type is returned and stored in img variable]
6sudo apt-get update sudo apt-get install python-pip
1sudo apt-get update sudo apt-get install python-pip
2_______1_______3# an object of Image type is returned and stored in img variable]
3____187_______86# an object of Image type is returned and stored in img variable]
3____2_______98#img = Image.open[path]
89sudo apt-get update sudo apt-get install python-pip
5sudo apt-get update sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable]
3sudo apt-get update sudo apt-get install python-pip
8from PIL import Image
6:
5:
6sudo 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 - 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,
9try
# 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# an object of Image type is returned and stored in img variable]
4# an object of Image type is returned and stored in img variable]
3# an object of Image type is returned and stored in img variable]
6sudo apt-get update sudo apt-get install python-pip
1sudo apt-get update sudo apt-get install python-pip
2_______1_______3# an object of Image type is returned and stored in img variable]
3from PIL import Image
98# On successful execution of this statement,
21# an object of Image type is returned and stored in img variable]
3# On successful execution of this statement,
24# an object of Image type is returned and stored in img variable]
3from PIL import Image
98# On successful execution of this statement,
27# an object of Image type is returned and stored in img variable]
3from PIL import Image
98# On successful execution of this statement,
31# On successful execution of this statement,
32sudo apt-get update sudo apt-get install python-pip
5sudo apt-get update sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable]
3sudo apt-get update sudo apt-get install python-pip
8from PIL import Image
6:
5:
6sudo 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 - 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,
9try
# 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# an object of Image type is returned and stored in img variable]
4# an object of Image type is returned and stored in img variable]
3# an object of Image type is returned and stored in img variable]
6sudo apt-get update sudo apt-get install python-pip
1sudo apt-get update sudo apt-get install python-pip
2_______1_______3# an object of Image type is returned and stored in img variable]
3# On successful execution of this statement,
64# an object of Image type is returned and stored in img variable]
3# On successful execution of this statement,
66# On successful execution of this statement,
67sudo apt-get update sudo apt-get install python-pip
23# On successful execution of this statement,
67# On successful execution of this statement,
70# an object of Image type is returned and stored in img variable]
3try
6# On successful execution of this statement,
74try
8sudo apt-get update sudo apt-get install python-pip
5sudo apt-get update sudo apt-get install python-pip
6# an object of Image type is returned and stored in img variable]
3sudo apt-get update sudo apt-get install python-pip
8from PIL import Image
6:
5:
6sudo 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
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