Nếu tôi chạy mã đó trong cùng thư mục với một tệp có tên "picture.jpg", tôi sẽ gặp lỗi: Traceback [most recent call last]: File "test.py", line 2, in import Image ImportError: No module named Image
. HĐH, nhập khẩu SYS là không liên quan; Bạn có thể có nghĩa là from PIL import Image
, yêu cầu chạy easy_install PIL
hoặc nếu bạn không cảm thấy may mắn [PIL đòi hỏi một lượng may mắn đáng kể để cài đặt], easy_install pillow
.
- & nbsp; chbrown
Ngày 20 tháng 7 năm 2014 lúc 21:33
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
- Bàn luận Imageio is a Python library that provides an easy interface to read and write a wide range of image data, including animated images, video, volumetric data, and scientific formats. It is cross-platform, runs on Python 3.7+, and is easy to install. It is the recommended alternative to scipy.misc.imread and is used internally by libraries like scikit-image to load images.
Python3
Python hỗ trợ các công cụ rất mạnh mẽ khi đến xử lý hình ảnh. Hãy cùng xem cách xử lý các hình ảnh bằng các thư viện khác nhau như ImageIO, OpenCV, Matplotlib, Pil, v.v. & NBSP;
Sử dụng ImageIO: ImageIO là một thư viện Python cung cấp giao diện dễ dàng để đọc và viết một loạt dữ liệu hình ảnh, bao gồm hình ảnh hoạt hình, video, dữ liệu thể tích và các định dạng khoa học. Đó là nền tảng chéo, chạy trên Python 3.7+ và dễ cài đặt. Đây là giải pháp thay thế được đề xuất cho scipy.misc.imread và được sử dụng nội bộ bởi các thư viện như hình ảnh scikit để tải hình ảnh.
Traceback [most recent call last]: File "test.py", line 2, in import Image ImportError: No module named Image
2Traceback [most recent call last]: File "test.py", line 2, in import Image ImportError: No module named Image
3Traceback [most recent call last]: File "test.py", line 2, in import Image ImportError: No module named Image
4
Output:
import
imageio as iio
OpenCV [Open Source Computer Vision] is a computer vision library that contains various functions to perform operations on pictures or videos. It was originally developed by Intel but was later maintained by Willow Garage and is now maintained by Itseez. This library is cross-platform that is it is available on multiple programming languages such as Python, C++ etc.
Python3
img
=
iio.imread[
Traceback [most recent call last]: File "test.py", line 2, in import Image ImportError: No module named Image
0Traceback [most recent call last]: File "test.py", line 2, in import Image ImportError: No module named Image
1
Sử dụng OpenCV: OpenCV [Tầm nhìn máy tính nguồn mở] là thư viện tầm nhìn máy tính chứa các chức năng khác nhau để thực hiện các hoạt động trên hình ảnh hoặc video. Nó ban đầu được phát triển bởi Intel nhưng sau đó được duy trì bởi Willow Garage và hiện được duy trì bởi Itseez. Thư viện này là nền tảng chéo, nó có sẵn trên nhiều ngôn ngữ lập trình như Python, C ++, v.v. & NBSP; & NBSP;
from PIL import Image
2from PIL import Image
3Traceback [most recent call last]: File "test.py", line 2, in import Image ImportError: No module named Image
4
from PIL import Image
5from PIL import Image
6from PIL import Image
7
from PIL import Image
8
import
Traceback [most recent call last]: File "test.py", line 2, in import Image ImportError: No module named Image
6
img
=
Traceback [most recent call last]: File "test.py", line 2, in import Image ImportError: No module named Image
9from PIL import Image
0Traceback [most recent call last]: File "test.py", line 2, in import Image ImportError: No module named Image
1
- Đầu ra: & nbsp; Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2002. Matplotlib comes with a wide variety of plots. Plots helps to understand trends, patterns, and to make correlations. They’re typically instruments for reasoning about quantitative
information.
& nbsp; & nbsp; & nbsp;
Sử dụng matplotlib: matplotlib là một thư viện trực quan tuyệt vời trong Python cho các lô 2D của mảng. Matplotlib là một thư viện trực quan hóa dữ liệu đa nền tảng được xây dựng trên các mảng numpy và được thiết kế để hoạt động với ngăn xếp Scipy rộng hơn. Nó được giới thiệu bởi John Hunter vào năm 2002. Matplotlib đi kèm với nhiều lô khác nhau. Các lô giúp hiểu xu hướng, mẫu và để tạo ra mối tương quan. Họ thường là các công cụ để lý luận về thông tin định lượng. & Nbsp; & nbsp;
Python
import
easy_install PIL
0
easy_install PIL
8
import
Traceback [most recent call last]: File "test.py", line 2, in import Image ImportError: No module named Image
6
img
=
Traceback [most recent call last]: File "test.py", line 2, in import Image ImportError: No module named Image
9from PIL import Image
0Traceback [most recent call last]: File "test.py", line 2, in import Image ImportError: No module named Image
1 PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. It was developed by Fredrik Lundh and several other contributors. Pillow is the friendly PIL fork and an easy to use library developed by Alex Clark and other contributors.
& nbsp; & nbsp; & nbsp;
Sử dụng matplotlib: matplotlib là một thư viện trực quan tuyệt vời trong Python cho các lô 2D của mảng. Matplotlib là một thư viện trực quan hóa dữ liệu đa nền tảng được xây dựng trên các mảng numpy và được thiết kế để hoạt động với ngăn xếp Scipy rộng hơn. Nó được giới thiệu bởi John Hunter vào năm 2002. Matplotlib đi kèm với nhiều lô khác nhau. Các lô giúp hiểu xu hướng, mẫu và để tạo ra mối tương quan. Họ thường là các công cụ để lý luận về thông tin định lượng. & Nbsp; & nbsp;
Python
import
0
import
1import
2import
3Traceback [most recent call last]: File "test.py", line 2, in import Image ImportError: No module named Image
1
import
1import
6
import
easy_install PIL
0
PNG RGBA
Python cung cấp thư viện để xác định loại hình ảnh, trên thư viện như vậy là IMGHDR. Gói Python IMGHDR xác định loại hình ảnh có trong một tệp hoặc luồng byte.
Làm cách nào để có được đường dẫn hình ảnh trong Python?
Python cung cấp thư viện để xác định loại hình ảnh, trên thư viện như vậy là IMGHDR. Gói Python IMGHDR xác định loại hình ảnh có trong một tệp hoặc luồng byte.
Làm cách nào để có được đường dẫn hình ảnh trong Python?
Để có được thư mục làm việc hiện tại trong Python, hãy sử dụng phương thức Os.getCwd []. Hàm này của mô -đun HĐH Python trả về chuỗi chứa đường dẫn tuyệt đối đến thư mục làm việc hiện tại.
Cải thiện bài viết
Lưu bài viết
Đọc PIL.Image.open[fp, mode=’r’]
Parameters::
FP - Tên tệp [Chuỗi], Đối tượng Pathlib.Path hoặc đối tượng Tệp. Đối tượng tệp phải thực hiện các phương thức Read [], Seek [] và Tell [] và được mở ở chế độ nhị phân.Mode - chế độ. Nếu được đưa ra, đối số này phải là R R. – A filename [string], pathlib.Path object or a file object. The file object must implement read[], seek[], and tell[] methods, and be opened in binary mode.
mode – The mode. If given, this argument must be “r”.Trả về Loại: Một đối tượng hình ảnh.: An image object.
Raises: IOError – If the file cannot be found, or the image cannot be opened and identified.
Hình ảnh được sử dụng:
easy_install PIL
9 easy_install pillow
0import
easy_install pillow
2
‘
img
0
Đầu ra: .jpg hình ảnh mở rộng mở..JPG extension image open.
Một ví dụ khác: Ở đây chúng tôi đã sử dụng tệp mở rộng .png. Here we used .PNG extension file.
Hình ảnh được sử dụng:
easy_install PIL
9 easy_install pillow
0import
easy_install pillow
2
‘
img
0
Đầu ra: .jpg hình ảnh mở rộng mở..PNG extension image
open.