Skip to main content
added 83 characters in body
Source Link
xioxox
  • 2.7k
  • 1
  • 24
  • 24

PyQt can also be used to draw lines in an image and return this as a numpy array. One advantage over skimage.draw.line_aa is that it allows floating point coordinates.

import numpy as np from PyQt6.QtGui import QPainter, QImage, QColorConstants from PyQt6.QtCore import QPointF xw, yw = 800, 200 img = QImage(xw, yw, QImage.Format.Format_Grayscale8) img.fill(0) paint = QPainter(img) paint.setRenderHint(QPainter.RenderHint.Antialiasing) paint.setPen(QColorConstants.White) paint.drawLine(QPointF(123, 2), QPointF(42, 3)) paint.drawLine(QPointF(4, 3), QPointF(2, 1)) paint.end() xwqt = img.bytesPerLine() # qt may store more than xw for optimisation arr = np.array(img.constBits().asarray(xw*ywxwqt*yw)).reshape((yw,xwxwqt))[:,:xw] 

Note that the centres of the pixels are at positions of 0.5,0.5, so you may need to add an offset. The width of the line can be given by constructing a pen using QPen().

PyQt can also be used to draw lines in an image and return this as a numpy array. One advantage over skimage.draw.line_aa is that it allows floating point coordinates.

import numpy as np from PyQt6.QtGui import QPainter, QImage, QColorConstants from PyQt6.QtCore import QPointF xw, yw = 800, 200 img = QImage(xw, yw, QImage.Format.Format_Grayscale8) img.fill(0) paint = QPainter(img) paint.setRenderHint(QPainter.RenderHint.Antialiasing) paint.setPen(QColorConstants.White) paint.drawLine(QPointF(123, 2), QPointF(42, 3)) paint.drawLine(QPointF(4, 3), QPointF(2, 1)) paint.end() arr = np.array(img.constBits().asarray(xw*yw)).reshape((yw,xw)) 

Note that the centres of the pixels are at positions of 0.5,0.5, so you may need to add an offset. The width of the line can be given by constructing a pen using QPen().

PyQt can also be used to draw lines in an image and return this as a numpy array. One advantage over skimage.draw.line_aa is that it allows floating point coordinates.

import numpy as np from PyQt6.QtGui import QPainter, QImage, QColorConstants from PyQt6.QtCore import QPointF xw, yw = 800, 200 img = QImage(xw, yw, QImage.Format.Format_Grayscale8) img.fill(0) paint = QPainter(img) paint.setRenderHint(QPainter.RenderHint.Antialiasing) paint.setPen(QColorConstants.White) paint.drawLine(QPointF(123, 2), QPointF(42, 3)) paint.drawLine(QPointF(4, 3), QPointF(2, 1)) paint.end() xwqt = img.bytesPerLine() # qt may store more than xw for optimisation arr = np.array(img.constBits().asarray(xwqt*yw)).reshape((yw,xwqt))[:,:xw] 

Note that the centres of the pixels are at positions of 0.5,0.5, so you may need to add an offset. The width of the line can be given by constructing a pen using QPen().

Post Undeleted by xioxox
Post Deleted by xioxox
Source Link
xioxox
  • 2.7k
  • 1
  • 24
  • 24

PyQt can also be used to draw lines in an image and return this as a numpy array. One advantage over skimage.draw.line_aa is that it allows floating point coordinates.

import numpy as np from PyQt6.QtGui import QPainter, QImage, QColorConstants from PyQt6.QtCore import QPointF xw, yw = 800, 200 img = QImage(xw, yw, QImage.Format.Format_Grayscale8) img.fill(0) paint = QPainter(img) paint.setRenderHint(QPainter.RenderHint.Antialiasing) paint.setPen(QColorConstants.White) paint.drawLine(QPointF(123, 2), QPointF(42, 3)) paint.drawLine(QPointF(4, 3), QPointF(2, 1)) paint.end() arr = np.array(img.constBits().asarray(xw*yw)).reshape((yw,xw)) 

Note that the centres of the pixels are at positions of 0.5,0.5, so you may need to add an offset. The width of the line can be given by constructing a pen using QPen().