- Python Multimedia
- Ninad Sathaye
- 189字
- 2021-08-03 15:27:39
Reading and writing images
To manipulate an existing image, we must open it first for editing and we also require the ability to save the image in a suitable file format after making changes. The Image
module in PIL provides methods to read and write images in the specified image file format. It supports a wide range of file formats.
To open an image, use Image.open
method. Start the Python interpreter and write the following code. You should specify an appropriate path on your system as an argument to the Image.open
method.
>>>import Image >>>inputImage = Image.open("C:\\PythonTest\\image1.jpg")
This will open an image file by the name image1.jpg
. If the file can't be opened, an IOError
will be raised, otherwise, it returns an instance of class Image
.
For saving image, use the save
method of the Image
class. Make sure you replace the following string with an appropriate /path/to/your/image/file
.
>>>inputImage.save("C:\\PythonTest\\outputImage.jpg")
You can view the image just saved, using the show
method of Image
class.
>>>outputImage = Image.open("C:\\PythonTest\\outputImage.jpg") >>>outputImage.show()
Here, it is essentially the same image as the input image, because we did not make any changes to the output image.
- Oracle VM Manager 2.1.2
- 微信小程序開發入門與實踐
- 鍵盤錄入技術(第2版)
- Flash CC動畫制作案例教程
- AutoCAD 2022中文版建筑設計從入門到精通
- 用Multisim玩轉電路仿真
- 無師自通AutoCAD 2014中文版機械設計
- 中文版Photoshop 2023從入門到精通
- Deep Inside osCommerce: The Cookbook
- Oracle BI Publisher 11g: A Practical Guide to Enterprise Reporting
- 攝影師的后期課:人像調色篇
- 中老年學微信(圖解大字版)
- 中文版Photoshop CS6技術大全
- 新編中文版Photoshop平面設計入門與提高(第2版)
- PPT效率手冊