Skip to content

Commit 24e8f98

Browse files
committed
fix: stop RGB->BGR->RBG - keep BGR and save a RGB
1 parent 3a574b6 commit 24e8f98

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/movie_barcodes/barcode_generation.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ def generate_circular_barcode(colors: list, img_size: int, scale_factor: int = 1
3535
):
3636
radius = (idx + 1) * radius_increment
3737

38-
# Handle both simple RGB tuples and smoothed frames
38+
# Handle both simple BGR tuples and smoothed frames
3939
if isinstance(color, np.ndarray) and color.ndim > 1:
40-
# For smoothed frames, take the average color
40+
# For smoothed frames, take the average BGR color
4141
color = np.mean(color, axis=(0, 1)).astype(int)
4242

43-
# Ensure color is in BGR format for OpenCV
44-
bgr_color = color[::-1] if len(color) == 3 else color[2::-1]
43+
# Colors are BGR throughout the pipeline; draw directly in BGR
44+
bgr_color = color
4545

4646
cv2.circle(
4747
barcode_high_res,
@@ -89,8 +89,7 @@ def generate_barcode(
8989
# For single color values
9090
color_column = np.full((frame_height, 1, 3), color, dtype=np.uint8)
9191

92-
# Convert to BGR (which will be interpreted as RGB when saved with PIL)
93-
bgr_color = cv2.cvtColor(color_column, cv2.COLOR_RGB2BGR)
94-
barcode[:, i] = bgr_color.reshape(frame_height, 3)
92+
# Keep BGR internally; perform RGB conversion once at save-time
93+
barcode[:, i] = color_column.reshape(frame_height, 3)
9594

9695
return barcode

src/movie_barcodes/utility.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,13 @@ def save_barcode_image(barcode: np.ndarray, base_name: str, args: argparse.Names
160160
destination_path = path.join(project_root, destination_path)
161161

162162
if barcode.shape[2] == 4: # If the image has an alpha channel (RGBA)
163-
image = Image.fromarray(barcode, "RGBA")
163+
# Convert BGRA -> RGBA once at save-time
164+
barcode_to_save = cv2.cvtColor(barcode, cv2.COLOR_BGRA2RGBA)
165+
image = Image.fromarray(barcode_to_save, "RGBA")
164166
else: # If the image doesn't have an alpha channel (RGB)
165-
image = Image.fromarray(barcode, "RGB")
167+
# Convert BGR -> RGB once at save-time
168+
barcode_to_save = cv2.cvtColor(barcode, cv2.COLOR_BGR2RGB)
169+
image = Image.fromarray(barcode_to_save, "RGB")
166170

167171
image.save(destination_path)
168172
logging.info("File saved at '%s'", destination_path)

src/movie_barcodes/video_processing.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def parallel_extract_colors(
6262
(i + 1) * frames_per_worker - 1,
6363
color_extractor,
6464
target_frames_per_worker,
65+
False, # disable worker progress bars
6566
)
6667
for i in range(workers)
6768
]
@@ -73,6 +74,7 @@ def parallel_extract_colors(
7374
frame_count - 1,
7475
color_extractor,
7576
target_frames - (workers - 1) * target_frames_per_worker,
77+
False,
7678
)
7779

7880
results = pool.starmap(extract_colors, args)
@@ -89,6 +91,7 @@ def extract_colors(
8991
end_frame: int,
9092
color_extractor: Callable,
9193
target_frames: Optional[int] = None,
94+
show_progress: bool = True,
9295
) -> List:
9396
"""
9497
Extracts dominant colors from frames in a video file.
@@ -98,6 +101,7 @@ def extract_colors(
98101
:param int end_frame: The index of the last frame to process.
99102
:param Callable color_extractor: A function to extract the dominant color from a frame.
100103
:param Optional[int] target_frames: The total number of frames to sample.
104+
:param bool show_progress: Whether to display a tqdm progress bar during extraction.
101105
:return: List of dominant colors from the sampled frames.
102106
"""
103107
video = cv2.VideoCapture(video_path)
@@ -112,7 +116,11 @@ def extract_colors(
112116

113117
colors = []
114118

115-
for _ in tqdm(range(target_frames or total_frames), desc="Processing frames"):
119+
iterator = range(target_frames or total_frames)
120+
if show_progress:
121+
iterator = tqdm(iterator, desc="Processing frames")
122+
123+
for _ in iterator:
116124
ret, frame = video.read() # Read the first or next frame
117125
if ret:
118126
dominant_color = color_extractor(frame)

0 commit comments

Comments
 (0)