import colorsys from PIL import Image sharp_image = Image.open("red1.jpg") hsi_image = sharp_image.convert("HSV") pixels = hsi_image.load() for i in range(hsi_image.size[0]): for j in range(hsi_image.size[1]): h, s, v = pixels[i, j] s *= 1.5 v *= 1.5 pixels[i, j] = (h, s, v) rgb_image = hsi_image.convert("RGB") rgb_image.save("red2.jpg")报错TypeError: integer argument expected, got float
时间: 2024-01-26 07:04:08 浏览: 81
这个错误是因为在 HSV 颜色空间中,饱和度和亮度的值域是浮点数(0-1),但在转换为 RGB 颜色空间时,这些值应该是整数(0-255)。这意味着你需要将这些浮点数值乘以 255 并四舍五入取整。你可以尝试修改代码中的这一部分:
```python
s *= 1.5
v *= 1.5
```
改为:
```python
s = int(s * 255 * 1.5 + 0.5)
v = int(v * 255 * 1.5 + 0.5)
```
这将把浮点数值乘以 255 并四舍五入取整,然后再乘以 1.5。这样就能够正确地转换为 RGB 颜色空间了。
相关问题
from bm3d import bm3d_rgb from experiment_funcs1 import get_experiment_noise, get_psnr, get_cropped_psnr from PIL import Image import argparse import os import torch import numpy as np from torchvision.utils import save_image def main(): imagename = './test_image1/(1271).jpg' save_dir = 'test_result' save_path = 'noise' y = np.array(Image.open(imagename)) / 255 noise_type = 'g3' noise_var = 0.02 seed = 0 noise, psd, kernel = get_experiment_noise(noise_type, noise_var, seed, y.shape) z = np.atleast_3d(y) + np.atleast_3d(noise) y_est = bm3d_rgb(z, psd) psnr = get_psnr(y, y_est) print("PSNR:", psnr) y_est = np.minimum(np.maximum(y_est, 0), 1) z_rang = np.minimum(np.maximum(z, 0), 1) z_rang = torch.from_numpy(np.transpose(z_rang, (2, 0, 1))).float() y_est = torch.from_numpy(np.transpose(y_est, (2, 0, 1))).float() denoise_img_path = os.path.join(save_dir, 'denoised.jpg') save_image(y_est, denoise_img_path) noise_img_path = os.path.join(save_path, 'noise.jpg') save_image(z_rang, noise_img_path) if __name__ == '__main__': main()改为对灰度图处理
可以将代码中的`bm3d_rgb`函数改为`bm3d`函数,同时将读入图像的方式改为读入灰度图像即可。具体代码如下:
```
from bm3d import bm3d
from experiment_funcs1 import get_experiment_noise, get_psnr, get_cropped_psnr
from PIL import Image
import argparse
import os
import torch
import numpy as np
from torchvision.utils import save_image
def main():
imagename = './test_image1/(1271).jpg'
save_dir = 'test_result'
save_path = 'noise'
y = np.array(Image.open(imagename).convert('L')) / 255
noise_type = 'g3'
noise_var = 0.02
seed = 0
noise, psd, kernel = get_experiment_noise(noise_type, noise_var, seed, y.shape)
z = y + noise
y_est = bm3d(z, psd)
psnr = get_psnr(y, y_est)
print("PSNR:", psnr)
y_est = np.minimum(np.maximum(y_est, 0), 1)
z_rang = np.minimum(np.maximum(z, 0), 1)
z_rang = torch.from_numpy(z_rang).unsqueeze(0).float()
y_est = torch.from_numpy(y_est).unsqueeze(0).float()
denoise_img_path = os.path.join(save_dir, 'denoised.jpg')
save_image(y_est, denoise_img_path)
noise_img_path = os.path.join(save_path, 'noise.jpg')
save_image(z_rang, noise_img_path)
if __name__ == '__main__':
main()
```
from PIL import ImageFilter image = Image.open("red1.jpg") sharp_image = image.filter(ImageFilter.SHARPEN) sharp_image.save("red2.jpg")报错NameError: name 'Image' is not defined
这个错误是因为你没有正确导入所需的模块。你需要在代码的开头添加以下代码:
```python
from PIL import Image, ImageFilter
```
这将导入 `Image` 和 `ImageFilter` 两个类,以便你可以使用它们来处理图像。
阅读全文