Skip to content

Commit

Permalink
Enhance pixmap to array
Browse files Browse the repository at this point in the history
  • Loading branch information
prkrtg committed Aug 30, 2024
1 parent 66a2ea7 commit c5c5596
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions histogram.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# -----------------------------------------------------------------------------
# @file histogram.py
# @brief A class for displaying histograms of images with interactive bin count adjustment and image navigation.
# @author Prakriti Gupta <[email protected]>
# -----------------------------------------------------------------------------

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QSlider, QLabel, QPushButton, QHBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QImage

class Histogram(QDialog):
"""
Expand Down Expand Up @@ -86,13 +93,39 @@ def update_histogram(self):
def pixmap_to_array(self, pixmap):
"""
Converts a QPixmap to a numpy array.
Handles both monochrome (grayscale), RGB, RGBA, and multispectral images.
"""
# Convert QPixmap to QImage
image = pixmap.toImage()
width, height = image.width(), image.height()

# Convert to numpy array
ptr = image.bits()
ptr.setsize(height * width * 4) # 4 bytes per pixel (RGBA)
arr = np.array(ptr).reshape((height, width, 4)) # RGBA image
return arr[:, :, 0] # Use the red channel or convert to grayscale if needed
if image.format() == QImage.Format_Grayscale8:
# Monochrome image (8-bit grayscale)
ptr.setsize(width * height) # 1 byte per pixel
arr = np.array(ptr).reshape((height, width))
else:
# RGB or RGBA image (8-bit per channel)
ptr.setsize(width * height * 4) # 4 bytes per pixel (RGBA)
arr = np.array(ptr).reshape((height, width, 4)) # RGBA image

# Handle different formats
if image.format() == QImage.Format_RGB888:
arr = arr[:, :, :3] # Extract RGB channels
elif image.format() == QImage.Format_RGBA8888:
arr = arr[:, :, :3] # Use RGB channels only
else:
# Handle multispectral images
if arr.shape[2] > 3:
# Multispectral image with more than 3 channels
# You can choose how to handle this, e.g., extract specific bands or process all bands
# For demonstration, we will use all bands
pass
else:
raise ValueError("Unsupported image format")

return arr

def show_prev_image(self):
"""
Expand Down

0 comments on commit c5c5596

Please sign in to comment.