Skip to content

Instantly share code, notes, and snippets.

View prerakmody's full-sized avatar
🏠
Working from home

pmod prerakmody

🏠
Working from home
View GitHub Profile
@prerakmody
prerakmody / video_frames.py
Created June 26, 2018 08:58
Video based code snippets
import imageio
reader = imageio.get_reader(os.path.join(folder_videos, video))
nframes = reader.get_meta_data()['nframes']
for j,im in enumerate(reader):
print (j, im)
@prerakmody
prerakmody / PIL_Image.py
Created June 27, 2018 09:52
Image manipulation codes
import io
import sys
from PIL import Image
def img_read_save(url, quality, format_='PNG'):
img = Image.open(url)
output = io.BytesIO()
img.save(output, format=format_, subsampling=0, quality=quality)
contents = output.getvalue()
print (type(contents))
@prerakmody
prerakmody / requests.py
Created July 16, 2018 11:21
Python Requests Library
import requests
if __name__ == "__main__":
r = requests.get(remote_url)
with open(local_url, 'wb') as fp:
fp.write(r.content)
@prerakmody
prerakmody / colors_random.py
Created July 20, 2018 04:49
Code Snipets on Colors in python
import random
import colorsys
def get_random_color():
h,s,l = random.random(), 0.5 + random.random()/2.0, 0.4 + random.random()/5.0
r,g,b = [int(256*i) for i in colorsys.hls_to_rgb(h,l,s)]
person_color1 = '#%02x%02x%02x' % (r,g,b)
person_color2 = "#" + "%06x" % random.randint(0, 0xFFFFFF)
return [(r,g,b), person_color1], person_color2
@prerakmody
prerakmody / beautiful_soup.py
Last active September 17, 2020 17:22
Beautiful Soup
import requests
from bs4 import BeautifulSoup
def get_framecount(url):
sess = requests.Session()
r = sess.get(url)
soup = BeautifulSoup(r.text, "lxml")
div = soup.find_all('div', {'class' : 'raw-block'})
frames_count = []
@prerakmody
prerakmody / datetime.py
Created July 23, 2018 10:42
Python Datetime
import datetime
if __name__ == "__main__":
a = datetime.datetime.strptime('2011-09-26 14:14:11.124700931'[:-3], '%Y-%m-%d %H:%M:%S.%f')
b = datetime.datetime.strptime('2011-09-26 14:15:16.432700931'[:-3], '%Y-%m-%d %H:%M:%S.%f')
diff = (b-a)
print (diff.seconds, diff.microseconds)
@prerakmody
prerakmody / requests.py
Last active July 24, 2018 07:03
Python Requests Lib
import os
import zipfile
import requests
def download_data(url_local, url_remote, root_dir):
with open(url__local, 'wb') as fp:
r = requests.get(url_remote)
fp.write(r.content)
zip_ref = zipfile.ZipFile(url_local, 'r')
@prerakmody
prerakmody / subprocess.py
Last active July 26, 2018 06:39
Python Subprocess Module
from subprocess import Popen, PIPE
def run(command):
process = Popen(command, stdout=PIPE, shell=True)
while True:
line = process.stdout.readline().rstrip()
if not line:
break
yield line
@prerakmody
prerakmody / cv2_findContours.py
Last active December 5, 2018 16:27
CV2 samples
"""
# References
- https://docs.opencv.org/3.0.0/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
- https://docs.opencv.org/3.0.0/d4/d73/tutorial_py_contours_begin.html
- contours :: Detected contours. Each contour is stored as a vector of points.
- len(contours) == total no. of contours
- hierarchy :: as many elements as the number of contours
- [[next_contour, prev_contour, child_contour, parent_countour], [], [], .. , []]
"""
@prerakmody
prerakmody / packages.py
Created July 26, 2018 06:36
Python Path Basics
import sys
import site
import numpy as np
if __name__ == "__main__":
print (sys.executable)
print (site.getsitepackages())
print (np.__file__)