Skip to content

Instantly share code, notes, and snippets.

View BilHim's full-sized avatar

Bilal Himite BilHim

View GitHub Profile
import os
import keyboard
import pyaudio, wave
import numpy.random as random
# Settings
chunk = 1024
channels = 2
sample_format = pyaudio.paInt16
# Coordinates in old image
x_ = x/scale_x
y_ = y/scale_y
# Finding neighboring points
x1 = min(int(np.floor(x_)), width-1)
y1 = min(int(np.floor(y_)), height-1)
x2 = min(int(np.ceil(x_)), width-1)
y2 = min(int(np.ceil(y_)), height-1)
x_nearest = int(np.round(x/scale_x))
y_nearest = int(np.round(y/scale_y))
pixel = image.getpixel((x_nearest, y_nearest))
import numpy as np
from PIL import Image
# Import image and get its dimensions
image = Image.open('image.jpg')
width, height = image.size
# Create a new empty image
new_width, new_height = 480, 320
scaled_image = Image.new(image.mode, (new_width, new_height), 'white')
# Load image pixels
data = np.array(image)
# Half kernel size
a = kernel_size // 2
for x in range(width):
for y in range(height):
# min and max are used for edge pixels
x_start = max(x - a, 0)
for x in range(width):
for y in range(height):
r, g, b = image.getpixel((x, y))
r_ = 255 * (r/255)**gamma
g_ = 255 * (g/255)**gamma
b_ = 255 * (b/255)**gamma
new_pixel = (int(r_), int(g_), int(b_))
new_image.putpixel((x, y), new_pixel)
# Calculate factor
if beta == 255: alpha = np.infty
else: alpha = (255+beta)/(255-beta)
for x in range(width):
for y in range(height):
r, g, b = image.getpixel((x, y))
μ = (r+g+b)/3
@BilHim
BilHim / contrast.py
Last active February 17, 2021 19:27
# Converting image into a numpy array with a dimension (width, height, 3)
data = np.array(image)
# Calculate average brightness
μ = np.mean(data, axis=2)
μ_mean = μ.mean()
# Calculate factor
if beta == 255: alpha = np.infty
else: alpha = (255+beta)/(255-beta)
def truncate(x):
'''makes sure returned value is between 0 and 255'''
return min(255, max(0, x))
# Brightness filter
for x in range(width):
for y in range(height):
r, g, b = image.getpixel((x, y))
# d is the brightness increase
r_ = truncate(r + d)
g_ = truncate(g + d)
b_ = truncate(b + d)