Python có hỗ trợ các tệp jpg không?

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_______1
    sudo apt-get update
    sudo apt-get install python-pip
    2
    sudo apt-get update
    sudo apt-get install python-pip
    3
    sudo apt-get update
    sudo apt-get install python-pip
    4

    sudo 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
    8

    sudo 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_______2_______4
     from PIL import Image
    5

     from PIL import Image
    6

     from PIL import Image
    7
    sudo apt-get update
    sudo apt-get install python-pip
    1
     from PIL import Image
    9

    #img  = Image.open[path]     0

    sudo 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]     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____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
    2
     from PIL import Image
    3_______2_______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# 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]6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo apt-get update
    sudo apt-get install python-pip
    2_______1_______3  0  1  2

      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]6

    sudo apt-get update
    sudo apt-get install python-pip
    1   9try0  2

      3

      3____191_______4

    # an object of Image type is returned and stored in img variable]3try6try7try8

        

    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

    sudo apt-get update
    sudo apt-get install python-pip
    8

     from PIL import Image
    6

    5 6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo 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_______2_______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# 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]6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo apt-get update
    sudo apt-get install python-pip
    2_______1_______3  0  1try8

    # an object of Image type is returned and stored in img variable]3____187_______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

    sudo apt-get update
    sudo apt-get install python-pip
    19
    sudo apt-get update
    sudo apt-get install python-pip
    1   0
    sudo apt-get update
    sudo apt-get install python-pip
    22
    sudo apt-get update
    sudo apt-get install python-pip
    23
    sudo apt-get update
    sudo apt-get install python-pip
    22
    sudo apt-get update
    sudo apt-get install python-pip
    25
    sudo apt-get update
    sudo apt-get install python-pip
    26
    sudo apt-get update
    sudo apt-get install python-pip
    27
    sudo apt-get update
    sudo apt-get install python-pip
    28
    sudo apt-get update
    sudo apt-get install python-pip
    26
    sudo apt-get update
    sudo apt-get install python-pip
    27try8

    # an object of Image type is returned and stored in img variable]3____189_______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]3try4

    # an object of Image type is returned and stored in img variable]3try6

    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

    sudo apt-get update
    sudo apt-get install python-pip
    8

     from PIL import Image
    6

    5 6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo 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_______2_______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# 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]6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo apt-get update
    sudo apt-get install python-pip
    2_______1_______3  0  1try8

    # an object of Image type is returned and stored in img variable]3____187_______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# an object of Image type is returned and stored in img variable]6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo apt-get update
    sudo apt-get install python-pip
    86
    sudo apt-get update
    sudo apt-get install python-pip
    26
    sudo apt-get update
    sudo apt-get install python-pip
    27
    sudo apt-get update
    sudo apt-get install python-pip
    28
    sudo apt-get update
    sudo apt-get install python-pip
    26
    sudo apt-get update
    sudo apt-get install python-pip
    27
    sudo apt-get update
    sudo apt-get install python-pip
    92

      3

    # an object of Image type is returned and stored in img variable]3try4

    # an object of Image type is returned and stored in img variable]3try6

    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

    sudo apt-get update
    sudo apt-get install python-pip
    8

     from PIL import Image
    6

    5 6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo 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_______2_______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# 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

     from 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]6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo apt-get update
    sudo apt-get install python-pip
    2_______1_______3  0  1  2

      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]3

     from PIL import Image
    42
    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo apt-get update
    sudo apt-get install python-pip
    2_______1_______3  0
     from PIL import Image
    47  2

    # an object of Image type is returned and stored in img variable]3

     from PIL import Image
    50
     from PIL import Image
    51
    sudo apt-get update
    sudo apt-get install python-pip
    23
     from PIL import Image
    51
    sudo apt-get update
    sudo apt-get install python-pip
    92

      3

    # an object of Image type is returned and stored in img variable]3try4

    # an object of Image type is returned and stored in img variable]3try6

     from PIL import Image
    60try8

      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

    sudo apt-get update
    sudo apt-get install python-pip
    8

     from PIL import Image
    6

    5 6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo 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# 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]6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo apt-get update
    sudo apt-get install python-pip
    2_______1_______3  0  1  2

      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_______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

    sudo apt-get update
    sudo apt-get install python-pip
    8

     from PIL import Image
    6

    5 6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo 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# 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]6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo apt-get update
    sudo apt-get install python-pip
    2_______1_______3  0  1  2

      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]     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#img  = Image.open[path]     45

    # an object of Image type is returned and stored in img variable]3____187_______47#img  = Image.open[path]     48try8

        

    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

    sudo apt-get update
    sudo apt-get install python-pip
    8

     from PIL import Image
    6

    5 6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo 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_______2_______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# 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]6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo apt-get update
    sudo apt-get install python-pip
    2_______1_______3  0  1  2

      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]     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

    sudo apt-get update
    sudo apt-get install python-pip
    8

     from PIL import Image
    6

    5 6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo 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# 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]6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo apt-get update
    sudo apt-get install python-pip
    2_______1_______3  0  1  2

    # an object of Image type is returned and stored in img variable]3

     from PIL import Image
    98 # On successful execution of this statement,21

      3

    # 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]3

     from PIL import Image
    98 # On successful execution of this statement,27

      3

    # an object of Image type is returned and stored in img variable]3

     from PIL import Image
    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

    sudo apt-get update
    sudo apt-get install python-pip
    8

     from PIL import Image
    6

    5 6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo 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# 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]6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo apt-get update
    sudo apt-get install python-pip
    2_______1_______3  0  1  2

      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,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]3try6# On successful execution of this statement,74try8

        

    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

    sudo apt-get update
    sudo apt-get install python-pip
    8

     from PIL import Image
    6

    5 6

    sudo apt-get update
    sudo apt-get install python-pip
    1
    sudo 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

Làm cách nào để đọc tệp JPG bằng Python?

Đọc một hình ảnh . use cv2. Hàm imread[] để đọc ảnh. Hình ảnh phải được đặt trong thư mục làm việc hiện tại nếu không chúng tôi cần cung cấp đường dẫn tuyệt đối.

Định dạng hình ảnh nào được hỗ trợ trong Python?

Xem Sổ tay Thư viện Hình ảnh Python để biết danh sách các định dạng như vậy. Định dạng lưu duy nhất được hỗ trợ là PNG , trừ khi PIL được cài đặt, trong trường hợp đó, bất kỳ định dạng nào nó hỗ trợ đều có thể được ghi.

Python có thể làm việc với hình ảnh không?

Thư viện hình ảnh Python [PIL] là gói Python của bên thứ 3 bổ sung khả năng xử lý hình ảnh cho trình thông dịch Python của bạn . Nó cho phép bạn xử lý ảnh và thực hiện nhiều thao tác với tệp ảnh phổ biến.

Tôi có thể hiển thị hình ảnh bằng Python không?

Python có nhiều thư viện khác nhau có thể dùng để hiển thị hình ảnh . Ba trong số các thư viện phổ biến nhất là Gối, Matplotlib và OpenCV.

Chủ Đề