This article mainly introduces the method of opening, displaying and saving images with python. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
When it comes to digital image processing, most people may think of matlab, but matlab also has its own shortcomings:
1. It is not open source and expensive.
2. The software has large capacity. Generally, it is 3G or above, and higher versions can even reach 5G or above.
3. It can only be used for research and is not easy to convert into software.
Therefore, we use the scripting language python here for digital image processing.
To use python, you must first install python, usually version 2.7 or above. Whether it is a windows system or a linux system, the installation is very simple.
To use python for various development, you must install the corresponding library. This is very similar to matlab, except that it is called a toolbox in matlab, and it is called a library or package in python. To install these libraries, you usually use pip to install them.
To use python for digital image processing, you must also install the Pillow package. Although python comes with a PIL (python images library), this library has stopped being updated, so Pillow is used, which is developed from PIL.
pip install Pillow
1. Opening and displaying pictures
from PIL import Image img=Image.open('d:/dog.png') img.show()
Although Pillow is used, it is forked from PIL, so it still needs to be imported from PIL. Use the open() function to open the image, and use the show() function to display the image.
This way of displaying pictures is to call the image browser that comes with the operating system to open the picture. Sometimes this method is not convenient, so we can also use another method to let the program draw the picture. .
from PIL import Image import matplotlib.pyplot as plt img=Image.open('d:/dog.png') plt.figure("dog") plt.imshow(img) plt.show()
Although this method is a bit more complicated, it is recommended to use this method. It uses a matplotlib library to draw pictures for display. matplotlib is a professional drawing library, equivalent to plot in matlab. You can set multiple figures, set the title of the figure, and even use subplot to display multiple pictures in one figure. matplotlib can be installed directly
pip install matplotlib
plt.axis('off')After opening the picture, you can use some attributes to view the picture information, such as
print img.size #图片的尺寸 print img.mode #图片的模式 print img.format #图片的格式display The result is:
(558, 450)RGBA
PNG
2. Save the picture
img.save('d:/dog.jpg')Just one line of code, very simple. This line of code can not only save the image, but also convert the format. In this example, the original png image is saved into a jpg image. Related recommendations:
Use python to process images to achieve pixel access in images
##
The above is the detailed content of How to open, display, and save images using python. For more information, please follow other related articles on the PHP Chinese website!

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
