Skip to content

Instantly share code, notes, and snippets.

@e96031413
Created August 29, 2023 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e96031413/6534efd59e02daf2c053148a90cb2e74 to your computer and use it in GitHub Desktop.
Save e96031413/6534efd59e02daf2c053148a90cb2e74 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import numpy as np
import rawpy
def pack_raw(raw):
im = raw.raw_image_visible.astype(np.float32)
im = np.expand_dims(im, axis=2)
img_shape = im.shape
H = img_shape[0]
W = img_shape[1]
out = np.concatenate((im[0:H:2, 0:W:2, :],
im[0:H:2, 1:W:2, :],
im[1:H:2, 1:W:2, :],
im[1:H:2, 0:W:2, :]), axis=2)
return out
def main(image_path):
if 'arw' in image_path.lower():
image = rawpy.imread(image_path)
image = pack_raw(image)
elif 'npy' in image_path.lower():
image = np.load(image_path)
channel_stats = []
for channel in range(4):
channel_values = image[:, :, channel]
mean_value = np.mean(channel_values)
max_value = np.max(channel_values)
min_value = np.min(channel_values)
channel_stats.append((mean_value, max_value, min_value))
print(f"Channel {channel}: Mean={mean_value:.2f}, Max={max_value}, Min={min_value}")
plt.figure(figsize=(12, 6))
colors = ['red', 'green', (50/255, 128/255, 10/255), 'blue']
for channel, color in zip(range(4), colors):
plt.hist(image[:, :, channel].ravel(), bins=16353, color=color, alpha=0.7, label=f'Channel {channel}')
xpos = 0.7
ypos = 0.88 - channel * 0.15
plt.annotate(
f'Mean={channel_stats[channel][0]:.2f}\nMax={channel_stats[channel][1]}\nMin={channel_stats[channel][2]}',
xy=(xpos, ypos), xycoords='axes fraction',
bbox=dict(boxstyle='round,pad=0.3', edgecolor=color, facecolor='lightgray'),
fontsize=10, color=color
)
plt.title("Pixel Value Distribution for Each Channel")
plt.xlabel("Pixel Value")
plt.ylabel("Frequency")
plt.legend()
output_file = 'pixel_value_plot.png'
plt.savefig(output_file)
if __name__=="__main__":
# image_path = 'path/to/RAW/Image/file.ARW'
main(image_path=image_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment