Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

python pillow

作者:噬血啸月   发布日期:2025-09-30   浏览:30

from PIL import Image

# 打开图像文件
img = Image.open("example.jpg")

# 显示图像
img.show()

# 调整图像大小
new_img = img.resize((500, 500))

# 保存调整大小后的图像
new_img.save("resized_example.jpg")

# 将图像转换为黑白
gray_img = img.convert('L')

# 保存黑白图像
gray_img.save("black_white_example.jpg")

# 翻转图像
flipped_img = img.transpose(Image.FLIP_LEFT_RIGHT)

# 保存翻转后的图像
flipped_img.save("flipped_example.jpg")

解释说明:

  1. 导入Pillow库from PIL import Image 导入了Pillow库中的Image模块,用于处理图像。
  2. 打开图像文件Image.open("example.jpg") 打开了一个名为 example.jpg 的图像文件。
  3. 显示图像img.show() 显示图像。
  4. 调整图像大小img.resize((500, 500)) 将图像调整为500x500像素的大小。
  5. 保存调整大小后的图像new_img.save("resized_example.jpg") 将调整大小后的图像保存为 resized_example.jpg
  6. 将图像转换为黑白img.convert('L') 将图像转换为灰度(黑白)模式。
  7. 保存黑白图像gray_img.save("black_white_example.jpg") 将黑白图像保存为 black_white_example.jpg
  8. 翻转图像img.transpose(Image.FLIP_LEFT_RIGHT) 水平翻转图像。
  9. 保存翻转后的图像flipped_img.save("flipped_example.jpg") 将翻转后的图像保存为 flipped_example.jpg

这些代码展示了如何使用Pillow库进行基本的图像处理操作。

上一篇:python import用法

下一篇:python time.sleep

大家都在看

python时间格式

python读取文件路径

staticmethod在python中有

python开发windows应用程序

python中len是什么意思

python ord和chr

python中的yield

python自定义异常

python判断路径是否存在

python list.pop

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站