Hướng dẫn python show image - python hiển thị hình ảnh

Trong bài viết này, chúng tôi sẽ nghiên cứu các cách khác nhau về cách bạn có thể đọc và hiển thị hình ảnh trong Python. Chúng ta có thể đạt được điều này theo nhiều cách. Lý do là do hỗ trợ thư viện phong phú. Chúng tôi cũng sẽ khám phá cách chúng tôi có thể sử dụng chúng trong việc lai tạo với nhau.how you can read and display images in Python. We can achieve this in numerous ways. The reason is due to the abundant library support. We will also explore how we can use them in crossbreeding with each other.how you can read and display images in Python. We can achieve this in numerous ways. The reason is due to the abundant library support. We will also explore how we can use them in crossbreeding with each other.

Nội dung chính ShowShow

  • 1. OpenCV để hiển thị hình ảnh trong Python
  • 2. Matplotlib
  • Sau đó gọi phương thức sys.exit () để thoát khỏi kỹ thuật một cách an toàn.
  • Đặt tiêu đề của hình ảnh AS & nbsp; hình ảnh cừu & nbsp; sử dụng & nbsp; plt.title () & nbsp; phương thức.
  • Sau đó sử dụng chức năng & nbsp; plt.imshow () & nbsp; lấy biến hình ảnh & nbsp; img. Nhưng nó sẽ hiển thị nó trong & nbsp; phần phụ trợ.
  • Sự kết luận
  • Nơi bạn có thể hiển thị một hình ảnh trong Python?
  • Bạn có thể hiển thị hình ảnh trong Terminal Python không?

Sau đây là danh sách các thư viện của Python cho phép chúng tôi xử lý hình ảnh và thực hiện các tác vụ tương ứng.

  1. OpenCV
  2. Matplotlib
  3. Cái gối
  4. Scikit-Image
  5. Tensorflow

Bây giờ, hãy xem cách hiển thị một hình ảnh trong cửa sổ GUI Python một cách dễ dàng. Có thể có nhiều mô -đun và/hoặc hack khác để xem hình ảnh, vì vậy, don giới hạn bản thân chỉ là 5 mô -đun này!

1. OpenCV để hiển thị hình ảnh trong Python

2. MatplotlibComputer Vision journey to next level. There are two main functions OpenCV provides to read and display images.

  1. cv2.imread()
  2. cv2.imshow()

Code:

import sys # to access the system
import cv2
img = cv2.imread("sheep.png", cv2.IMREAD_ANYCOLOR)

while True:
    cv2.imshow("Sheep", img)
    cv2.waitKey(0)
    sys.exit() # to exit from all the processes

cv2.destroyAllWindows() # destroy all windows

Sau đó gọi phương thức sys.exit () để thoát khỏi kỹ thuật một cách an toàn.

Hướng dẫn python show image - python hiển thị hình ảnh

Đặt tiêu đề của hình ảnh AS & nbsp; hình ảnh cừu & nbsp; sử dụng & nbsp; plt.title () & nbsp; phương thức.

Explanation:

  1. Sau đó sử dụng chức năng & nbsp; plt.imshow () & nbsp; lấy biến hình ảnh & nbsp; img. Nhưng nó sẽ hiển thị nó trong & nbsp; phần phụ trợ.OpenCV package to access the functions. Also, import the sys module for additional packages.
  2. Sự kết luậnimg that holds our image. Call the cv2.imread() function and deliver the image path/image name as a first parameter. Then set the cv2.IMREAD_ANYCOLOR is the next parameter to read every color of the image.
  3. Nơi bạn có thể hiển thị một hình ảnh trong Python?while loop and that will help us render the image an infinite number of times till we exit the system.
  4. Bạn có thể hiển thị hình ảnh trong Terminal Python không?cv2.imshow() function inside the while loop. It takes two parameters, the image title and the image path variable img.
  5. Sau đây là danh sách các thư viện của Python cho phép chúng tôi xử lý hình ảnh và thực hiện các tác vụ tương ứng.cv2.waitkey() method waits till we exit or click on the close button.
  6. Sau đó gọi phương thức sys.exit () để thoát khỏi kỹ thuật một cách an toàn.
  7. Đặt tiêu đề của hình ảnh AS & nbsp; hình ảnh cừu & nbsp; sử dụng & nbsp; plt.title () & nbsp; phương thức.cv2.destroyAllWindows().

2. Matplotlib

Sau đó gọi phương thức sys.exit () để thoát khỏi kỹ thuật một cách an toàn.graphical format where each pixel lies on 2D x-y axes.

Đặt tiêu đề của hình ảnh AS & nbsp; hình ảnh cừu & nbsp; sử dụng & nbsp; plt.title () & nbsp; phương thức.

  1. matplotlib.image.imread()
  2. matplotlib.pyplot.imshow()

Code:

Sau đó sử dụng chức năng & nbsp; plt.imshow () & nbsp; lấy biến hình ảnh & nbsp; img. Nhưng nó sẽ hiển thị nó trong & nbsp; phần phụ trợ.

Output:

Sự kết luận

Explanation:

  1. Nơi bạn có thể hiển thị một hình ảnh trong Python?Matplotlib packages’ pylot and image modules.
  2. Bạn có thể hiển thị hình ảnh trong Terminal Python không?Sheep Image using plt.title() method.
  3. Sau đây là danh sách các thư viện của Python cho phép chúng tôi xử lý hình ảnh và thực hiện các tác vụ tương ứng.x-y plane. We need labels xlabel() and ylabel() functions to mention the axes and the pixels.
  4. Tạo một biến AS & nbsp; một hình ảnh & nbsp; giữ hình ảnh của chúng tôi. Gọi & nbsp; mpimg.imread () & nbsp; function và đưa ra & nbsp; đường dẫn/hình ảnh & nbsp; name & nbsp; làm tham số đầu tiên.an image that holds our image. Call the mpimg.imread() function and give the image path/image name as a first parameter.an image that holds our image. Call the mpimg.imread() function and give the image path/image name as a first parameter.
  5. Sau đó đặt a & nbsp; trong khi loop & nbsp; và điều đó sẽ giúp chúng tôi hiển thị hình ảnh số lần vô hạn cho đến khi chúng tôi thoát khỏi hệ thống.while loop and that will help us render the image an infinite number of times till we exit the system.while loop and that will help us render the image an infinite number of times till we exit the system.
  6. Sau đó sử dụng chức năng & nbsp; cv2.imshow () & nbsp; bên trong vòng lặp trong khi. Nó lấy hai tham số, tiêu đề hình ảnh và biến đường dẫn hình ảnh & nbsp; img.plt.imshow() function that takes image variable img. But it will show it in the backend.plt.imshow() function that takes image variable img. But it will show it in the backend.
  7. & Nbsp; cv2.waitkey () & nbsp; phương thức chờ cho đến khi chúng tôi thoát hoặc nhấp vào nút Đóng.plt.show() method and we have our image with properly scaled parameters on the screen.plt.show() method and we have our image with properly scaled parameters on the screen.

Sau đó gọi phương thức sys.exit () để thoát khỏi kỹ thuật một cách an toàn.

Cuối cùng, chúng tôi phá hủy tất cả các cửa sổ được tạo bằng cách sử dụng & nbsp; cv2.destroyallwindows ().open() and show() from PILLOW’s Image module. This action is just within three lines of code.open() and show() from PILLOW’s Image module. This action is just within three lines of code.

Code:

from PIL import Image
img = Image.open("sheep.png")
img.show()

Output:

2. Matplotlib

Explanation:

  1. Gói này chủ yếu để trực quan hóa dữ liệu. Nhưng, thông qua các kỹ thuật âm mưu, chúng ta có thể xem hình ảnh ở định dạng đồ họa trong đó mỗi pixel nằm trên trục 2D X-Y.Image from PIL.Image from PIL.
  2. Thư viện Thie cũng có các chức năng tương đương như của CV mở. Chỉ là tên gói thay đổi.img and then call the function open() in it. Give the path that has the image file.img and then call the function open() in it. Give the path that has the image file.
  3. Hiển thị hình ảnh thông qua matplotlibshow() function in joint with img variable through the dot operator “.”.show() function in joint with img variable through the dot operator “.”.
  4. Nhập & nbsp; matplotlib & nbsp; gói, & nbsp; pylot & nbsp; và & nbsp; hình ảnh & nbsp; mô -đun.

Đặt tiêu đề của hình ảnh AS & nbsp; hình ảnh cừu & nbsp; sử dụng & nbsp; plt.title () & nbsp; phương thức.

Khi matplotlib đọc hình ảnh trong mặt phẳng & nbsp; x-y. Chúng tôi cần nhãn & nbsp; xlabel () & nbsp; và & nbsp; ylabel () & nbsp; các chức năng để đề cập đến các trục và pixel.Scikit-Learn. It is built upon Python and supportive library Matplotlib thus it derives some of its functionalities. Methods are similar to that of the previous packages we saw before.Scikit-Learn. It is built upon Python and supportive library Matplotlib thus it derives some of its functionalities. Methods are similar to that of the previous packages we saw before.

Code:

from skimage import io

img = io.imread("sheep.png")
io.imshow(img)

Output:

Tạo một biến AS & nbsp; một hình ảnh & nbsp; giữ hình ảnh của chúng tôi. Gọi & nbsp; mpimg.imread () & nbsp; function và đưa ra & nbsp; đường dẫn/hình ảnh & nbsp; name & nbsp; làm tham số đầu tiên.

Sau đó sử dụng chức năng & nbsp; plt.imshow () & nbsp; lấy biến hình ảnh & nbsp; img. Nhưng nó sẽ hiển thị nó trong & nbsp; phần phụ trợ.

Để xem nó trên màn hình, hãy sử dụng phương thức & nbsp; plt.show () & nbsp; và chúng tôi có hình ảnh của chúng tôi với các tham số được chia tỷ lệ đúng trên màn hình.Machine Learning library especially from Google.Inc. It works on different aspects of Machine Learning, Deep Learning, and related concepts. It also has built-in datasets to start a hassle-free journey of Data Science and ML engineering. It works specifically on the computer’s GPU CUDA cores. This makes the model training more efficient and gives less stress to the CPU.Machine Learning library especially from Google.Inc. It works on different aspects of Machine Learning, Deep Learning, and related concepts. It also has built-in datasets to start a hassle-free journey of Data Science and ML engineering. It works specifically on the computer’s GPU CUDA cores. This makes the model training more efficient and gives less stress to the CPU.

Chúng tôi sẽ sử dụng thư viện này trong khớp với mô -đun matplotlib. Bởi vì điều này làm cho âm mưu hình ảnh và hiển thị dễ dàng hơn nhiều.

Code:

from warnings import filterwarnings
import tensorflow as tf
from tensorflow import io
from tensorflow import image
from matplotlib import pyplot as plt

filterwarnings("ignore") 
tf_img = io.read_file("sheep.png")
tf_img = image.decode_png(tf_img, channels=3)
print(tf_img.dtype)
plt.imshow(tf_img)
# plt.show()

Explanation:

  1. Nhập khẩu tenorflow. Sau đó từ TensorFlow & nbsp; cũng nhập IO và Image. & Nbsp; also import io and image.  also import io and image. 
  2. Nhập khẩu & nbsp; Matplotlib từ & nbsp; mô -đun pyplot cho mục đích vẽ đồ thị.matplotlib’s pyplot module for plotting purposes.matplotlib’s pyplot module for plotting purposes.
  3. (Tùy chọn) & NBSP; Ngoài ra, hãy sử dụng gói cảnh báo để tránh các cảnh báo không cần thiết. also, use the warnings package to avoid unnecessary warnings. also, use the warnings package to avoid unnecessary warnings.
  4. Tạo một biến hình ảnh tenorflow, tf_img và gọi phương thức io.Read_file (). Cho đường dẫn hình ảnh bên trong nó.
  5. Nó được đọc dưới dạng tệp mặc định & nbsp; Để xem nó là hình ảnh chúng ta cần sử dụng & nbsp; decode_png () & nbsp; hàm từ & nbsp; hình ảnh & nbsp; để được hệ thống công nhận. Hãy chắc chắn rằng bạn sử dụng chức năng quyết định chính xác. Chúng khác nhau cho từng loại hình ảnh. & NBSP; Sử dụng kênh = 3. Để sử dụng GPU mặc định.file. To view it as the image we need to use the decode_png() function from the image to get recognized by the system. Make sure you use the correct decider function. They are different for each image type. Use channels = 3. for default GPU usage.file. To view it as the image we need to use the decode_png() function from the image to get recognized by the system. Make sure you use the correct decider function. They are different for each image type. Use channels = 3. for default GPU usage.
  6. Cuối cùng, hiển thị hình ảnh đã chụp thông qua phương thức & nbsp; plt.imshow () & nbsp;plt.imshow() method.plt.imshow() method.

Output:

Hiển thị hình ảnh thông qua Tensorflow và Matplotlib

Sự kết luận

Vì vậy, đây là những cách khác nhau đáng kể thông qua đó chúng ta có thể thực hiện xử lý hình ảnh. Python có rất nhiều tùy chọn cho mỗi nhiệm vụ duy nhất. Nhận xét xuống phương thức và thư viện nào bạn thích nhiều nhất chúng tôi đã thực hiện trong bài viết này.

Python có nhiều thư viện khác nhau có thể được sử 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.Pillow, Matplotlib, and OpenCV.Pillow, Matplotlib, and OpenCV.

Nó giúp chuyển đổi hình ảnh thành mã thoát ANSI của nó để có thể chuyển đổi có thể in trên các giao diện dòng lệnh.Nó cho phép các mã màu 8/16/256 bit cho hình ảnh sống động.