Switch to rawpy/PIL and simplify

This commit is contained in:
Ian Gulliver
2024-08-11 14:28:00 -07:00
parent 81a9fd5fb8
commit 35d78bf37e
2 changed files with 57 additions and 59 deletions

View File

@@ -6,18 +6,11 @@ from PIL import Image, ImageOps
path = 'data/RAW/SC1/BR1' path = 'data/RAW/SC1/BR1'
filename = os.path.join(path, '00570/back.orf') filename = os.path.join(path, '00570/back.orf')
raw = rawpy.imread(filename).postprocess()
rgba = Image.fromarray(raw).convert('RGBA')
raw = rawpy.imread(filename) h, _, _ = rgba.convert('HSV').split()
rgb_image = raw.postprocess() mask = ImageOps.invert(Image.eval(h, lambda x: 255 if 50 < x < 90 else 0))
image = Image.fromarray(rgb_image).convert('RGBA') rgba.putalpha(mask)
rgba.save('output.png', 'PNG')
hsv_image = image.convert('HSV')
h, s, v = hsv_image.split()
green_mask = Image.eval(h, lambda x: 255 if 50 < x < 90 else 0)
green_mask = ImageOps.invert(green_mask)
rgba_image = image.copy()
rgba_image.putalpha(green_mask)
rgba_image.save('output.png', 'PNG')

View File

@@ -1,15 +1,18 @@
import os
import base64 import base64
import io
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, 'pending') pending = os.path.join(path, '00570')
bitting = None bitting = None
@@ -20,11 +23,13 @@ for file in sorted(os.listdir(pending)):
print(file) print(file)
filename = os.path.join(pending, file) filename = os.path.join(pending, file)
with Image(filename=filename) as img: raw = rawpy.imread(filename).postprocess()
img.format = 'jpg' rgb = Image.fromarray(raw).convert('RGB')
img.resize(512, 384) smaller = rgb.resize((512, 384))
blob = img.make_blob()
blob_b64 = base64.b64encode(blob).decode('utf-8') buf = io.BytesIO()
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=[