Created
October 30, 2018 19:18
-
-
Save 0xa/2c64bf3765d2b83e342025198081840f to your computer and use it in GitHub Desktop.
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
#!/bin/env python3 | |
# uvccapture -d/dev/video0 -x2560 -y720 -osnap.jpg | |
# ./this.py snap.jpg | |
# -> output.jpg | |
from PIL import Image, ImageFilter | |
import numpy as np | |
import sys | |
im = Image.open(sys.argv[1]) | |
width = im.size[0] | |
height = im.size[1] | |
def get_array(i): | |
# let's ignore red and blue channels, they look empty and noisy. | |
# probably filtered by the hardware | |
(r, g, b) = i.split() | |
return np.array(g) | |
# Lower half is always black | |
# Left and right look to be the same, merged for good measure | |
left_im = im.crop((0, 0, width / 2, height / 2)) | |
right_im = im.crop((width / 2, 0, width, height / 2)) | |
# Extract useful data | |
left_arr = get_array(left_im) | |
right_arr = get_array(right_im) | |
# Merge sides | |
arr = (left_arr + right_arr) / 2 | |
# Normalization/leveling (`a` makes it darker) | |
limits = (arr.min(), arr.max()) | |
print("limits:", limits) | |
a = 65 | |
arr = np.interp(arr, (arr.min() + a, arr.max()), (0, 255)) | |
arr = np.rint(arr) | |
arr = arr.astype('uint8') | |
# Back to an Image, some smoothing, and horizontal scaling | |
g = Image.fromarray(arr, 'L') | |
scaled_w = int(round(width / 4)) | |
scaled_h = int(round(height / 2)) | |
g = g.filter(ImageFilter.GaussianBlur(radius=1)) | |
g = g.resize((scaled_w, scaled_h), Image.LANCZOS) | |
g.save('output.jpg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment