Skip to content

Instantly share code, notes, and snippets.

@HiCraigChen
HiCraigChen / copy files from docker.sh
Last active September 19, 2019 04:00
copy files from docker
# Open another terminal to find your running container id
docker ps
# CONTAINER ID IMAGE COMMAND...
#<CONTAINER ID> py36lambda "bash" ...
# Copy the files from container to local
docker cp <CONTAINER_ID>:/path/to/file/in/docker /path/to/local/directory
@HiCraigChen
HiCraigChen / Get package files.sh
Created September 19, 2019 04:10
Get package files
# Install two utilities to download and extract .rpm files
yum install yum-utils rpmdevtools
# Download .rpm files
yumdownloader <package_name>
# Extract files from .rpm
rpmdev-extract *.rpm
@HiCraigChen
HiCraigChen / app.py
Last active September 20, 2019 04:43
pdftotext python code
from chalice import Chalice
import os
app = Chalice(app_name='pdftotext')
@app.route('/')
def index():
# Get a sample pdf file
os.system("curl https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf -o /tmp/file.pdf")
@HiCraigChen
HiCraigChen / app.py
Last active September 25, 2019 01:10
Convert PDF to image using Python wand library
from wand.image import Image
import os
# Download and read pdf file
os.system("curl https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf -o /tmp/file.pdf")
f = open('/tmp/file.pdf','rb')
infile = f.read()
f.close()
with Image(blob=infile,resolution=150) as img_pdf:
@HiCraigChen
HiCraigChen / app.py
Last active February 9, 2023 13:39
Convert PDF to image using Python with pdf2image library
from chalice import Chalice, Response
from pdf2image import convert_from_bytes
import os
from io import BytesIO
app = Chalice(app_name='pdf2image')
@app.route('/')
def index():
@HiCraigChen
HiCraigChen / bluetooth_test.py
Last active August 13, 2020 12:54
Enable bluetooth socket (server socket) to communicate with other device (client socket) using python
import bluetooth
server_socket=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
port = 1
server_socket.bind(("",port))
server_socket.listen(1)
client_socket,address = server_socket.accept()
print("Accepted connection from ",address)
while True:
import bluetooth
import subprocess
class BluetoothComm:
def __init__(self):
self.server_socket=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
port = 1
self.server_socket.bind(("",port))
self.server_socket.listen(1)
self.client_socket,address = self.server_socket.accept()
print("Accepted connection from ",address)
import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(6.4*5, 4.8*5), constrained_layout=False)
img_c1 = cv2.imread("left01.jpg", 0)
img_c2 = np.fft.fft2(img_c1)
img_c3 = np.fft.fftshift(img_c2)
img_c4 = np.fft.ifftshift(img_c3)
def distance(point1,point2):
return sqrt((point1[0]-point2[0])**2 + (point1[1]-point2[1])**2)
def idealFilterLP(D0,imgShape):
base = np.zeros(imgShape[:2])
rows, cols = imgShape[:2]
center = (rows/2,cols/2)
for x in range(cols):
for y in range(rows):
if distance((y,x),center) < D0:
def distance(point1,point2):
return sqrt((point1[0]-point2[0])**2 + (point1[1]-point2[1])**2)
def butterworthLP(D0,imgShape,n):
base = np.zeros(imgShape[:2])
rows, cols = imgShape[:2]
center = (rows/2,cols/2)
for x in range(cols):
for y in range(rows):
base[y,x] = 1/(1+(distance((y,x),center)/D0)**(2*n))