Skip to content

Commit

Permalink
perf: Optimize the brightness contrast adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
PairZhu authored Apr 10, 2024
1 parent c574121 commit bb7588c
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions anylabeling/views/labeling/widgets/brightness_contrast_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import PIL.Image
import PIL.ImageEnhance
from PyQt5 import QtGui, QtWidgets
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt

from .. import utils
from PyQt5.QtGui import QImage


class BrightnessContrastDialog(QtWidgets.QDialog):
Expand All @@ -17,28 +16,54 @@ def __init__(self, img, callback, parent=None):
self.setWindowTitle(self.tr("Brightness/Contrast"))

self.slider_brightness = self._create_slider()
brightness_label = QtWidgets.QLabel(
f"{self.slider_brightness.value() / 50:.2f}"
)
self.slider_brightness.valueChanged.connect(
lambda value: brightness_label.setText(f"{value / 50:.2f}")
)
layout = QtWidgets.QHBoxLayout()
layout.addWidget(self.slider_brightness)
layout.addWidget(brightness_label)
brightness_widget = QtWidgets.QWidget()
brightness_widget.setLayout(layout)

self.slider_contrast = self._create_slider()
contrast_label = QtWidgets.QLabel(
f"{self.slider_contrast.value() / 50:.2f}"
)
self.slider_contrast.valueChanged.connect(
lambda value: contrast_label.setText(f"{value / 50:.2f}")
)
layout = QtWidgets.QHBoxLayout()
layout.addWidget(self.slider_contrast)
layout.addWidget(contrast_label)
contrast_widget = QtWidgets.QWidget()
contrast_widget.setLayout(layout)

form_layout = QtWidgets.QFormLayout()
form_layout.addRow(self.tr("Brightness"), self.slider_brightness)
form_layout.addRow(self.tr("Contrast"), self.slider_contrast)
form_layout.addRow(self.tr("Brightness"), brightness_widget)
form_layout.addRow(self.tr("Contrast"), contrast_widget)
self.setLayout(form_layout)

assert isinstance(img, PIL.Image.Image)
self.img = img
self.callback = callback

def on_new_value(self, value):
def on_new_value(self, _):
"""On new value event"""
brightness = self.slider_brightness.value() / 50.0
contrast = self.slider_contrast.value() / 50.0

img = self.img
img = PIL.ImageEnhance.Brightness(img).enhance(brightness)
img = PIL.ImageEnhance.Contrast(img).enhance(contrast)
if brightness != 1:
img = PIL.ImageEnhance.Brightness(img).enhance(brightness)
if contrast != 1:
img = PIL.ImageEnhance.Contrast(img).enhance(contrast)

img_data = utils.img_pil_to_data(img)
qimage = QtGui.QImage.fromData(img_data)
qimage = QImage(
img.tobytes(), img.width, img.height, QImage.Format_RGB888
)
self.callback(qimage)

def _create_slider(self):
Expand Down

0 comments on commit bb7588c

Please sign in to comment.