Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
import os
import csv
import glob
import sys
import argparse
from datetime import datetime
from csv import reader
@AdamDimech
AdamDimech / listfiles.py
Last active January 18, 2023 06:13
List all files in a directory and save their details into a CSV file. Details: https://code.adonline.id.au/list-directory-contents-in-csv-file-using-python/
#!/usr/bin/env python
import os
import csv
import sys
import csv
from datetime import datetime
import argparse
import glob
@AdamDimech
AdamDimech / blob_analysis.py
Created December 10, 2021 05:10
A procedure to perform a blob analysis in OpenCV using Python. Details at https://code.adonline.id.au/blob-analysis-with-opencv-in-python/
#!/usr/bin/python
import matplotlib as mpl
import matplotlib.pyplot as plt
import cv2
import numpy as np
import csv
colours = [(230, 63, 7), (48, 18, 59), (68, 81, 191), (69, 138, 252), (37, 192, 231), (31, 233, 175), (101, 253, 105), (175, 250, 55), (227, 219, 56), (253, 172, 52), (246, 108, 25), (216, 55, 6), (164, 19, 1), (90, 66, 98), (105, 116, 203), (106, 161, 253), (81, 205, 236), (76, 237, 191), (132, 253, 135), (191, 251, 95), (233, 226, 96), (254, 189, 93), (248, 137, 71), (224, 95, 56), (182, 66, 52), (230, 63, 7), (48, 18, 59), (68, 81, 191), (69, 138, 252), (37, 192, 231), (31, 233, 175), (101, 253, 105), (175, 250, 55), (227, 219, 56), (253, 172, 52), (246, 108, 25), (216, 55, 6), (164, 19, 1), (90, 66, 98), (105, 116, 203), (106, 161, 253), (81, 205, 236), (76, 237, 191), (132, 253, 135), (191, 251, 95), (233, 226, 96), (254, 189, 93), (248, 137, 71), (224, 95, 56), (182, 66, 52)]
@AdamDimech
AdamDimech / mse-ssim.py
Created February 18, 2021 07:36
Quantitatively check the quality of a compressed image by calculating the Structural Similarity Index (SSIM) and Mean Square Errors (MSE) between two images.
#!/usr/bin/env python
from skimage.metrics import structural_similarity as ssim
import numpy as np
import cv2
import argparse
def options():
parser = argparse.ArgumentParser(description="Read image metadata")
parser.add_argument("-o", "--first", help="Input image file.", required=True)
@AdamDimech
AdamDimech / psnr.py
Created February 11, 2021 23:36
Quantitatively check the quality of a compressed image by calculating the Peak Signal-to-Noise Ratio (PSNR) between two images. More info at https://code.adonline.id.au/peak-signal-to-noise-ratio-python/
#!/usr/bin/env python
from math import log10, sqrt
import cv2
import numpy as np
import argparse
# Based on https://www.geeksforgeeks.org/python-peak-signal-to-noise-ratio-psnr/
def options():
@AdamDimech
AdamDimech / image_reporter.py
Last active June 23, 2022 10:29
A Python script for displaying EXIF metadata from JPEG, TIF, PNG, GIF, BMP, CR2 and NEF files. More information at https://code.adonline.id.au/reading-exif-data-in-python/
#!/usr/bin/env python
import imageio
import exifread
from PIL import Image, ExifTags
from PIL.ExifTags import TAGS
from PIL.PngImagePlugin import PngImageFile, PngInfo
import re
import os
from rawphoto.cr2 import Cr2
@AdamDimech
AdamDimech / sql_query.py
Last active December 11, 2020 03:22
Query a PostgreSQL database from Python. Further information at https://code.adonline.id.au/query-a-postgresql-database-via-python/
#!/usr/bin/env python
import psycopg2
try:
login = "dbname='YourDatabaseName' user='Adam' host='127.0.0.1' " + \
"password='MyPa$$word'"
# Establish a connection
conn = psycopg2.connect(login)
# Create a psycopg2 cursor that can execute queries
@AdamDimech
AdamDimech / rotate-images.py
Created December 3, 2020 22:28
A Python script for rotating images recursively through a directory. More information at https://code.adonline.id.au/selectively-rotating-images-in-python-recursively/
#!/usr/bin/env python
import os
import fnmatch
import argparse
import sys
import cv2
# Intro Text
print("\033[1;34;40m\n\nRotate images \033[0m")
@AdamDimech
AdamDimech / ImageJ_Loop_Through_Files_Headless.ijm
Created October 23, 2018 04:46
This generic script can be used in ImageJ "headless" mode to loop through files in a selected directory and do something to them.
// Blank ImageJ Macro Script that loops through files in a directory
// Written by Adam Dimech
// https://code.adonline.id.au/imagej-batch-process-headless/
// Specify global variables
#@String input
#@String suffix
// Add trailing slashes
@AdamDimech
AdamDimech / database_tables_to_csv.sql
Created December 15, 2017 04:59
Convert tables in a SQL database to individual CSVs
/*
1. Log in to psql as a superuser
2. CHMOD target directory to 777
3. Execute via SELECT db_to_csv('/path/to/output');
4. Use complete file path.
*/
CREATE OR REPLACE FUNCTION db_to_csv(path TEXT) RETURNS void AS $$
declare
tables RECORD;