[英]Is there a way of converting an image to HSL using Pillow?
有沒有一種方法可以在Python中使用Pillow將圖像轉換為HSL?
理想情況下,我希望能夠打開JPEG,將其轉換為HSL,更改HSL值,轉換回RGB並保存。
我唯一能colorsys.rgb_to_hls
方法是在每個像素上使用colorsys.rgb_to_hls
。
枕頭(5.4.1)不支持將RGB轉換為HSL:
Python 3.7.2 (default, Dec 27 2018, 07:35:52)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> im = Image.open("Tests/images/hopper.png")
>>> hsl = im.convert("HSL")
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/PIL/Image.py", line 1030, in convert
im = self.im.convert(mode, dither)
ValueError: conversion from RGB to HSL not supported
但是,如果可以使用它,則支持RGB to HSV:
from PIL import Image
print("Open a JPEG")
im = Image.open("Tests/images/hopper.png")
px = im.load()
print(im.mode) # "RGB"
print(px[0, 0]) # (20, 21, 67) ~= navy
print("Convert it to HSV")
hsv = im.convert("HSV")
px = hsv.load()
print(hsv.mode) # "HSV"
print(px[0, 0]) # (169, 178, 67) ~= navy
print("Change an HSV value")
px[0, 0] = (0, 255, 255) # red
print(px[0, 0]) # (0, 255, 255) == red
print("Convert back to RGB and save")
im2 = hsv.convert("RGB")
px = im2.load()
print(im2.mode) # "RGB"
print(px[0, 0]) # (255, 0, 0) == red
im2.save("out.png")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.