python - 在 Python 中手动将 RGB 转换为 HSI
问题描述
我有一个作业到期,需要一些帮助。
因此,我需要将 RGB 从图像转换为 HSI 并打印出三幅图像(1. 色调、2. 饱和度和 3. 强度)。
我不能使用 openCV 之外的任何库来加载和打印图像;其他任何事情都必须手动完成。
我(我认为)计算正确,但我似乎无法正确打印。我告诉我我不能,因为输入是一个元组而输出不是。所以,我的输出(put.pixel)现在是空的。
希望任何人都可以提供帮助!
import math
from PIL import Image
RgbString = ' :RGB for pixel: '
HsiString = ' :HSI for pixel: '
img = Image.open('MemeFace.jpg')
HImage = Image.new("HSV", (img.width,
img.height))
rgb_img = img.convert('RGB')
r, g, b = rgb_img.getpixel((1, 1))
HImage.convert('HSV')
def rgb2hsv(r, g, b):
r = float(r)
g = float(g)
b = float(b)
high = max(r, g, b)
low = min(r, g, b)
h, s, v = high, high, high
d = high - low
s = 0 if high == 0 else d/high
if high == low:
h = 0.0
else:
h = {
r: (g - b) / d + (6 if g < b
else 0),
g: (b - r) / d + 2,
b: (r - g) / d + 4,
}[high]
h /= 6
return h,s,v
for x in range (1, img.width):
for y in range (1, img.height):
r, g, b = rgb_img.getpixel((x,y))
print(r, g, b, RgbString, x, y)
print(rgb2hsv(r, g, b),
HsiString, x, y)
HImage.putpixel((x, y), ())
Image._show(HImage)
Image.fromdef
解决方案
推荐阅读
- ios - 指定最小单元格高度会产生警告吗?
- xamarin.forms - IMarkupExtension - 在编译期间检查属性类型 (xamarin.forms)
- github - 使用 Saltstack 构建包
- cygwin - Cygwin 无法设置 db_home
- java - 在 ServletContext 资源 [/WEB-INF/spring-security.xml] 中定义的名称为“offlineTokenServices”的无效 bean 定义
- google-apps-script - 用户上传的 Google 表单文件不是问题
- jquery - DataTable 问题在重新加载时删除 thead 子项
- python - 如何使用python从json文件中打印
- javascript - 在标记页面中,如何向 html 标签的 textContent 属性添加内容?
- java - InfoWindowAdapter 使用或覆盖已弃用的 API