Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(video): preserve grayscale image brightness (#1620)

Rudy Mizrahi Celeklicae383304a73
diff --git a/app/services/video.py b/app/services/video.py
index c9a03349..c5f7a518 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -727,7 +727,13 @@ def _sanitize_image_file(image_path: str) -> str:
# Strip metadata after applying the camera's orientation.
# Palette transparency belongs to pixels, not removable metadata.
mode = "RGBA" if "A" in upright.getbands() or "transparency" in upright.info else "RGB"
- cleaned_image = upright.convert(mode)
+ if upright.mode == "I;16":
+ # PNG grayscale samples span0..65535; an RGB cast either
+ # clips them to white or wraps when MoviePy encodes them.
+ with upright.point(lambda value: value / 257 + 0.5) as byte_range:
+ cleaned_image = byte_range.convert(mode)
+ else:
+ cleaned_image = upright.convert(mode)
cleaned_image.info.clear()
source_digest.update(str(cleaned_image.size).encode())
source_digest.update(cleaned_image.mode.encode())
@@ -756,6 +762,7 @@ def _open_image_clip_with_fallback(image_path: str):
# MoviePy does not apply camera EXIF orientation while decoding an image.
# CMYK JPEG channels must become RGB; LA must become RGBA so MoviePy
# creates a proper opacity mask rather than treating alpha as a color band.
+ # Bilevel pixels must become byte-range RGB rather than bool values0/1.
# Ordinary RGB inputs retain the direct path.
try:
with Image.open(image_path) as image:
@@ -764,7 +771,7 @@ def _open_image_clip_with_fallback(image_path: str):
except Exception:
orientation = 1
image_mode = None
- if orientation in range(2, 9) or image_mode in {"CMYK", "LA"}:
+ if orientation in range(2, 9) or image_mode in {"CMYK", "LA", "1", "I;16"}:
sanitized_path = _sanitize_image_file(image_path)
return ImageClip(sanitized_path), sanitized_path
diff --git a/test/services/test_image_bilevel_pixel_range.py b/test/services/test_image_bilevel_pixel_range.py
new file mode 100644
index 00000000..2539e664
--- /dev/null
+++ b/test/services/test_image_bilevel_pixel_range.py
@@ -0,0 +1,18 @@
+import pytest
+from PIL import Image
+from moviepy import VideoFileClip
+
+from app.services.video import close_clip, render_image_zoom_video
+
+
[email protected]("extension", ["png", "bmp"])
+def test_bilevel_white_pixels_remain_white_in_native_render(tmp_path, extension):
+ source = tmp_path / f"bilevel.{extension}"
+ Image.new("1", (32, 32), 1).save(source)
+ output = render_image_zoom_video(str(source), 0.1)
+ clip = VideoFileClip(output, audio=False)
+ try:
+ pixel = clip.get_frame(0)[16, 16]
+ assert all(int(channel) >= 245 for channel in pixel)
+ finally:
+ close_clip(clip)
diff --git a/test/services/test_image_uint16_pixel_range.py b/test/services/test_image_uint16_pixel_range.py
new file mode 100644
index 00000000..d0623b85
--- /dev/null
+++ b/test/services/test_image_uint16_pixel_range.py
@@ -0,0 +1,19 @@
+import numpy as np
+import pytest
+from PIL import Image
+from moviepy import VideoFileClip
+
+from app.services.video import close_clip, render_image_zoom_video
+
+
[email protected]('source_value,expected', [(16384, 64), (32768, 128)])
+def test_uint16_grayscale_retains_brightness_in_native_render(tmp_path, source_value, expected):
+ source = tmp_path / 'grayscale16.png'
+ Image.fromarray(np.full((32, 32), source_value, dtype=np.uint16)).save(source)
+ output = render_image_zoom_video(str(source), 0.1)
+ clip = VideoFileClip(output, audio=False)
+ try:
+ pixel = clip.get_frame(0)[16, 16]
+ assert all(abs(int(channel) - expected) <= 5 for channel in pixel)
+ finally:
+ close_clip(clip)