Back to wand and solid keying
This commit is contained in:
57
chromakey.py
57
chromakey.py
@@ -1,16 +1,51 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
import rawpy
|
from sklearn.cluster import KMeans
|
||||||
from PIL import Image, ImageOps
|
from wand.image import Image
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
def chroma_key(img, object_colors=1, key_coords=(0,0)):
|
||||||
|
hsv = _hsv_points(img)
|
||||||
|
hsv_cart = _hsv_to_cartesian(hsv)
|
||||||
|
labels = _cluster(hsv_cart, object_colors+1, img)
|
||||||
|
mask = _mask(labels, key_coords)
|
||||||
|
with Image.from_array(mask) as mask_img:
|
||||||
|
img.composite_channel('all_channels', mask_img, 'multiply')
|
||||||
|
|
||||||
|
|
||||||
|
def _hsv_points(img):
|
||||||
|
hsv_img = img.clone()
|
||||||
|
hsv_img.transform_colorspace('hsv')
|
||||||
|
pixels = list(hsv_img.export_pixels(channel_map='RGB')) # this actually means HSV
|
||||||
|
return np.array(pixels).reshape(-1, 3)
|
||||||
|
|
||||||
|
|
||||||
|
def _hsv_to_cartesian(hsv):
|
||||||
|
h = hsv[:, 0]
|
||||||
|
s = hsv[:, 1]
|
||||||
|
v = hsv[:, 2]
|
||||||
|
|
||||||
|
x = s * np.cos(h * 2 * np.pi / 255)
|
||||||
|
y = s * np.sin(h * 2 * np.pi / 255)
|
||||||
|
|
||||||
|
return np.column_stack((x, y, v))
|
||||||
|
|
||||||
|
|
||||||
|
def _cluster(points, num_clusters, img):
|
||||||
|
kmeans = KMeans(n_clusters=num_clusters)
|
||||||
|
kmeans.fit(points)
|
||||||
|
return kmeans.labels_.reshape(img.height, img.width)
|
||||||
|
|
||||||
|
|
||||||
|
def _mask(labels, key_coords):
|
||||||
|
key_label = labels[key_coords]
|
||||||
|
return (labels != key_label).astype(np.uint8) * 255
|
||||||
|
|
||||||
|
|
||||||
path = 'data/RAW/SC1/BR1'
|
path = 'data/RAW/SC1/BR1'
|
||||||
filename = os.path.join(path, '00570/back.orf')
|
filename = os.path.join(path, '41244/back.orf')
|
||||||
|
|
||||||
raw = rawpy.imread(filename).postprocess()
|
with Image(filename=filename) as img:
|
||||||
rgba = Image.fromarray(raw).convert('RGBA')
|
chroma_key(img)
|
||||||
|
img.save(filename='masked_image.jpg')
|
||||||
h, _, _ = rgba.convert('HSV').split()
|
|
||||||
mask = ImageOps.invert(Image.eval(h, lambda x: 255 if 50 < x < 90 else 0))
|
|
||||||
|
|
||||||
rgba.putalpha(mask)
|
|
||||||
rgba.save('output.png', 'PNG')
|
|
||||||
@@ -1,18 +1,15 @@
|
|||||||
import base64
|
import base64
|
||||||
import io
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
from wand.image import Image
|
||||||
import rawpy
|
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
client = OpenAI()
|
client = OpenAI()
|
||||||
|
|
||||||
# List all files and directories in the specified path
|
# List all files and directories in the specified path
|
||||||
path = 'data/RAW/SC1/BR1'
|
path = 'data/RAW/SC1/BR1'
|
||||||
pending = os.path.join(path, '00570')
|
pending = os.path.join(path, 'pending')
|
||||||
|
|
||||||
bitting = None
|
bitting = None
|
||||||
|
|
||||||
@@ -23,13 +20,11 @@ for file in sorted(os.listdir(pending)):
|
|||||||
print(file)
|
print(file)
|
||||||
filename = os.path.join(pending, file)
|
filename = os.path.join(pending, file)
|
||||||
|
|
||||||
raw = rawpy.imread(filename).postprocess()
|
with Image(filename=filename) as img:
|
||||||
rgb = Image.fromarray(raw).convert('RGB')
|
img.format = 'jpg'
|
||||||
smaller = rgb.resize((512, 384))
|
img.resize(512, 384)
|
||||||
|
blob = img.make_blob()
|
||||||
buf = io.BytesIO()
|
blob_b64 = base64.b64encode(blob).decode('utf-8')
|
||||||
smaller.save(buf, format='JPEG')
|
|
||||||
blob_b64 = base64.b64encode(buf.getvalue()).decode('utf-8')
|
|
||||||
|
|
||||||
chat_completion = client.chat.completions.create(
|
chat_completion = client.chat.completions.create(
|
||||||
messages=[
|
messages=[
|
||||||
|
|||||||
Reference in New Issue
Block a user