mirror of
https://github.com/tcsenpai/pensieve.git
synced 2025-06-07 03:35:24 +00:00
feat(screen-recorder): save using webp format
This commit is contained in:
parent
b53d816b17
commit
e115e00702
@ -82,12 +82,12 @@ def take_screenshot(
|
|||||||
["screencapture", "-C", "-x", "-D", str(display + 1), temp_filename]
|
["screencapture", "-C", "-x", "-D", str(display + 1), temp_filename]
|
||||||
)
|
)
|
||||||
|
|
||||||
# 压缩图像为 JPEG 并添加元数据
|
# 压缩图像为 WebP 并添加元数据
|
||||||
with Image.open(temp_filename) as img:
|
with Image.open(temp_filename) as img:
|
||||||
img = img.convert("RGB")
|
img = img.convert("RGB")
|
||||||
jpeg_filename = os.path.join(
|
webp_filename = os.path.join(
|
||||||
os.path.join(base_dir, date),
|
os.path.join(base_dir, date),
|
||||||
f"screenshot-{timestamp}-of-{screen_name}.jpg",
|
f"screenshot-{timestamp}-of-{screen_name}.webp",
|
||||||
)
|
)
|
||||||
|
|
||||||
# 计算当前截图的哈希值
|
# 计算当前截图的哈希值
|
||||||
@ -124,15 +124,15 @@ def take_screenshot(
|
|||||||
}
|
}
|
||||||
|
|
||||||
# 使用 write_image_metadata 函数写入元数据
|
# 使用 write_image_metadata 函数写入元数据
|
||||||
img.save(jpeg_filename, format="JPEG", quality=85)
|
img.save(webp_filename, format="WebP", quality=85)
|
||||||
write_image_metadata(jpeg_filename, metadata)
|
write_image_metadata(webp_filename, metadata)
|
||||||
save_screen_sequences(base_dir, screen_sequences, date)
|
save_screen_sequences(base_dir, screen_sequences, date)
|
||||||
|
|
||||||
# 删除临时 PNG 文件
|
# 删除临时 PNG 文件
|
||||||
os.remove(temp_filename)
|
os.remove(temp_filename)
|
||||||
|
|
||||||
# 添加 JPEG 文件到截图列表
|
# 添加 WebP 文件到截图列表
|
||||||
screenshots.append(jpeg_filename)
|
screenshots.append(webp_filename)
|
||||||
# 记录成功的截图
|
# 记录成功的截图
|
||||||
worklog.write(f"{timestamp} - {screen_name} - Saved\n")
|
worklog.write(f"{timestamp} - {screen_name} - Saved\n")
|
||||||
|
|
||||||
|
@ -17,6 +17,8 @@ def write_image_metadata(image_path, metadata):
|
|||||||
metadata_info = PngInfo()
|
metadata_info = PngInfo()
|
||||||
metadata_info.add_text("Description", json.dumps(metadata))
|
metadata_info.add_text("Description", json.dumps(metadata))
|
||||||
img.save(image_path, "PNG", pnginfo=metadata_info)
|
img.save(image_path, "PNG", pnginfo=metadata_info)
|
||||||
|
elif image_path.lower().endswith(".webp"):
|
||||||
|
img.save(image_path, "WebP", quality=85, metadata=json.dumps(metadata))
|
||||||
else:
|
else:
|
||||||
print(f"Skipping unsupported file format: {image_path}")
|
print(f"Skipping unsupported file format: {image_path}")
|
||||||
|
|
||||||
@ -39,6 +41,12 @@ def get_image_metadata(image_path):
|
|||||||
return json.loads(existing_description)
|
return json.loads(existing_description)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
return {}
|
return {}
|
||||||
|
elif image_path.lower().endswith(".webp"):
|
||||||
|
existing_metadata = img.info.get("metadata", "{}")
|
||||||
|
try:
|
||||||
|
return json.loads(existing_metadata)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
return {}
|
||||||
else:
|
else:
|
||||||
print(f"Unsupported file format: {image_path}")
|
print(f"Unsupported file format: {image_path}")
|
||||||
return None
|
return None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user