python实验。利用PIL库处理一张的照片,至少变换五种处理效果并展示
时间: 2024-05-07 12:23:56 浏览: 20
好的,以下是利用PIL库处理图片的代码,使用了五种不同的处理效果:
```python
from PIL import Image, ImageFilter, ImageDraw
# 打开原始图片
img = Image.open('original.jpg')
# 1. 对图片进行裁剪
cropped = img.crop((0, 0, 100, 100))
cropped.show()
# 2. 对图片进行缩放
resized = img.resize((img.width // 2, img.height // 2))
resized.show()
# 3. 对图片进行模糊
blurred = img.filter(ImageFilter.BLUR)
blurred.show()
# 4. 对图片进行反转
flipped = img.transpose(Image.FLIP_LEFT_RIGHT)
flipped.show()
# 5. 对图片进行绘制文本
draw = ImageDraw.Draw(img)
draw.text((100, 100), "Hello, PIL", fill=(255, 0, 0))
img.show()
```
请注意,这些操作是针对单个图像的,如果需要对整个图像文件夹进行操作,可以使用Python的os库来遍历文件夹,依次对每个图像进行处理。
相关问题
用python程序的pIL库和numpy库使用自己的一张照片,变换光照角度,生成一张手绘画
好的,我知道了。以下是具体的步骤:
1. 导入必要的库:PIL和numpy
```python
from PIL import Image
import numpy as np
```
2. 打开图片并将其转换为numpy数组
```python
img = Image.open('your_image.jpg')
img_array = np.array(img)
```
3. 将RGB数组转换为灰度数组
```python
gray_array = np.dot(img_array[...,:3], [0.2989, 0.5870, 0.1140])
```
4. 定义一个模糊函数,用于模糊图像以模拟手绘效果
```python
def blur(img, radius):
kernel_size = radius * 2 + 1
kernel = np.ones((kernel_size, kernel_size)) / kernel_size ** 2
return np.uint8(np.convolve(img, kernel, mode='valid'))
```
5. 对灰度数组进行模糊处理
```python
blurred_array = blur(gray_array, radius=3)
```
6. 对模糊数组进行边缘检测,得到一个黑白图像
```python
from scipy.signal import convolve2d
sobel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
sobel_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
gradient_x = convolve2d(blurred_array, sobel_x, mode='same')
gradient_y = convolve2d(blurred_array, sobel_y, mode='same')
gradient_magnitude = np.sqrt(gradient_x ** 2 + gradient_y ** 2)
gradient_direction = np.arctan2(gradient_y, gradient_x)
threshold = np.max(gradient_magnitude) * 0.1
edges = np.zeros_like(gradient_magnitude)
edges[gradient_magnitude > threshold] = 255
```
7. 将黑白图像转换为RGB数组
```python
rgb_edges = np.zeros_like(img_array)
rgb_edges[:,:,0] = edges
rgb_edges[:,:,1] = edges
rgb_edges[:,:,2] = edges
```
8. 将原始图像和边缘检测后的图像进行融合,得到最终手绘效果图
```python
sketch_array = np.where(rgb_edges == 255, img_array, blurred_array[...,None])
```
9. 将手绘效果图转换为PIL图像并保存
```python
sketch_img = Image.fromarray(sketch_array)
sketch_img.save('your_sketch.jpg')
```
这样就可以生成一张手绘画了。注意,通过调整模糊半径和边缘检测的阈值,可以得到不同的效果。
用python和PIL库写图像对数变换的代码
以下是使用 Python 和 PIL 库实现图像对数变换的代码示例:
```python
from PIL import Image
import math
# 打开图像并转换为灰度图像
img = Image.open('input.jpg').convert('L')
# 获取图像的宽度和高度
width, height = img.size
# 对数变换的常数
c = 255 / math.log(1 + img.getextrema()[1])
# 对每个像素应用对数变换
for x in range(width):
for y in range(height):
# 获取像素的亮度值
pixel = img.getpixel((x, y))
# 计算对数变换后的值
new_pixel = int(c * math.log(1 + pixel))
# 更新像素值
img.putpixel((x, y), new_pixel)
# 保存变换后的图像
img.save('output.jpg')
```
以上代码将图像 `input.jpg` 转换为灰度图像,并对每个像素应用对数变换。对数变换的常数 `c` 使用了图像的最大亮度值。最后,将变换后的图像保存为 `output.jpg`。
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)