使用 PIL 库 Image 类实现用两张图片合成一张新图片的功能;对生成新图片 尝试增强其亮度、对比度,调整色彩后保存
时间: 2024-04-14 09:27:27 浏览: 132
好的,下面是一个使用PIL库的Image类来合成两张图片并增强亮度、对比度以及调整色彩的示例程序:
```python
from PIL import Image, ImageEnhance
def merge_images(image1_path, image2_path, output_path):
# 打开两张图片
image1 = Image.open(image1_path)
image2 = Image.open(image2_path)
# 确保图片大小一致
width, height = max(image1.size[0], image2.size[0]), max(image1.size[1], image2.size[1])
image1 = image1.resize((width, height))
image2 = image2.resize((width, height))
# 合成图片
merged_image = Image.blend(image1, image2, alpha=0.5)
# 增强亮度
enhancer = ImageEnhance.Brightness(merged_image)
enhanced_image = enhancer.enhance(1.5)
# 增强对比度
enhancer = ImageEnhance.Contrast(enhanced_image)
enhanced_image = enhancer.enhance(1.5)
# 调整色彩
enhancer = ImageEnhance.Color(enhanced_image)
enhanced_image = enhancer.enhance(1.2)
# 保存合成后的图片
enhanced_image.save(output_path)
print("合成并增强图片完成!")
# 示例使用:
image1_path = 'image1.jpg'
image2_path = 'image2.jpg'
output_path = 'output.jpg'
merge_images(image1_path, image2_path, output_path)
```
你需要将 `image1.jpg` 和 `image2.jpg` 替换为你要合成的两张图片的路径,将 `output.jpg` 替换为要保存的合成图片的路径。程序将打开两张图片并合成为一张新的图片,然后对新图片进行亮度、对比度和色彩的增强,并保存为 `output.jpg` 文件。
请确保已安装PIL库(可以使用`pip install pillow`进行安装)并且图片文件存在。运行程序后,新图片将合成并保存在指定路径,并增强了亮度、对比度和色彩。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)