Io.bytesio trong python

Lớp quan trọng nhất trong Thư viện hình ảnh Python là lớp

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
1, được định nghĩa trong mô-đun có cùng tên. Bạn có thể tạo các thể hiện của lớp này theo nhiều cách;

Để tải hình ảnh từ một tệp, hãy sử dụng hàm

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
2 trong mô-đun
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
1

>>> from PIL import Image
>>> im = Image.open["hopper.ppm"]

Nếu thành công, hàm này trả về một đối tượng

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
1. Bây giờ bạn có thể sử dụng các thuộc tính thể hiện để kiểm tra nội dung tệp

>>> print[im.format, im.size, im.mode]
PPM [512, 512] RGB

Thuộc tính

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
5 xác định nguồn của hình ảnh. Nếu hình ảnh không được đọc từ một tệp, nó được đặt thành Không có. Thuộc tính kích thước là 2-tuple chứa chiều rộng và chiều cao [tính bằng pixel]. Thuộc tính
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
6 xác định số lượng và tên của các dải trong ảnh, cũng như loại pixel và độ sâu. Các chế độ phổ biến là “L” [độ sáng] cho ảnh thang độ xám, “RGB” cho ảnh màu trung thực và “CMYK” cho ảnh in sẵn

Nếu tệp không thể mở được, một ngoại lệ

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
7 sẽ được đưa ra

Khi bạn có một thể hiện của lớp

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
1, bạn có thể sử dụng các phương thức được định nghĩa bởi lớp này để xử lý và thao tác với hình ảnh. Ví dụ: hãy hiển thị hình ảnh chúng ta vừa tải

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
0

Ghi chú

Phiên bản tiêu chuẩn của

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
9 không hiệu quả lắm, vì nó lưu hình ảnh vào một tệp tạm thời và gọi một tiện ích để hiển thị hình ảnh. Nếu bạn không cài đặt một tiện ích phù hợp, nó thậm chí sẽ không hoạt động. Tuy nhiên, khi nó hoạt động, nó rất thuận tiện để gỡ lỗi và kiểm tra

Các phần sau đây cung cấp tổng quan về các chức năng khác nhau được cung cấp trong thư viện này

Đọc và ghi hình ảnh#

Thư viện hình ảnh Python hỗ trợ nhiều định dạng tệp hình ảnh. Để đọc tệp từ đĩa, hãy sử dụng hàm

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
2 trong mô-đun
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
1. Bạn không cần phải biết định dạng tệp để mở tệp. Thư viện tự động xác định định dạng dựa trên nội dung của tệp

Để lưu tệp, hãy sử dụng phương thức

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
42 của lớp
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
1. Khi lưu tệp, tên trở nên quan trọng. Trừ khi bạn chỉ định định dạng, thư viện sẽ sử dụng phần mở rộng tên tệp để khám phá định dạng lưu trữ tệp nào sẽ sử dụng

Chuyển đổi tập tin sang JPEG#

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]

Đối số thứ hai có thể được cung cấp cho phương thức

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
42 chỉ định rõ ràng định dạng tệp. Nếu bạn sử dụng tiện ích mở rộng không chuẩn, bạn phải luôn chỉ định định dạng theo cách này

Tạo hình thu nhỏ JPEG#

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
4

Điều quan trọng cần lưu ý là thư viện không giải mã hoặc tải dữ liệu raster trừ khi nó thực sự cần thiết. Khi bạn mở một tệp, tiêu đề tệp được đọc để xác định định dạng tệp và trích xuất những thứ như chế độ, kích thước và các thuộc tính khác cần thiết để giải mã tệp, nhưng phần còn lại của tệp không được xử lý cho đến sau này

Điều này có nghĩa là mở tệp hình ảnh là một thao tác nhanh, không phụ thuộc vào kích thước tệp và kiểu nén. Đây là một tập lệnh đơn giản để nhanh chóng xác định một tập hợp các tệp hình ảnh

Xác định tệp hình ảnh #

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
9

Cắt, dán và hợp nhất hình ảnh#

Lớp

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
1 chứa các phương thức cho phép bạn thao tác các vùng trong một hình ảnh. Để trích xuất một hình chữ nhật con từ một hình ảnh, hãy sử dụng phương pháp
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
46

Sao chép một hình chữ nhật con từ một hình ảnh#

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
2

Vùng được xác định bởi 4-tuple, trong đó tọa độ là [trái, trên, phải, dưới]. Thư viện hình ảnh Python sử dụng hệ tọa độ với [0, 0] ở góc trên bên trái. Cũng lưu ý rằng tọa độ đề cập đến vị trí giữa các pixel, vì vậy vùng trong ví dụ trên chính xác là 300x300 pixel

Vùng hiện có thể được xử lý theo một cách nhất định và dán lại

Đang xử lý một hình chữ nhật con và dán lại#

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
3

Khi dán lại các vùng, kích thước của vùng phải khớp chính xác với vùng đã cho. Ngoài ra, khu vực không thể mở rộng ra bên ngoài hình ảnh. Tuy nhiên, các chế độ của ảnh gốc và vùng không cần phải khớp. Nếu không, vùng sẽ tự động được chuyển đổi trước khi được dán [xem phần về Chuyển đổi màu bên dưới để biết chi tiết].

Đây là một ví dụ bổ sung

cuộn một hình ảnh

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
4

Hoặc nếu bạn muốn hợp nhất hai hình ảnh thành một hình ảnh rộng hơn

Hợp nhất hình ảnh #

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
5

Đối với các thủ thuật nâng cao hơn, phương thức dán cũng có thể lấy mặt nạ trong suốt làm đối số tùy chọn. Trong mặt nạ này, giá trị 255 cho biết rằng hình ảnh đã dán bị mờ ở vị trí đó [nghĩa là hình ảnh đã dán nên được sử dụng nguyên trạng]. Giá trị 0 có nghĩa là hình ảnh đã dán hoàn toàn trong suốt. Các giá trị ở giữa biểu thị các mức độ minh bạch khác nhau. Ví dụ: dán một hình ảnh RGBA và cũng sử dụng nó làm mặt nạ sẽ dán phần mờ của hình ảnh chứ không phải nền trong suốt của nó

Thư viện hình ảnh Python cũng cho phép bạn làm việc với các dải riêng lẻ của hình ảnh nhiều dải, chẳng hạn như hình ảnh RGB. Phương pháp phân tách tạo ra một tập hợp các hình ảnh mới, mỗi hình ảnh chứa một dải từ hình ảnh nhiều dải ban đầu. Hàm hợp nhất lấy một chế độ và một bộ ảnh rồi kết hợp chúng thành một ảnh mới. Mẫu sau hoán đổi ba dải của hình ảnh RGB

Tách và hợp nhất các dải#

>>> print[im.format, im.size, im.mode]
PPM [512, 512] RGB
0

Lưu ý rằng đối với hình ảnh một băng tần,

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
47 trả về chính hình ảnh đó. Để làm việc với các dải màu riêng lẻ, trước tiên bạn có thể muốn chuyển đổi hình ảnh thành "RGB"

phép biến đổi hình học

Lớp

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
48 chứa các phương thức để
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
49 và
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
90 một hình ảnh. Cái trước lấy một tuple cho kích thước mới, cái sau lấy góc theo độ ngược chiều kim đồng hồ

Biến đổi hình học đơn giản

>>> print[im.format, im.size, im.mode]
PPM [512, 512] RGB
1

Để xoay hình ảnh theo các bước 90 độ, bạn có thể sử dụng phương pháp

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
90 hoặc phương pháp
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
92. Cái sau cũng có thể được sử dụng để lật hình ảnh quanh trục ngang hoặc dọc của nó

Chuyển đổi một hình ảnh

>>> print[im.format, im.size, im.mode]
PPM [512, 512] RGB
2

Các thao tác

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
93 cũng có thể được thực hiện giống hệt với các thao tác
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
90, miễn là cờ
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
95 là đúng, để cung cấp các thay đổi tương tự đối với kích thước của hình ảnh

Một hình thức biến đổi hình ảnh tổng quát hơn có thể được thực hiện thông qua phương pháp

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
96

Biến đổi màu #

Thư viện hình ảnh Python cho phép bạn chuyển đổi hình ảnh giữa các biểu diễn pixel khác nhau bằng phương pháp

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
97

Chuyển đổi giữa các chế độ

>>> print[im.format, im.size, im.mode]
PPM [512, 512] RGB
3

Thư viện hỗ trợ chuyển đổi giữa từng chế độ được hỗ trợ và chế độ “L” và “RGB”. Để chuyển đổi giữa các chế độ khác, bạn có thể phải sử dụng một hình ảnh trung gian [thường là hình ảnh “RGB”]

nâng cao hình ảnh#

Thư viện hình ảnh Python cung cấp một số phương thức và mô-đun có thể được sử dụng để nâng cao hình ảnh

Bộ lọc #

Mô-đun

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
98 chứa một số bộ lọc nâng cao được xác định trước có thể được sử dụng với phương pháp
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
99

Áp dụng bộ lọc#

>>> print[im.format, im.size, im.mode]
PPM [512, 512] RGB
4

Hoạt động điểm #

Phương pháp

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
20 có thể được sử dụng để dịch các giá trị pixel của một hình ảnh [e. g. thao tác tương phản hình ảnh]. Trong hầu hết các trường hợp, một đối tượng hàm mong đợi một đối số có thể được truyền cho phương thức này. Mỗi pixel được xử lý theo chức năng đó

Áp dụng biến đổi điểm

>>> print[im.format, im.size, im.mode]
PPM [512, 512] RGB
5

Sử dụng kỹ thuật trên, bạn có thể nhanh chóng áp dụng bất kỳ biểu thức đơn giản nào cho hình ảnh. Bạn cũng có thể kết hợp các phương pháp

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
20 và
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
22 để sửa đổi một cách có chọn lọc một hình ảnh

Xử lý các dải riêng lẻ

>>> print[im.format, im.size, im.mode]
PPM [512, 512] RGB
6

Lưu ý cú pháp được sử dụng để tạo mặt nạ

>>> print[im.format, im.size, im.mode]
PPM [512, 512] RGB
7

Python chỉ đánh giá một phần của biểu thức logic khi cần thiết để xác định kết quả và trả về giá trị cuối cùng được kiểm tra là kết quả của biểu thức. Vì vậy, nếu biểu thức trên là sai [0], Python không nhìn vào toán hạng thứ hai và do đó trả về 0. Nếu không, nó trả về 255

sự nâng cao#

Để cải thiện hình ảnh nâng cao hơn, bạn có thể sử dụng các lớp trong mô-đun

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
23. Sau khi được tạo từ một hình ảnh, một đối tượng nâng cao có thể được sử dụng để nhanh chóng thử các cài đặt khác nhau

Bạn có thể điều chỉnh độ tương phản, độ sáng, cân bằng màu sắc và độ sắc nét theo cách này

Tăng cường hình ảnh#

>>> print[im.format, im.size, im.mode]
PPM [512, 512] RGB
8

Chuỗi hình ảnh #

Thư viện hình ảnh Python chứa một số hỗ trợ cơ bản cho chuỗi hình ảnh [còn được gọi là định dạng hoạt ảnh]. Các định dạng trình tự được hỗ trợ bao gồm FLI/FLC, GIF và một số định dạng thử nghiệm. Các tệp TIFF cũng có thể chứa nhiều hơn một khung hình

Khi bạn mở tệp trình tự, PIL sẽ tự động tải khung hình đầu tiên trong trình tự. Bạn có thể sử dụng các phương thức tìm kiếm và cho biết để di chuyển giữa các khung hình khác nhau

Trình tự đọc #

>>> print[im.format, im.size, im.mode]
PPM [512, 512] RGB
9

Như đã thấy trong ví dụ này, bạn sẽ nhận được một ngoại lệ

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
24 khi chuỗi kết thúc

Lớp sau cho phép bạn sử dụng câu lệnh for để lặp qua chuỗi

Sử dụng lớp ImageSequence Iterator#

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
00

In PostScript#

Thư viện hình ảnh Python bao gồm các chức năng in hình ảnh, văn bản và đồ họa trên máy in PostScript. Đây là một ví dụ đơn giản

Vẽ PostScript#

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
01

Thêm về đọc hình ảnh #

Như đã mô tả trước đó, chức năng

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
2 của mô-đun
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
1 được sử dụng để mở tệp hình ảnh. Trong hầu hết các trường hợp, bạn chỉ cần chuyển tên tệp làm đối số.
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
27 có thể được sử dụng làm trình quản lý ngữ cảnh

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
02

Nếu mọi thứ suôn sẻ, kết quả là một đối tượng

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
48. Mặt khác, một ngoại lệ
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
7 được đưa ra

Bạn có thể sử dụng một đối tượng giống như tệp thay vì tên tệp. Đối tượng phải triển khai các phương thức

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
30,
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
31 và
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
32 và được mở ở chế độ nhị phân

Đang đọc từ một tệp đang mở#

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
03

Để đọc một hình ảnh từ dữ liệu nhị phân, hãy sử dụng lớp

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
33

Đọc từ dữ liệu nhị phân#

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
04

Lưu ý rằng thư viện sẽ tua lại tệp [sử dụng

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
34] trước khi đọc tiêu đề hình ảnh. Ngoài ra, seek cũng sẽ được sử dụng khi dữ liệu hình ảnh được đọc [theo phương thức load]. Nếu tệp hình ảnh được nhúng trong tệp lớn hơn, chẳng hạn như tệp tar, bạn có thể sử dụng mô-đun
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
35 hoặc
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
36 để truy cập tệp

Đọc từ URL#

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
05

Đọc từ kho lưu trữ tar#

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
06

xử lý hàng loạt#

Có thể áp dụng các thao tác cho nhiều tệp hình ảnh. Ví dụ: tất cả hình ảnh PNG trong thư mục hiện tại có thể được lưu dưới dạng JPEG với chất lượng giảm

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
07

Vì hình ảnh cũng có thể được mở từ

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
37 từ mô-đun
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
38, ví dụ này có thể được sửa đổi để sử dụng
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
38 thay vì mô-đun
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
40

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
08

Điều khiển bộ giải mã #

Một số bộ giải mã cho phép bạn thao tác với hình ảnh trong khi đọc nó từ một tệp. Điều này thường có thể được sử dụng để tăng tốc độ giải mã khi tạo hình thu nhỏ [khi tốc độ thường quan trọng hơn chất lượng] và in bằng máy in laser đơn sắc [khi chỉ cần một phiên bản thang độ xám của hình ảnh]

Phương pháp

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
41 xử lý một hình ảnh đã mở nhưng chưa được tải để nó khớp với chế độ và kích thước đã cho nhất có thể. Điều này được thực hiện bằng cách cấu hình lại bộ giải mã hình ảnh

Đọc ở chế độ nháp#

Điều này chỉ khả dụng cho các tệp JPEG và MPO

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
09

Điều này in một cái gì đó giống như

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext[infile]
    outfile = f + ".jpg"
    if infile != outfile:
        try:
            with Image.open[infile] as im:
                im.save[outfile]
        except OSError:
            print["cannot convert", infile]
0

Lưu ý rằng hình ảnh kết quả có thể không khớp chính xác với chế độ và kích thước được yêu cầu. Để đảm bảo rằng hình ảnh không lớn hơn kích thước đã cho, thay vào đó hãy sử dụng phương pháp hình thu nhỏ

Chủ Đề