Mô-đun
img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
1 của thư viện xử lý ảnh Gối [PIL] cung cấp phương pháp img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
2 để thay đổi kích thước ảnhHình ảnh. resize[size, resample=0]
Trả về bản sao đã thay đổi kích thước của hình ảnh này.
Mô-đun hình ảnh — Gối [PIL Fork] 4. 0. 0 tài liệu
Bài viết này mô tả các nội dung sau
- Cách sử dụng
3img_resize = img.resize[[img.width // 2, img.height // 2]] img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
- Mã mẫu để thay đổi kích thước hàng loạt
- Chỉ thay đổi kích thước các tệp có phần mở rộng cụ thể
- Thay đổi kích thước tất cả các tệp hình ảnh mà Gối hỗ trợ
Xem bài viết sau để biết cách cài đặt và sử dụng cơ bản Gối [PIL]
- Cách sử dụng Gối [PIL. Thư viện hình ảnh Python]
Sử dụng
img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
4 để cắt một phần của hình ảnh- Cắt một phần ảnh bằng Python, Gối [trimming]
Liên kết được tài trợ
Cách sử dụng img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
3
img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
Truyền một bộ dữ liệu của
img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
0 cho đối số img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
1Chuyển bộ lọc được sử dụng để lấy mẫu lại cho đối số
img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
2. Có 6 loại sau. Nếu bỏ qua, img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
3 được sử dụng theo mặc địnhBộ lọc
- GẦN NHẤT
- HỘP
- SONG TUYẾN
- HAMMING . 0. 0 tài liệu
- BICUBIC
- LANCZOS
Concepts — Pillow [PIL Fork] 4.0.0 documentation
from PIL import Image
img = Image.open['data/src/lena_square.png']
img_resize = img.resize[[256, 256]]
img_resize.save['data/dst/lena_pillow_resize_nearest.jpg']
img_resize_lanczos = img.resize[[256, 256], Image.LANCZOS]
img_resize_lanczos.save['data/dst/lena_pillow_resize_lanczos.jpg']
nguồn. gối_hình ảnh_resize. py
img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
4 và img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
5 mất nhiều thời gian xử lý hơn so với img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
3, nhưng chất lượng tốt hơnHình ảnh bên trái được thay đổi kích thước bằng bộ lọc
img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
3 và hình ảnh bên phải được thay đổi kích thước bằng bộ lọc img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
5Trong ví dụ trên, kích thước hình ảnh được cố định là
img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
9. Để chỉ định nó dựa trên kích thước của ảnh gốc, hãy làm như sauimg_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
nguồn. gối_hình ảnh_resize. py
Vì kích thước phải được chỉ định dưới dạng số nguyên, nên sử dụng
img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
20Mã mẫu để thay đổi kích thước hàng loạt
Để thay đổi kích thước hàng loạt, hãy lấy chuỗi đường dẫn của các tệp trong thư mục đích [thư mục] với
img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
21, thay đổi kích thước chúng bằng img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
2 và lưu chúng với tên tệp mới là img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
23. Bạn có thể tạo thư mục đích bằng img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
24- toàn cầu — Mở rộng mẫu đường dẫn kiểu Unix — Python 3. 9. 0 tài liệu
- Lấy tên tệp, thư mục, phần mở rộng từ một chuỗi đường dẫn trong Python
- Tạo thư mục với mkdir[], makedirs[] trong Python
Chỉ thay đổi kích thước các tệp có phần mở rộng cụ thể
Mã mẫu để chỉ trích xuất các tệp có phần mở rộng
img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
25 như sauimg_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
2nguồn. gối_image_resize_all. py
Nếu bạn muốn lấy các tệp có nhiều phần mở rộng, bạn có thể chọn chúng sau khi giải nén đường dẫn của tất cả các tệp có đuôi
img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
21- toán tử in trong Python [đối với danh sách, chuỗi, từ điển, v.v. ]
img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
9nguồn. gối_image_resize_all. py
Thay đổi kích thước tất cả các tệp hình ảnh mà Gối hỗ trợ
Nếu bạn muốn resize tất cả các file ảnh mà Gối hỗ trợ thay vì giải nén bằng extension, hãy sử dụng
img_resize = img.resize[[img.width // 2, img.height // 2]]
img_resize_lanczos.save['data/dst/lena_pillow_resize_half.jpg']
27 để xử lý ngoại lệ. Nó dễ dàng hơn việc chỉ định rõ ràng nhiều tiện ích mở rộng