python 将图片颜色反过来
时间: 2023-07-29 13:07:37 浏览: 45
要将图片颜色反过来,可以使用Pillow库中的ImageOps模块,具体操作如下:
```python
from PIL import Image, ImageOps
# 打开图片
image = Image.open('example.jpg')
# 反转颜色
inverted_image = ImageOps.invert(image)
# 保存图片
inverted_image.save('inverted_example.jpg')
```
其中,`example.jpg`是待处理的图片文件名,`inverted_example.jpg`是处理后保存的文件名。
相关问题
python将数组反过来
在Python中,可以使用切片(slice)来将数组反过来。切片是一种用于获取序列的子序列的方法,通过指定起始位置、结束位置和步长来实现。要将数组反过来,可以使用[::-1]作为切片的参数。下面是一个示例代码:
```python
arr = [1, 2, 3, 4, 5]
reversed_arr = arr[::-1]
print(reversed_arr)
```
输出结果为:[5, 4, 3, 2, 1]
Python将图片颜色颠倒
可以使用Pillow库来处理图片,以下是将图片颜色颠倒的Python代码:
```python
from PIL import Image
# 打开图片
image = Image.open('example.jpg')
# 将图片颜色颠倒
inverted_image = ImageOps.invert(image)
# 保存处理后的图片
inverted_image.save('inverted_example.jpg')
```
在这个例子中,我们首先使用`Image.open()`打开了一张名为`example.jpg`的图片。然后,我们使用`ImageOps.invert()`将图片颜色颠倒,并将处理后的图片保存为`inverted_example.jpg`。