1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Create glowing edges Photoshop filter using open-cv

Discussão em 'Python' iniciado por Stack, Setembro 17, 2025.

  1. Stack

    Stack Membro Participativo

    Does anyone know how to recreate the Photoshop filter called Glowing Edges using python/open-cv? This should be a bunch of filters one after another, maybe someone already made it.

    I have tried to recreate the same filter and it looks like below, but still too noisy and the result is similar, but not as it is in photoshop: on the left is what I expected to get (Photoshop glowing edges filter with settings: edge width 4, brightness 4, smoothness 15) and on the right is what I could reconstruct in my filter. The Photoshop filter lines look smooth and solid, while mine are noisy and the overall image has too much dotted noise.

    [​IMG]

    import sys
    from PyQt6.QtWidgets import (
    QApplication, QMainWindow, QLabel, QPushButton,
    QVBoxLayout, QFileDialog, QWidget
    )
    from PyQt6.QtGui import QPixmap, QImage
    from PyQt6.QtCore import Qt
    from PIL import Image
    import cv2
    import numpy as np


    class EdgeFilter(QMainWindow):
    def __init__(self):
    super().__init__()
    self.setWindowTitle("Filter")
    self.setGeometry(100, 100, 600, 400)
    self.original_image = None
    self.filtered_image = None
    self.init_ui()

    def init_ui(self):
    central_widget = QWidget()
    self.setCentralWidget(central_widget)
    layout = QVBoxLayout(central_widget)
    self.btn_open = QPushButton("Open img")
    self.btn_open.clicked.connect(self.open_image)
    layout.addWidget(self.btn_open)
    self.filtered_label = QLabel("Filter")
    self.filtered_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
    self.filtered_label.setMinimumSize(400, 300)
    self.filtered_label.setStyleSheet("border: 1px solid gray;")
    layout.addWidget(self.filtered_label)

    ############## FILTER DOWN HERE \/ \/ \/ \/ \/ #####################
    def apply_filter(self):
    img = self._ensure_gray_numpy(self.original_image)

    scales = [2.0, 3.0, 4.0]
    edges = np.zeros_like(img, dtype=np.float64)

    for scale in scales:
    size = int(12 * scale + 1)
    if size % 2 == 0: size += 1
    blur = cv2.GaussianBlur(img, (size, size), scale)

    gx = cv2.Sobel(blur, cv2.CV_64F, 1, 0, ksize=3)
    gy = cv2.Sobel(blur, cv2.CV_64F, 0, 1, ksize=3)
    mag = np.sqrt(gx ** 2 + gy ** 2)
    edges = np.maximum(edges, mag)

    edges = np.where(edges > edges.mean() * 1.7, 200, 0)

    self.filtered_image = Image.fromarray(edges.astype(np.uint8))
    self.display_image(self.filtered_image)

    def open_image(self):
    file_path, _ = QFileDialog.getOpenFileName(
    self, "Open img", "", "Images (*.png *.jpg *.jpeg)"
    )
    if file_path:
    self.original_image = Image.open(file_path).convert('L')
    self.apply_filter()

    def _ensure_gray_numpy(self, image):
    if image is None:
    return np.zeros((1, 1), dtype=np.uint8)
    if isinstance(image, np.ndarray):
    arr = image
    if arr.ndim == 3:
    try:
    arr = cv2.cvtColor(arr, cv2.COLOR_RGB2GRAY)
    except Exception:
    try:
    arr = cv2.cvtColor(arr, cv2.COLOR_BGR2GRAY)
    except Exception:
    arr = np.mean(arr, axis=2).astype(np.uint8)
    return arr.astype(np.uint8)

    pil = image
    if pil.mode != "L":
    pil = pil.convert("L")
    return np.array(pil, dtype=np.uint8)

    def display_image(self, image):
    if image is None:
    self.filtered_label.clear()
    return
    mode = image.mode
    w, h = image.width, image.height
    if mode == "L":
    data = image.tobytes()
    bytes_per_line = w
    qimg = QImage(data, w, h, bytes_per_line, QImage.Format.Format_Grayscale8)
    else:
    pil_L = image.convert("L")
    data = pil_L.tobytes()
    bytes_per_line = w
    qimg = QImage(data, w, h, bytes_per_line, QImage.Format.Format_Grayscale8)

    pixmap = QPixmap.fromImage(qimg)
    scaled_pixmap = pixmap.scaled(
    self.filtered_label.size(),
    Qt.AspectRatioMode.KeepAspectRatio,
    Qt.TransformationMode.SmoothTransformation
    )
    self.filtered_label.setPixmap(scaled_pixmap)

    if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = EdgeFilter()
    window.show()
    sys.exit(app.exec())

    Continue reading...

Compartilhe esta Página