pyqt如何修复“QImage:内存不足,返回空图像”

2024-06-26 15:02:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些Python代码,我在那里画圆和线。。。一台机器,它返回一条关于内存不足的奇怪错误消息。。。但从任务管理器的角度来看,系统并不是内存不足。我在其他地方读到,可能没有足够的连续内存用于分配(内存是碎片)。使用QImage.Format_RGB16检查大约16000平方米的图像尺寸,在压缩之前将生成512MB的图像(之后大约是1MB)。我想这可能是个问题。。。但我能做什么呢?不知怎么把公羊拆了?让Python/pyqt使用RAMDISK/swap?为什么操作系统(Windows)不能处理交换并使RAM块可用?在

注:这是在做鼻子测试时发生的。。。所以多个测试可能正在调用这个代码。。。我想知道垃圾回收器是否在测试之间快速删除前一个映像?在

class QtDrawingTool(object):
    def __init__(self):
        self.newImage = None
        self.qpainter = None
        self.qt_max_edge_dimension = 32767.0

    def get_max_dimension(self):
        return self.qt_max_edge_dimension

    def get_color(self, color_wanted):
        if color_wanted == 'white':
            color = QColor(qRgb(255, 255, 255))
        elif color_wanted == 'black':
            color = QColor(qRgb(0, 0, 0))
        elif color_wanted == 'blue':
            color = QColor(qRgb(255, 0, 0))
        elif color_wanted == 'green':
            color = QColor(qRgb(0, 255, 0))
        elif color_wanted == 'red':
            color = QColor(qRgb(255, 0, 0))
        elif color_wanted == 'yellow':
            color = Qt.yellow
        elif color_wanted == 'orange':
            color = QColor(qRgb(250, 158, 37))
        elif isinstance(color_wanted, tuple):
            color = QColor(qRgb(*color_wanted))
        else:
            raise Exception('color not available: {}'.format(color_wanted))
        return color

    def create_image(self, w, h):
        self.newImage = QImage(QSize(w, h),
                               QImage.Format_RGB16)
        assert(not self.newImage.isNull())

        # fill image with black
        self.newImage.fill(self.get_color('black'))

        # get a painter attached to our image data object
        self.qpainter = QPainter(self.newImage)

    def get_output_scaling_factor(self, width, height):
        # get a multiplier that will give our coordinates the most resolution relative to QT's maximum image resolution
        # return min(self.qt_max_edge_dimension/width, self.qt_max_edge_dimension/height)
        return 1.0

    def set_color_and_width(self, color, line_width):
        _color = self.get_color(color)
        the_pen = QPen(_color,
                       line_width,
                       Qt.SolidLine,
                       Qt.RoundCap,
                       Qt.RoundJoin)

        # the_brush = self.qpainter.brush()
        # the_brush.setColor(_color)
        the_brush = QBrush(_color)

        self.qpainter.setPen(the_pen)
        self.qpainter.setBrush(the_brush)

    def draw_line(self, src_x, src_y, tgt_x, tgt_y, line_width, color):
        # brush -- defines the color or pattern that is used for filling shapes.
        # pen   -- defines the color or stipple that is used for drawing lines or boundaries.
        self.set_color_and_width(color, line_width)
        self.qpainter.drawLine(src_x, src_y, tgt_x, tgt_y)

    def draw_circle(self, x, y, radius, line_width, color):
        self.set_color_and_width(color, line_width)
        self.qpainter.drawEllipse(int(x), int(y), radius*2, radius*2)

    def save_image(self, save_path, image_to_save=None):
        if image_to_save is None:
            image_to_save = self.newImage
        return image_to_save.save(save_path)

    @property
    def image_data(self):
        return self.newImage

    def get_flipped_along_y_axis(self):
        return self.newImage.transformed(QTransform().scale(-1, 1))

    def get_flipped_along_x_axis(self):
        return self.newImage.transformed(QTransform().scale(1, -1))

    def __del__(self):
        del self.qpainter

下面是另一个类的代码,我在这里创建图像,并绘制一些图形:

^{pr2}$

Tags: theimageselfgetreturnsavedefwidth