gpt4 book ai didi

python - QPixmap(图像)的图 block 具有重叠区域

转载 作者:行者123 更新时间:2023-12-01 00:39:01 25 4
gpt4 key购买 nike

我想创建一个带有图像的按钮网格。每个按钮上的图像都是更大图像的一部分,该图像被切成碎片 - 因此按钮网格可以被视为原始图像的图 block (我用来在按钮上放置图像的代码是 from here )。

为了将图像(image=QPixmap(path_to_image))切成碎片,我在 for 中使用了方法image.copy(x, y, width, height)循环。

起初我认为代码工作正常。更精确的检查显示图像片段有重叠部分。

这是我用 GIF 图像测试的代码:

import os
import sys

from PyQt5.QtCore import QSize
from PyQt5.QtGui import QPixmap, QIcon
from PyQt5.QtWidgets import \
QWidget, QApplication, \
QGridLayout, \
QLabel, QPushButton

def cut_image_into_tiles(image, rows=4, cols=4) -> dict:
if isinstance(image, str) and os.path.exists(image):
image = QPixmap(image)
elif not isinstance(image, QPixmap):
raise ValueError("image must be str or QPixmap object")

# Dim of the tile
tw = int(image.size().width() / cols)
th = int(image.size().height() / rows)

# prepare return value
tiles = {"width": tw, "height": th}
for r in range(rows):
for c in range(cols):
tile = image.copy(c * th, r * tw, tw, th)
# args: x, y, width, height
# https://doc.qt.io/qt-5/qpixmap.html#copy-1
tiles[(r, c)] = tile

return tiles


def create_pixmapbutton(pixmap, width=0, height=0) -> QPushButton:
if isinstance(pixmap, QPixmap):
button = QPushButton()
if width > 0 and height > 0:
button.setIconSize(QSize(width, height))
else:
button.setIconSize(pixmap.size())
button.setIcon(QIcon(pixmap))
return button


def run_viewer(imagepath, rows=4, cols=4):

# ImageCutter.run_viewer(imagepath)
app = QApplication(sys.argv)

image = QPixmap(imagepath)

tiles = cut_image_into_tiles(image=image, rows=rows, cols=cols)
tilewidth = tiles["width"]
tileheight = tiles["height"]
# get dict tiles (keys:=(row, col) or width or height)

viewer = QWidget()
layout = QGridLayout()
viewer.setLayout(layout)
viewer.setWindowTitle("ImageCutter Viewer")

lbl = QLabel()
lbl.setPixmap(image)
layout.addWidget(lbl, 0, 0, rows, cols)

for r in range(rows):
for c in range(cols):
btn = create_pixmapbutton(
tiles[r, c], width=tilewidth, height=tileheight)

btninfo = "buttonsize={}x{}".format(tilewidth, tileheight)
btn.setToolTip(btninfo)
# logger.debug(" create button [{}]".format(btninfo))
layout.addWidget(btn, r, cols + c)

viewer.show()
sys.exit(app.exec_())

我使用 run_viewer(path_to_a_gif_image, rows=3, cols=3) 测试了代码

最佳答案

每个网格的宽度必须取决于图像的宽度,在您的情况下,它取决于 th,但是 th 取决于高度,这是不正确的,您必须将 th 更改为 tw,高度也相同。

考虑到上述情况,解决方案是:

import os
import sys

from PyQt5.QtCore import QSize
from PyQt5.QtGui import QPixmap, QIcon
from PyQt5.QtWidgets import QWidget, QApplication, QGridLayout, QLabel, QPushButton


def cut_image_into_tiles(image, rows=4, cols=4) -> dict:
if isinstance(image, str) and os.path.exists(image):
image = QPixmap(image)
elif not isinstance(image, QPixmap):
raise ValueError("image must be str or QPixmap object")

# Dim of the tile
tw = int(image.size().width() / cols)
th = int(image.size().height() / rows)

# prepare return value
tiles = {"width": tw, "height": th}
for r in range(rows):
for c in range(cols):
tile = image.copy(c * tw, r * th, tw, th) # <----
# args: x, y, width, height
# https://doc.qt.io/qt-5/qpixmap.html#copy-1
tiles[(r, c)] = tile

return tiles


def create_pixmapbutton(pixmap, width=0, height=0) -> QPushButton:
if isinstance(pixmap, QPixmap):
button = QPushButton()
if width > 0 and height > 0:
button.setIconSize(QSize(width, height))
else:
button.setIconSize(pixmap.size())
button.setIcon(QIcon(pixmap))
button.setContentsMargins(0, 0, 0, 0)
button.setFixedSize(button.iconSize())
return button


def run_viewer(imagepath, rows=4, cols=4):

# ImageCutter.run_viewer(imagepath)
app = QApplication(sys.argv)

image = QPixmap(imagepath)

tiles = cut_image_into_tiles(image=image, rows=rows, cols=cols)
tilewidth = tiles["width"]
tileheight = tiles["height"]
# get dict tiles (keys:=(row, col) or width or height)

viewer = QWidget()
layout = QGridLayout(viewer)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
viewer.setWindowTitle("ImageCutter Viewer")

lbl = QLabel()
lbl.setPixmap(image)
layout.addWidget(lbl, 0, 0, rows, cols)

for r in range(rows):
for c in range(cols):
btn = create_pixmapbutton(tiles[r, c], width=tilewidth, height=tileheight)

btninfo = "buttonsize={}x{}".format(tilewidth, tileheight)
btn.setToolTip(btninfo)
# logger.debug(" create button [{}]".format(btninfo))
layout.addWidget(btn, r, cols + c)

viewer.show()
viewer.setFixedSize(viewer.sizeHint())
sys.exit(app.exec_())


run_viewer("linux.gif", 3, 3)

关于python - QPixmap(图像)的图 block 具有重叠区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57501497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com