Hướng dẫn how do you read an image as a numpy array in python? - làm thế nào để bạn đọc một hình ảnh dưới dạng một mảng numpy trong python?

Giới thiệu

Trong học máy, Python sử dụng dữ liệu hình ảnh dưới dạng một mảng numpy, tức là, [chiều cao, chiều rộng, kênh] định dạng. Để tăng cường hiệu suất của mô hình dự đoán, chúng ta phải biết cách tải và thao tác hình ảnh. Trong Python, chúng ta có thể thực hiện một nhiệm vụ theo những cách khác nhau. Chúng tôi có các tùy chọn từ Numpy đến Pytorch và Cuda, tùy thuộc vào sự phức tạp của vấn đề.

Show

Đến cuối hướng dẫn này, bạn sẽ có kinh nghiệm thực hành với:

  • Tải và hiển thị hình ảnh bằng API Matplotlib, OpenCV và Keras
  • Chuyển đổi các hình ảnh được tải thành mảng và trở lại Numpy
  • Tiến hành thao tác cơ bản của một hình ảnh bằng cách sử dụng các thư viện gối và numpy và lưu nó vào hệ thống địa phương của bạn.
  • Đọc hình ảnh dưới dạng mảng trong API Keras và OpenCV

Thư viện gối

Gối là một công cụ thao tác hình ảnh ưa thích. Python Phiên bản 2 đã sử dụng Thư viện hình ảnh Python (PIL) và Python phiên bản 3 sử dụng Thư viện Gối Python, một bản nâng cấp của PIL.

Trước tiên bạn nên tạo một môi trường ảo trong Anaconda cho các dự án khác nhau. Hãy chắc chắn rằng bạn có các gói hỗ trợ như Numpy, Scipy và Matplotlib để cài đặt trong môi trường ảo bạn tạo.

Khi bạn thiết lập các gói, bạn có thể dễ dàng cài đặt gối bằng

1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
2.
1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
2
.

Bạn có thể xác nhận rằng thư viện được cài đặt chính xác bằng cách kiểm tra phiên bản của nó.

1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__)

Python

Tuyệt quá! Phiên bản mới nhất hiện được tải xuống. Hãy chuyển sang bước tiếp theo.

Tải hình ảnh

Ở đây chúng tôi sẽ tìm hiểu hai cách để tải và lấy chi tiết của một hình ảnh: sử dụng thư viện gối và sử dụng thư viện matplotlib.

Phương pháp 1: Thư viện gối

Chọn một hình ảnh thử nghiệm để tải và làm việc với thư viện Gối (PIL). Hình ảnh có thể là PNG hoặc JPEG. Trong ví dụ này, chúng tôi sẽ sử dụng một hình ảnh có tên Kolala.jpeg. Hoặc tải lên hình ảnh trong thư mục làm việc hoặc đưa ra đường dẫn mong muốn của bạn. Lớp

1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
3 là trái tim của
1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
4 và các thuộc tính của nó giúp thao tác với các pixel, định dạng và độ tương phản của hình ảnh.
1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
3
class is the heart of
1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
4
, and its properties help manipulate the pixels, format, and contrast of the image.

Hướng dẫn how do you read an image as a numpy array in python? - làm thế nào để bạn đọc một hình ảnh dưới dạng một mảng numpy trong python?

Lớp

1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
3 sử dụng các chức năng này:*
1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
3
class uses these functions:*

  • 1# load and show an image with Pillow
    2from PIL import Image
    3# Open the image form working directory
    4image = Image.open('kolala.jpeg')
    5# summarize some details about the image
    6print(image.format)
    7print(image.size)
    8print(image.mode)
    9# show the image
    10load_image.show()
    6: Điều này có thể tải trực tiếp hình ảnh. Nó có các thuộc tính
    1# load and show an image with Pillow
    2from PIL import Image
    3# Open the image form working directory
    4image = Image.open('kolala.jpeg')
    5# summarize some details about the image
    6print(image.format)
    7print(image.size)
    8print(image.mode)
    9# show the image
    10load_image.show()
    7 như
    1# load and show an image with Pillow
    2from PIL import Image
    3# Open the image form working directory
    4image = Image.open('kolala.jpeg')
    5# summarize some details about the image
    6print(image.format)
    7print(image.size)
    8print(image.mode)
    9# show the image
    10load_image.show()
    8, cung cấp thông tin về định dạng tệp kỹ thuật số của hình ảnh, ________ 19Size`, hiển thị kích thước của hình ảnh trong pixel (ví dụ: 480x240).: This can directly load the image. It has
    1# load and show an image with Pillow
    2from PIL import Image
    3# Open the image form working directory
    4image = Image.open('kolala.jpeg')
    5# summarize some details about the image
    6print(image.format)
    7print(image.size)
    8print(image.mode)
    9# show the image
    10load_image.show()
    7
    properties like
    1# load and show an image with Pillow
    2from PIL import Image
    3# Open the image form working directory
    4image = Image.open('kolala.jpeg')
    5# summarize some details about the image
    6print(image.format)
    7print(image.size)
    8print(image.mode)
    9# show the image
    10load_image.show()
    8
    , which gives information about the digital file format of an image,
    1# load and show an image with Pillow
    2from PIL import Image
    3# Open the image form working directory
    4image = Image.open('kolala.jpeg')
    5# summarize some details about the image
    6print(image.format)
    7print(image.size)
    8print(image.mode)
    9# show the image
    10load_image.show()
    9
    size`, which displays the dimensions of the image in pixels (e.g., 480x240).

  • 1# load and display an image with Matplotlib
    2from matplotlib import image
    3from matplotlib import pyplot
    4# load image as pixel array
    5image = image.imread('kolala.jpeg')
    6# summarize shape of the pixel array
    7print(image.dtype)
    8print(image.shape)
    9# display the array of pixels as an image
    10pyplot.imshow(image)
    11pyplot.show()
    0: Điều này sẽ hiển thị hình ảnh. Ứng dụng xem trước ảnh mặc định của bạn sẽ bật lên.
    : This will display the image. Your default photo preview application will pop up.

1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()

Python

Tuyệt quá! Phiên bản mới nhất hiện được tải xuống. Hãy chuyển sang bước tiếp theo.

Tải hình ảnh

1# load and display an image with Matplotlib
2from matplotlib import image
3from matplotlib import pyplot
4# load image as pixel array
5image = image.imread('kolala.jpeg')
6# summarize shape of the pixel array
7print(image.dtype)
8print(image.shape)
9# display the array of pixels as an image
10pyplot.imshow(image)
11pyplot.show()
1 class that performs the same function. Functions used in this piece of code are
1# load and display an image with Matplotlib
2from matplotlib import image
3from matplotlib import pyplot
4# load image as pixel array
5image = image.imread('kolala.jpeg')
6# summarize shape of the pixel array
7print(image.dtype)
8print(image.shape)
9# display the array of pixels as an image
10pyplot.imshow(image)
11pyplot.show()
2
, which loads the image in the form of an array of the pixel and
1# load and display an image with Matplotlib
2from matplotlib import image
3from matplotlib import pyplot
4# load image as pixel array
5image = image.imread('kolala.jpeg')
6# summarize shape of the pixel array
7print(image.dtype)
8print(image.shape)
9# display the array of pixels as an image
10pyplot.imshow(image)
11pyplot.show()
3
, which displays that image.

Ở đây chúng tôi sẽ tìm hiểu hai cách để tải và lấy chi tiết của một hình ảnh: sử dụng thư viện gối và sử dụng thư viện matplotlib.

1# load and display an image with Matplotlib
2from matplotlib import image
3from matplotlib import pyplot
4# load image as pixel array
5image = image.imread('kolala.jpeg')
6# summarize shape of the pixel array
7print(image.dtype)
8print(image.shape)
9# display the array of pixels as an image
10pyplot.imshow(image)
11pyplot.show()
4 class from the Matplotlib library to plot the image into the frame.

1# load and display an image with Matplotlib
2from matplotlib import image
3from matplotlib import pyplot
4# load image as pixel array
5image = image.imread('kolala.jpeg')
6# summarize shape of the pixel array
7print(image.dtype)
8print(image.shape)
9# display the array of pixels as an image
10pyplot.imshow(image)
11pyplot.show()

Python

Tuyệt quá! Phiên bản mới nhất hiện được tải xuống. Hãy chuyển sang bước tiếp theo.

1# load and display an image with Matplotlib
2from matplotlib import image
3from matplotlib import pyplot
4# load image as pixel array
5image = image.imread('kolala.jpeg')
6# summarize shape of the pixel array
7print(image.dtype)
8print(image.shape)
9# display the array of pixels as an image
10pyplot.imshow(image)
11pyplot.show()
5 argument, we get a report on the data type of the array. In this case, it is 8-bit unsigned integers. The shape of the array is 800 pixels wide by 450 pixels high and
1# load and display an image with Matplotlib
2from matplotlib import image
3from matplotlib import pyplot
4# load image as pixel array
5image = image.imread('kolala.jpeg')
6# summarize shape of the pixel array
7print(image.dtype)
8print(image.shape)
9# display the array of pixels as an image
10pyplot.imshow(image)
11pyplot.show()
6
denotes color channels for red, green, and blue.

Hướng dẫn how do you read an image as a numpy array in python? - làm thế nào để bạn đọc một hình ảnh dưới dạng một mảng numpy trong python?

Tải hình ảnh

Ở đây chúng tôi sẽ tìm hiểu hai cách để tải và lấy chi tiết của một hình ảnh: sử dụng thư viện gối và sử dụng thư viện matplotlib.

Phương pháp 1: Thư viện gối

1# load and display an image with Matplotlib
2from matplotlib import image
3from matplotlib import pyplot
4# load image as pixel array
5image = image.imread('kolala.jpeg')
6# summarize shape of the pixel array
7print(image.dtype)
8print(image.shape)
9# display the array of pixels as an image
10pyplot.imshow(image)
11pyplot.show()
7 class to convert PIL images into NumPy arrays. The
1# load and display an image with Matplotlib
2from matplotlib import image
3from matplotlib import pyplot
4# load image as pixel array
5image = image.imread('kolala.jpeg')
6# summarize shape of the pixel array
7print(image.dtype)
8print(image.shape)
9# display the array of pixels as an image
10pyplot.imshow(image)
11pyplot.show()
8
function also produce the same result. The
1# load and display an image with Matplotlib
2from matplotlib import image
3from matplotlib import pyplot
4# load image as pixel array
5image = image.imread('kolala.jpeg')
6# summarize shape of the pixel array
7print(image.dtype)
8print(image.shape)
9# display the array of pixels as an image
10pyplot.imshow(image)
11pyplot.show()
9
function displays the class of an image.

Chọn một hình ảnh thử nghiệm để tải và làm việc với thư viện Gối (PIL). Hình ảnh có thể là PNG hoặc JPEG. Trong ví dụ này, chúng tôi sẽ sử dụng một hình ảnh có tên Kolala.jpeg. Hoặc tải lên hình ảnh trong thư mục làm việc hoặc đưa ra đường dẫn mong muốn của bạn. Lớp

1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
3 là trái tim của
1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
4 và các thuộc tính của nó giúp thao tác với các pixel, định dạng và độ tương phản của hình ảnh.
1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
0
function. This function comes in handy when the manipulation is performed on
1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
1
image data, that we laterwant to save as a PNG or JPEG file.

1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)

Python

Hướng dẫn how do you read an image as a numpy array in python? - làm thế nào để bạn đọc một hình ảnh dưới dạng một mảng numpy trong python?

Tuyệt quá! Phiên bản mới nhất hiện được tải xuống. Hãy chuyển sang bước tiếp theo. gives the value of each pixel of the NumPy array image.

Hướng dẫn how do you read an image as a numpy array in python? - làm thế nào để bạn đọc một hình ảnh dưới dạng một mảng numpy trong python?

Tải hình ảnh

Ở đây chúng tôi sẽ tìm hiểu hai cách để tải và lấy chi tiết của một hình ảnh: sử dụng thư viện gối và sử dụng thư viện matplotlib.

Phương pháp 1: Thư viện gối

1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
8 argument saves the file in different formats, such as PNG, GIF, or PEG.

Chọn một hình ảnh thử nghiệm để tải và làm việc với thư viện Gối (PIL). Hình ảnh có thể là PNG hoặc JPEG. Trong ví dụ này, chúng tôi sẽ sử dụng một hình ảnh có tên Kolala.jpeg. Hoặc tải lên hình ảnh trong thư mục làm việc hoặc đưa ra đường dẫn mong muốn của bạn. Lớp

1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
3 là trái tim của
1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
4 và các thuộc tính của nó giúp thao tác với các pixel, định dạng và độ tương phản của hình ảnh.

Lớp 1# load and show an image with Pillow 2from PIL import Image 3# Open the image form working directory 4image = Image.open('kolala.jpeg') 5# summarize some details about the image 6print(image.format) 7print(image.size) 8print(image.mode) 9# show the image 10load_image.show()3 sử dụng các chức năng này:*

1import numpy as np
2from PIL import Image
3
4im = np.array(Image.open('kolala.jpeg').convert('L')) #you can pass multiple arguments in single line
5print(type(im))
6
7gr_im= Image.fromarray(im).save('gr_kolala.png')

Python

Hướng dẫn how do you read an image as a numpy array in python? - làm thế nào để bạn đọc một hình ảnh dưới dạng một mảng numpy trong python?

Tuyệt quá! Phiên bản mới nhất hiện được tải xuống. Hãy chuyển sang bước tiếp theo.

1load_img_rz = np.array(Image.open('kolala.jpeg').resize((200,200)))
2Image.fromarray(load_img_rz).save('r_kolala.jpeg')
3print("After resizing:",load_img_rz.shape)

Python

1After resizing: (200, 200, 3)

Tuyệt quá! Phiên bản mới nhất hiện được tải xuống. Hãy chuyển sang bước tiếp theo.

1im = np.array(Image.open('kolala.jpeg'))
2
3print("Before trimming:",im.shape)
4
5im_trim = im[128:384, 128:384]
6print("After trimming:",im_trim.shape)
7
8Image.fromarray(im_trim).save('trim_kolala.jpeg')

Python

Hướng dẫn how do you read an image as a numpy array in python? - làm thế nào để bạn đọc một hình ảnh dưới dạng một mảng numpy trong python?

Tuyệt quá! Phiên bản mới nhất hiện được tải xuống. Hãy chuyển sang bước tiếp theo.

Tải hình ảnh

Ở đây chúng tôi sẽ tìm hiểu hai cách để tải và lấy chi tiết của một hình ảnh: sử dụng thư viện gối và sử dụng thư viện matplotlib.

1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
4 command (CPU version). Keras runs on the top of the TensorFlow framework. Make sure the package is correctly installed.

Các chức năng này có thể là các chức năng tiện lợi hữu ích khi bắt đầu một dự án thị giác máy tính học tập mới hoặc khi bạn cần kiểm tra các hình ảnh cụ thể.

Đang tải một hình ảnh với API Keras

Keras cung cấp chức năng

1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
5 để tải hình ảnh PIL. Tìm hiểu thêm về chức năng ở đây.
1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
5
function for loading a PIL image. Learn more about the function here .

1from keras.preprocessing.image import load_img
2import warnings
3
4# load the image
5img = load_img('kolala.jpeg')
6# report details about the image
7print(type(img))
8print(img.format)
9print(img.mode)
10print(img.size)
11# show the image
12img.show()

Python

Theo đầu ra bên dưới, chúng tôi có thể xác nhận rằng hình ảnh được tải ở định dạng PIL và có các kênh màu RGB định dạng JPEG và kích thước 800 x 450 pixel. Hình ảnh sẽ được hiển thị trong trình xem hình ảnh mặc định của bạn.

Hướng dẫn how do you read an image as a numpy array in python? - làm thế nào để bạn đọc một hình ảnh dưới dạng một mảng numpy trong python?

Chuyển đổi hình ảnh bằng API Keras

Keras sử dụng chức năng

1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
6 để chuyển đổi hình ảnh PIL thành Numpy. API cũng cung cấp chức năng
1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
7, có thể được sử dụng để chuyển đổi một mảng dữ liệu pixel thành hình ảnh PIL.
1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
6
function to convert the PIL image into numpy. The API also provides the
1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
7
function, which can be used for converting an array of pixel data into a PIL image.

1# example of converting an image with the Keras API
2from keras.preprocessing.image import load_img
3from keras.preprocessing.image import img_to_array
4from keras.preprocessing.image import array_to_img
5
6# load the image
7img = load_img('kolala.jpeg')
8print("Orignal:" ,type(img))
9
10# convert to numpy array
11img_array = img_to_array(img)
12print("NumPy array info:") 
13print(type(img_array))    
14
15print("type:",img_array.dtype)
16print("shape:",img_array.shape)
17# convert back to image
18
19img_pil = array_to_img(img_array)
20print("converting NumPy array:",type(img_pil))

Python

Hướng dẫn how do you read an image as a numpy array in python? - làm thế nào để bạn đọc một hình ảnh dưới dạng một mảng numpy trong python?

Theo đầu ra bên dưới, chúng tôi có thể xác nhận rằng hình ảnh được tải ở định dạng PIL và có các kênh màu RGB định dạng JPEG và kích thước 800 x 450 pixel. Hình ảnh sẽ được hiển thị trong trình xem hình ảnh mặc định của bạn.

Chuyển đổi hình ảnh bằng API Keras

1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
8 function to save an image locally. Here the function takes the path and the file name where we want to save the image data in NumPy array format. This function is useful when you have manipulated the image and wish to save the image for later use.

Keras sử dụng chức năng

1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
6 để chuyển đổi hình ảnh PIL thành Numpy. API cũng cung cấp chức năng
1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
7, có thể được sử dụng để chuyển đổi một mảng dữ liệu pixel thành hình ảnh PIL.

Python

Hướng dẫn how do you read an image as a numpy array in python? - làm thế nào để bạn đọc một hình ảnh dưới dạng một mảng numpy trong python?

Theo đầu ra bên dưới, chúng tôi có thể xác nhận rằng hình ảnh được tải ở định dạng PIL và có các kênh màu RGB định dạng JPEG và kích thước 800 x 450 pixel. Hình ảnh sẽ được hiển thị trong trình xem hình ảnh mặc định của bạn.

Chuyển đổi hình ảnh bằng API Keras

Keras sử dụng chức năng

1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
6 để chuyển đổi hình ảnh PIL thành Numpy. API cũng cung cấp chức năng
1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
7, có thể được sử dụng để chuyển đổi một mảng dữ liệu pixel thành hình ảnh PIL.
1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)
9
. The
1import numpy as np
2from PIL import Image
3
4im = np.array(Image.open('kolala.jpeg').convert('L')) #you can pass multiple arguments in single line
5print(type(im))
6
7gr_im= Image.fromarray(im).save('gr_kolala.png')
0
package provides an
1# load and display an image with Matplotlib
2from matplotlib import image
3from matplotlib import pyplot
4# load image as pixel array
5image = image.imread('kolala.jpeg')
6# summarize shape of the pixel array
7print(image.dtype)
8print(image.shape)
9# display the array of pixels as an image
10pyplot.imshow(image)
11pyplot.show()
2
function to load the image. It also reads a PIL image in the NumPy array format. The only thing we need to convert is the image color from BGR to RGB.
1import numpy as np
2from PIL import Image
3
4im = np.array(Image.open('kolala.jpeg').convert('L')) #you can pass multiple arguments in single line
5print(type(im))
6
7gr_im= Image.fromarray(im).save('gr_kolala.png')
2
saves the image in the file.

1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
1

Python

Theo đầu ra bên dưới, chúng tôi có thể xác nhận rằng hình ảnh được tải ở định dạng PIL và có các kênh màu RGB định dạng JPEG và kích thước 800 x 450 pixel. Hình ảnh sẽ được hiển thị trong trình xem hình ảnh mặc định của bạn.

Chuyển đổi hình ảnh bằng API Keras

Làm cách nào để đọc một hình ảnh vào một mảng numpy?

Làm thế nào để chuyển đổi một hình ảnh thành một mảng numpy trong Python..
Hình ảnh = Pil. Hình ảnh. Mở ("red_image.png").
Image_Array = NP. Mảng (hình ảnh).
in (Image_Array [0] [0]) In pixel đầu tiên trong hình ảnh ..
In (Image_Array. Hình) Kích thước in của hình ảnh ..

Làm thế nào để bạn lưu trữ một hình ảnh trong một mảng trong Python?

Sử dụng hàm Image.FromArray () để lưu mảng numpy dưới dạng hình ảnh ..
Sử dụng hàm ImageIO.IMWRITE () để lưu mảng numpy dưới dạng hình ảnh ..
Sử dụng hàm matplotlib.pyplot.imsave () để lưu một mảng numpy dưới dạng hình ảnh ..
Sử dụng hàm cv2.imwrite () để lưu mảng numpy làm hình ảnh ..

Chức năng nào được sử dụng để đọc hình ảnh từ một tệp vào một mảng?

Các hàm được sử dụng trong đoạn mã này là imread (), tải hình ảnh dưới dạng một mảng của pixel và imshow (), hiển thị hình ảnh đó.imread() , which loads the image in the form of an array of the pixel and imshow() , which displays that image.

Hình ảnh được lưu trữ trong Numpy như thế nào?

Hình ảnh có thể được đọc thành Numpy, và được lưu trữ dưới dạng mảng đa chiều.Các giá trị cường độ pixel thuộc loại UINT8, có nghĩa là chúng nằm trong khoảng từ 0 đến 255.Như bạn có thể thấy, trong kênh màu đỏ, lá phong gần như vô hình (tại sao?)multi-dimensional arrays. The pixel intensity values are of type uint8 , meaning they range from 0 to 255 . As you can see, in the red channel, the maple leaf is almost invisible (why?)