Skip to content

Instantly share code, notes, and snippets.

View InputBlackBoxOutput's full-sized avatar
🐮

Rutuparn Pawar InputBlackBoxOutput

🐮
  • Boston, MA
View GitHub Profile
@InputBlackBoxOutput
InputBlackBoxOutput / resize.py
Created October 12, 2021 02:54
Resize an image to a square image with the help of minimal padding
from PIL import Image
def resize_image(img_path, desired_size = 300, background_color=None):
img = Image.open(img_path)
old_size = img.size
if background_color == None:
background_color = img.load()[0,0]
@InputBlackBoxOutput
InputBlackBoxOutput / pixels.py
Created October 12, 2021 02:39
Perform operations on pixels in an image
import cv2
import numpy as np
from PIL import Image
def process_pixels(img):
pixel_data = list(img.getdata())
pixels = np.array(pixel_data).flatten()
width = img.size[0]
height = img.size[1]
@InputBlackBoxOutput
InputBlackBoxOutput / remove_duplicate.py
Created October 4, 2021 11:07
Remove duplicate files in a directory identified by comparing SHA256 hash
import os
import hashlib
import glob
# Get the SHA-256 hash of a file
def sha256(fname, size=4096):
sha256_hash = hashlib.sha256()
with open(fname, 'rb') as f:
for byte_block in iter(lambda: f.read(4096), b""):
@InputBlackBoxOutput
InputBlackBoxOutput / detect_edges.py
Last active January 28, 2025 16:48
Canny edge detector with threshold calculation using Otsu's method
import cv2
import numpy as np
def calculate_threshold(img):
hist, bin_edges = np.histogram(img, bins=256)
bin_mids = (bin_edges[:-1] + bin_edges[1:]) / 2.
weight1 = np.cumsum(hist)
weight2 = np.cumsum(hist[::-1])[::-1]
mean1 = np.cumsum(hist * bin_mids) / weight1
@InputBlackBoxOutput
InputBlackBoxOutput / convert_delimiter.py
Created September 10, 2021 10:42
Python program for changing the delimiter of a CSV file
import re, sys
input_file ="input.csv"
output_file ="output.csv"
input_file_delimiter = ';'
output_file_delimiter = ','
output = []
with open(input_file) as f_input:
lines = f_input.read().splitlines()
@InputBlackBoxOutput
InputBlackBoxOutput / word_cloud.py
Created September 7, 2021 15:53
Create a word cloud using the 'wordcloud' module
from wordcloud import WordCloud
import matplotlib.pyplot as plt
def plot_wordcloud(text, mask=None, max_words=200, max_font_size=100, figure_size=(12.0,12.0),
title = None, title_size=18, image_color=False):
wordcloud = WordCloud(background_color='white',
max_words = max_words,
max_font_size = max_font_size,
random_state = 42,
@InputBlackBoxOutput
InputBlackBoxOutput / csv_json.py
Last active September 6, 2021 17:12
Convert a text file or CSV file into a JSON file
import json
seperator = ','
with open("text.txt", encoding='utf-8') as file:
data = file.read().splitlines()
print(len(data))
json_object = {}
for i, each in enumerate(data):
@InputBlackBoxOutput
InputBlackBoxOutput / dulicate_entries.py
Created August 17, 2021 06:25
Python program to remove duplicate entries in a text or CSV file
with open("text.txt", 'r') as infile:
data = infile.read().splitlines()
# print(data)
with open("text.txt", 'w') as outfile:
for each in list(set(data)):
outfile.write(each + '\n')
@InputBlackBoxOutput
InputBlackBoxOutput / image_data_augmentation.py
Created August 6, 2021 04:23
Image data augmentation using openCV
# Image Data augmentation
# Use this program to perform image data augmentation in case you do not want to use a generator for some reason
# Check out ImageDataGenerator if you are looking for a generator in TensorFlow: https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator
import cv2
import numpy as np
import glob
import hashlib
search_pattern = "images/*.jpg"
@InputBlackBoxOutput
InputBlackBoxOutput / timeseries_plot.py
Created August 6, 2021 04:06
Python program to plot timeseries data stored in a CSV file. Format: <timestamp>,<value>\n
import sys
try:
import matplotlib.pyplot as plt
except ModuleNotFoundError:
print("Error: Module was not found")
sys.exit()
except:
print("Something went wrong while importing module/s \n")
sys.exit()