Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the layout of the various dimensions of the data when opencv, numpy and pillow process the picture?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

What is the layout of the various dimensions of the data when opencv, numpy and pillow deal with pictures? in view of this question, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

When opencv reads pictures, the format of numpy array after reading is (height, width, channels). The format of data after reading pictures by pillow third-party library is (width, height). Use the numpy library to read pillow pictures in the format of (width, height), the data format is automatically converted to (height, width, channels) pillow from the dimension to (height, width, channels) numpy data, automatically adjust the dimension to (width, height).

The code experiment shows:

From PIL import Imageimport cv2import numpy as npif _ _ name__ ='_ _ main__':print () print ('test the format in which opencv-python reads pictures' .center (50 opencv-python read pictures') img_cv2 = cv2.imread ('bryant.jpg', 1) # read the picture file img_cv2 = np.float32 (img_cv2) / 255 # to float32 print (' opencv-python 's picture reading format: (height, width, channels)' Img_cv2.shape) # opencv-python image reading format: (height, width, channels) (667,667, 3) print () print ('Test the format of the picture read by the Python third-party library Pillow' .center ("bryant.jpg") print (img_PIL.format, img_PIL.size, img_PIL.mode) # JPEG (1000, 667) RGBprint ('width of the picture:', img_PIL.width 'height of the picture:', img_PIL.height) # width of the picture: 1000 height of the picture: 667print () print ('test the way Python third-party library NumPy handles PIL images' .center (50th imgNumPy_from_PIL.shape)) imgNumPy_from_PIL = np.array (img_PIL) print ('imgNumPy shape is:', imgNumPy_from_PIL.shape) # imgNumPy shape is: (667,667,3) print ((height, width) how NumPy handles PIL images Channels)', imgNumPy_from_PIL.shape) # NumPy handles PIL images: (height, width, channels) (667, 1000, 3) print () print ('Test the format of NumPy data read by Python third-party library Pillow' .center (50Mae NumPy) mg_PIL_from_Numpy = Image.fromarray (imgNumPy_from_PIL) print (mg_PIL_from_Numpy.format, mg_PIL_from_Numpy.size, mg_PIL_from_Numpy.mode) # None (1000) RGBprint ('width of picture:', mg_PIL_from_Numpy.width, 'height of picture:', mg_PIL_from_Numpy.height) # width of picture: 1000 height of picture: 667mg_PIL_from_Numpy.show () img_PIL.show ()

Console output result:

Copyright (C) Windows PowerShell Microsoft Corporation. All rights reserved. It took 891 milliseconds to try a new cross-platform PowerShell https://aka.ms/pscore6 to load personal and system profiles. (base) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq > &'D:\ Anaconda3\ envs\ ssd4pytorch2_2_0\ python.exe''c:\ Users\ chenxuqi\ .vscode\ extensions\ ms-python.python-2021.1.502429796\ pythonFiles\ lib\ python\ debugpy\ launcher' '61922' -'c:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq\ test.py'- Test opencv-python Format for reading pictures-opencv-python format for reading pictures: (height Width, channels) (1000, 1000, 3)-Test the format of pictures read by Python third-party library Pillow-JPEG (667) RGB picture width: 1000 picture height: 667-test the way Python third-party library NumPy handles PIL images-the shape of imgNumPy is: (667,1000) 3) the way NumPy handles PIL images: (height, width, channels) (667,667,3)-Test the format of NumPy data read by Python third-party library Pillow-None (1000) RGB picture width: 1000 picture height: 667 (base) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq > conda activate ssd4pytorch2_2_0 (ssd4pytorch2_2_0) PS C:\ Users\ chenxuqi\ Desktop\ News4cxq\ test4cxq >

Original image used: bryant.jpg:

Show the running results:

This is the answer to the question about the layout of the various dimensions of data when opencv, numpy and pillow deal with pictures. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report