Skip to content

Instantly share code, notes, and snippets.

@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 Apr 1, 2021
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);
@AdamDimech
AdamDimech / UNC-from-file-path.ps1
Last active Mar 7, 2017
Select a file to determine its UNC path
View UNC-from-file-path.ps1
# UNC File Path Finder
# Written by Adam Dimech
# Based on https://code.adonline.id.au/unc-path-from-local-path-in-powershell/
# 1 March 2017
Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
Multiselect = $false # Only one file can be chosen
InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"; #Opens in My Computer
Filter = 'All File Types|*.*' # Specified file types