Skip to content

Instantly share code, notes, and snippets.

View ghandic's full-sized avatar

Andy Challis ghandic

View GitHub Profile
@ghandic
ghandic / fastapi_query_utils.py
Last active April 22, 2024 04:02
fastapi sortby/filter
from enum import auto
from typing import List, Optional
from collections import Counter
from fastapi_utils.enums import StrEnum
from fastapi_utils.inferring_router import InferringRouter
from pydantic import BaseModel
from fastapi import HTTPException, Query, Depends
router = InferringRouter()
@ghandic
ghandic / r_rename_ext.sh
Created February 27, 2019 01:57
Recursive rename of file extension
find . -regex '.*.py' -type f | sed "s/\.py$//" | xargs -I% mv -iv %.py %.txt
@ghandic
ghandic / s3_pdf.py
Created July 27, 2018 12:04
Load pdf from S3 directly into memory as list of PIL images
"""This module requires:
- poppler-utils [apt-get upde && apt-get install poppler-utils]
- pdf2image [pip install pdf2image]
- pillow [pip install pillow]
- boto3 [pip install boto3]
"""
import boto3
from pdf2image import convert_from_bytes
@ghandic
ghandic / pil_s3.py
Last active March 15, 2024 14:16
Load image from S3 directly into memory as PIL image and write to S3 directly from memory from PIL image
import boto3
from PIL import Image
from io import BytesIO
import os
class S3ImagesInvalidExtension(Exception):
pass
class S3ImagesUploadFailed(Exception):
pass
@ghandic
ghandic / contour_field_detection.py
Created July 26, 2018 11:38
Function to detect fields from a form, may need fine tuning!
from PIL import Image
import numpy as np
import cv2
import time
# Helper function for jupyter notebooks
def showarray(a):
return Image.fromarray(a)
def detect_boxes(img, min_percent_of_area=0.02, max_percent_of_area=2):
@ghandic
ghandic / pandas_s3.py
Last active June 5, 2023 11:40
Load csv from S3 directly into memory and write to S3 directly from memory by extending pd.DataFrame class
import boto3
import pandas as pd
from io import StringIO
class S3DataFrame(pd.DataFrame):
"""
# Make a dataframe and upload it as csv
s3df = S3DataFrame({'h1':[1], 'h2':[2]})
s3df.to_s3(Bucket='bucket-name',
@ghandic
ghandic / faster_tess.py
Created July 17, 2018 16:29
Running Tesseract with lots of small images is much slower than multipage TIFF, so make a multipage tiff (to be implemented with multiprocessing for the major speed gains)
## Tesseract4
# 2 images into tiff: 843 msec 796
# 10 images into tiff: 1003 msec
# 53 images (same as page 1) into tiff: 1740 msec
## Tesseract3
# 2 images into tiff: 727 msec
# 10 images into tiff: 816 msec
# 53 images (same as page 1) into tiff: 1620 msec
@ghandic
ghandic / png_with_meta.py
Created July 12, 2018 08:08
Save image as png with metadata
from PIL import Image
from PIL.PngImagePlugin import PngInfo
im = Image.open('example.png')
im_info = PngInfo()
im_info.add_text('Key Information', 'Value Information')
im.save('example.png', pnginfo=im_info)
@ghandic
ghandic / labeled_gif.py
Last active July 6, 2018 09:16
Creates a labeled gif using opencv imageio and numpy
import imageio
from typing import NamedTuple
import cv2
import numpy as np
LabeledGIFState = NamedTuple('LabeledGIFState', [('image', np.ndarray), ('label', str)])
class GifSettings(object):
red = [255, 0, 0]
@ghandic
ghandic / imessage.sh
Created June 18, 2018 16:40
Send an automated iMessage
targetBuddyPhone='+44765979706'
targetMessage='This is a automated message'
osascript <<EOF
tell application "Messages"
set targetService to 1st service whose service type = iMessage
set targetBuddy to buddy "$targetBuddyPhone" of targetService
send "$targetMessage" to targetBuddy
end tell
EOF