Created
October 11, 2025 07:40
-
-
Save akasakakona/287c5c3100cb23b180c95fe5e7779244 to your computer and use it in GitHub Desktop.
Bad Apple Maker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import numpy as np | |
| video = "Bad_Apple.mp4" | |
| capture = cv2.VideoCapture(video) | |
| if not capture.isOpened(): | |
| print("Error opening video file") | |
| exit(1) | |
| fps = capture.get(cv2.CAP_PROP_FPS) | |
| width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
| height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
| fourcc = cv2.VideoWriter_fourcc(*'mp4v') | |
| out = cv2.VideoWriter('output.mp4', fourcc, fps, (width, height), isColor=False) | |
| frame_i = 0 | |
| blkSize = 20 | |
| thresh = 127 | |
| while True: | |
| ret, frame = capture.read() | |
| if not ret: | |
| break | |
| gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| output = np.zeros_like(gray_frame) | |
| for i in range(0, height, blkSize): | |
| for j in range(0, width, blkSize): | |
| block = gray_frame[i:i+blkSize, j:j+blkSize] | |
| avg = np.mean(block) | |
| color = 255 if avg > thresh else 0 | |
| output[i:i+blkSize, j:j+blkSize] = color | |
| out.write(output) | |
| capture.release() | |
| out.release() | |
| cv2.destroyAllWindows() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment