Last active
May 18, 2022 21:59
-
-
Save austinbhale/268a8fbacbcba90fb006b035b087e46e to your computer and use it in GitHub Desktop.
Generate Psychedelic Effects in OpenCV Python
This file contains 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 | |
def psycho(img, trippy_factor=1./3): | |
ris = img.astype(np.float32) | |
ris += cv2.randn(img.copy(), 0, 10) | |
kernel = np.zeros((3,3), dtype=np.float32) | |
kernel.fill(trippy_factor) | |
for r, c in np.ndindex(kernel.shape): | |
ris += kernel[r][c] * ris | |
return ris.astype(np.uint8) | |
if __name__ == "__main__": | |
filename = "elephant.jpg" | |
input_img = cv2.imread(filename, cv2.IMREAD_UNCHANGED) | |
for i in range(2,10): | |
output_img = psycho(input_img, trippy_factor=1./float(i)) | |
square_img = True | |
if square_img: | |
h, w, _ = output_img.shape | |
sq = h if h > w else w | |
output_img = cv2.resize(output_img, (sq, sq), interpolation=cv2.INTER_AREA) | |
compress = True | |
output_name = "psych-{}-{}".format(i, filename) | |
if compress: | |
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 50] | |
cv2.imwrite(output_name, output_img, encode_param) | |
else: | |
cv2.imwrite(output_name, output_img) | |
print("Done writing {}".format(output_name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment