So I'm attempting to convert my image from RGB to HSV so I can manipulate the hue of the image. I managed to figure out how to change the hue using numpy and pillow, but every time I use image.convert(), on a transparent image, it eliminates the transparency and affects the image quality. How can I preserve the image's transparency when converting it to HSV and back again?
My code:
from PIL import Image
img = Image.open("flame.png")
new_img = img.convert("HSV")
new_img.show()
UPDATE:
So I attempted to preserve the alpha channel by creating and alpha mask and applying it on the converted image. Here's the modified code:
import numpy as np
from PIL import Image
import cv2
alpha = cv2.imread("traits/Background/background.png", cv2.IMREAD_UNCHANGED)
ret, mask = cv2.threshold(alpha[:, :, 3], 0, 255, cv2.THRESH_BINARY)
channel = Image.fromarray(mask)
img = Image.open("traits/Background/background.png")
new_img = img.convert("HSV")
new_img = new_img.convert("RGBA")
new_img.putalpha(channel)
new_img.show()
This is the result:
It has some transparency now but still looks terrible. Still doesn't preserve the original transparency.
How would you go about extracting and storing the alpha channel in a way that preserves the original quality?
Use
alpha = im.getchannel('A')
to grab the alpha channel. Then do your processing and afterwards restore the alpha channel:
im.putalpha(alpha)