Skip to content

Instantly share code, notes, and snippets.

@AdamDimech
AdamDimech / blob_analysis.py
Created Dec 10, 2021
A procedure to perform a blob analysis in OpenCV using Python. Details at https://code.adonline.id.au/blob-analysis-with-opencv-in-python/
View blob_analysis.py
#!/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 Feb 18, 2021
Quantitatively check the quality of a compressed image by calculating the Structural Similarity Index (SSIM) and Mean Square Errors (MSE) between two images.
View mse-ssim.py
#!/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 Feb 11, 2021
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/
View psnr.py
#!/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 Jun 23, 2022
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/
View image_reporter.py
#!/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 Dec 11, 2020
Query a PostgreSQL database from Python. Further information at https://code.adonline.id.au/query-a-postgresql-database-via-python/
View sql_query.py
#!/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 Dec 3, 2020
A Python script for rotating images recursively through a directory. More information at https://code.adonline.id.au/selectively-rotating-images-in-python-recursively/
View rotate-images.py
#!/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 Oct 23, 2018
This generic script can be used in ImageJ "headless" mode to loop through files in a selected directory and do something to them.
View ImageJ_Loop_Through_Files_Headless.ijm
// 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 Dec 15, 2017
Convert tables in a SQL database to individual CSVs
View database_tables_to_csv.sql
/*
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;
@AdamDimech
AdamDimech / ImageJ_Loop_Through_Files.ijm
Last active Mar 14, 2017
This generic script can be used in ImageJ to loop through files in a selected directory and do something to them.
View ImageJ_Loop_Through_Files.ijm
// Blank ImageJ Macro Script that loops through files in a directory
// Written by Adam Dimech
// Also available at https://gist.github.com/AdamDimech/cd3999f6fe9eddfa55c19d6fd3997bbc
// Specify global variables
input = getDirectory("Input Directory");
output = input; // Output images to the same directory as input (prevents second dialogue box, otherwise getDirectory("Output Directory"))
Dialog.create("File Type");
Dialog.addString("File Suffix: ", ".png", 5);
@AdamDimech
AdamDimech / Date_Time_Output.ijm
Created Mar 14, 2017
ImageJ Date and Time Output
View Date_Time_Output.ijm
// ImageJ Macro Code
// Output timestamp in format EEE dd MMM yyyy, hh:mm:ss
MonthNames = newArray("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
DayNames = newArray("Sun", "Mon","Tue","Wed","Thu","Fri","Sat");
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
print (DayNames[dayOfWeek], dayOfMonth, MonthNames[month], year + "," + hour + ":" + minute + ":" + second);