Skip to content

Instantly share code, notes, and snippets.

@atadams
Last active June 21, 2024 00:59
Show Gist options
  • Save atadams/185846fabcd13ee5b04cbe237357b211 to your computer and use it in GitHub Desktop.
Save atadams/185846fabcd13ee5b04cbe237357b211 to your computer and use it in GitHub Desktop.
Python scripts that compares PCA mean vectors for altered images and unaltered images from raw files
import cv2 as cv
import numpy as np
import rawpy
def perform_pca(filepath, alter_image=False):
if alter_image:
# Read the CR2 image with auto white balance or auto brightness
rgb_image = rawpy.imread(filepath).postprocess()
else:
# Read the CR2 image without auto white balance or auto brightness
rgb_image = rawpy.imread(file_path).postprocess(no_auto_bright=True, use_auto_wb=False, use_camera_wb=True)
# Reshape the image data to a 2D array of RGB values
x = rgb_image.reshape(-1, 3).astype(np.float32)
# Perform PCA using OpenCV's PCACompute
rgb_means, eigenvectors, eigenvalues = cv.PCACompute2(x, mean=None)
# get the mean of each channel of the image using numpy
rgb_means_numpy = x.mean(axis=0)
return rgb_means, rgb_means_numpy
path_name = '/path/to/cr2-files/' # Replace with your CR2 file path
filenames = ['IMG_1837.CR2', 'IMG_1839.CR2'] # Replace with your CR2 filenames
for filename in filenames:
file_path = f'{path_name}{filename}'
# Step 1: Perform PCA on the ALTERED CR2 image
altered_rgb_means, altered_rgb_means_numpy = perform_pca(file_path, alter_image=True)
# Step 2: Print the mean PCA values for the ALTERED CR2 image
print(f"ALTERED image {filename}:")
print(
f"PCA mean vectors: R: {altered_rgb_means[0][0]:.6f} G: {altered_rgb_means[0][1]:.6f} B: {altered_rgb_means[0][2]:.6f}")
print(
f" Numpy RGB means: R: {altered_rgb_means_numpy[0]:.6f} G: {altered_rgb_means_numpy[1]:.6f} B: {altered_rgb_means_numpy[2]:.6f}")
print()
# Step 3: Perform PCA on the UNALTERED CR2 image
unaltered_rgb_means, unaltered_rgb_means_numpy = perform_pca(file_path, alter_image=False)
# Step 4: Print the mean PCA values for the UNALTERED CR2 image
print(f"UNALTERED image {filename}:")
print(
f"PCA mean vectors: R: {unaltered_rgb_means[0][0]:.6f} G: {unaltered_rgb_means[0][1]:.6f} B: {unaltered_rgb_means[0][2]:.6f}")
print(
f" Numpy RGB means: R: {unaltered_rgb_means_numpy[0]:.6f} G: {unaltered_rgb_means_numpy[1]:.6f} B: {unaltered_rgb_means_numpy[2]:.6f}")
print()
print()
@Ray9T
Copy link

Ray9T commented Jun 18, 2024

This is a Fake code! this is not PCA

@atadams
Copy link
Author

atadams commented Jun 18, 2024

This is a Fake code! this is not PCA

Fake comment. The code calls OpenCV’s PCACompute2.

@Ray9T
Copy link

Ray9T commented Jun 20, 2024

Fake comment? I'm telling you this is not how PCA code is written. Get some basic education and ethics before dismissing feedback.

@atadams
Copy link
Author

atadams commented Jun 20, 2024

Fake comment? I'm telling you this is not how PCA code is written. Get some basic education and ethics before dismissing feedback.

Thanks for the feedback. I ran out and got some basic education and ethics. The education taught me this is how PCA code is written. The ethics taught me that harassing and defaming people is bad.

@Ray9T
Copy link

Ray9T commented Jun 21, 2024

Get your trashy code reviewed by a decent capable person, if they're willing that is.
Creating photoshop images and calling them real based on a nice story exactly what defames a person, if they had any shame, they'd know better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment