微信朋友图九宫格代码,图案还有圆形。
可以拖动缩放,九宫格和圆形九宫格
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QFileDialog, QMessageBox
from PyQt5.QtGui import QPixmap, QPainter, QPen, QImage
from PyQt5.QtCore import Qt, QPoint, QRect
from datetime import datetime
class ImageSplitter(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.image = None
self.image_rect = None
self.drag_start = None
self.scale = 1.0
def initUI(self):
self.setWindowTitle('微信朋友圈九宫格图片分割工具by林林柒')
self.setGeometry(100, 100, 650, 700)
self.image_frame = QLabel(self)
self.image_frame.setFixedSize(600, 600)
self.image_frame.setAlignment(Qt.AlignCenter)
self.image_frame.setStyleSheet("border: 1px solid black;")
self.info_label = QLabel(self)
self.info_label.setAlignment(Qt.AlignCenter)
self.note_label = QLabel("备注:可以拖入图片或导入图片,可以拖动和鼠标滚轮缩放进行分割", self)
self.note_label.setAlignment(Qt.AlignCenter)
import_button = QPushButton('导入图片', self)
import_button.clicked.connect(self.importImage)
split_button = QPushButton('分割图片', self)
split_button.clicked.connect(self.splitImage)
button_layout = QHBoxLayout()
button_layout.addWidget(import_button)
button_layout.addWidget(split_button)
layout = QVBoxLayout()
layout.addWidget(self.image_frame)
layout.addWidget(self.info_label)
layout.addWidget(self.note_label)
layout.addLayout(button_layout)
self.setLayout(layout)
self.setAcceptDrops(True)
def importImage(self):
file_name, _ = QFileDialog.getOpenFileName(self, "选择图片", "", "图片文件 (*.png *.jpg *.bmp)")
if file_name:
self.loadImage(file_name)
def loadImage(self, file_name):
self.image = QImage(file_name)
if self.image.width() < 400 or self.image.height() < 400:
self.info_label.setText("图片大小不能小于400*400")
return
self.scale = min(600 / self.image.width(), 600 / self.image.height())
self.image_rect = QRect(0, 0, self.image.width() * self.scale, self.image.height() * self.scale)
self.updateImage()
self.info_label.setText(f"图片尺寸: {self.image.width()} x {self.image.height()}")
def updateImage(self):
if self.image:
scaled_image = self.image.scaled(self.image_rect.width(), self.image_rect.height(),
Qt.KeepAspectRatio, Qt.SmoothTransformation)
pixmap = QPixmap(600, 600)
pixmap.fill(Qt.white)
painter = QPainter(pixmap)
painter.drawImage(self.image_rect, scaled_image)
self.drawGrid(painter)
painter.end()
self.image_frame.setPixmap(pixmap)
def drawGrid(self, painter):
pen = QPen(Qt.red, 2, Qt.SolidLine)
painter.setPen(pen)
w, h = 600, 600
painter.drawLine(w/3, 0, w/3, h)
painter.drawLine(2*w/3, 0, 2*w/3, h)
painter.drawLine(0, h/3, w, h/3)
painter.drawLine(0, 2*h/3, w, 2*h/3)
def splitImage(self):
if self.image:
save_dir = QFileDialog.getExistingDirectory(self, "选择保存位置", "")
if save_dir:
frame_rect = self.image_frame.rect()
visible_rect = self.image_rect.intersected(frame_rect)
# 计算可见区域在原图中的位置和大小
orig_x = max(0, int((visible_rect.x() - self.image_rect.x()) / self.scale))
orig_y = max(0, int((visible_rect.y() - self.image_rect.y()) / self.scale))
orig_w = min(self.image.width() - orig_x, int(visible_rect.width() / self.scale))
orig_h = min(self.image.height() - orig_y, int(visible_rect.height() / self.scale))
# 生成文件名前缀
date_prefix = datetime.now().strftime("%Y%m%d%H%M%S")
for j in range(3):# 先遍历行
for i in range(3):# 再遍历列
x = orig_x + int(i * orig_w / 3)
y = orig_y + int(j * orig_h / 3)
w = int(orig_w / 3)
h = int(orig_h / 3)
cropped = self.image.copy(x, y, w, h)
file_path = f'{save_dir}/{date_prefix}_{j*3+i+1:02d}.png'
cropped.save(file_path)
QMessageBox.information(self, "完成", f"图片已分割并保存到 {save_dir}")
else:
print("取消保存")
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
files =
if files:
self.loadImage(files)
def mousePressEvent(self, event):
if self.image and self.image_frame.geometry().contains(event.pos()):
self.drag_start = event.pos()
def mouseMoveEvent(self, event):
if self.drag_start and self.image:
delta = event.pos() - self.drag_start
self.image_rect.translate(delta)
self.drag_start = event.pos()
self.updateImage()
def mouseReleaseEvent(self, event):
self.drag_start = None
def wheelEvent(self, event):
if self.image:
delta = event.angleDelta().y()
if delta > 0:
self.scale *= 1.1
else:
self.scale /= 1.1
self.image_rect.setWidth(int(self.image.width() * self.scale))
self.image_rect.setHeight(int(self.image.height() * self.scale))
self.updateImage()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = ImageSplitter()
ex.show()
sys.exit(app.exec_())
圆形
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QFileDialog, QMessageBox
from PyQt5.QtGui import QPixmap, QPainter, QPen, QImage, QColor, QBrush
from PyQt5.QtCore import Qt, QPoint, QRect
from datetime import datetime
class ImageSplitter(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.image = None
self.image_rect = None
self.drag_start = None
self.scale = 1.0
def initUI(self):
self.setWindowTitle('微信朋友圈九宫格图片分割工具by林林柒')
self.setGeometry(100, 100, 650, 700)
self.image_frame = QLabel(self)
self.image_frame.setFixedSize(600, 600)
self.image_frame.setAlignment(Qt.AlignCenter)
self.image_frame.setStyleSheet("border: 1px solid black;")
self.info_label = QLabel(self)
self.info_label.setAlignment(Qt.AlignCenter)
self.note_label = QLabel("备注:可以拖入图片或导入图片,可以拖动和鼠标滚轮缩放进行分割", self)
self.note_label.setAlignment(Qt.AlignCenter)
import_button = QPushButton('导入图片', self)
import_button.clicked.connect(self.importImage)
split_button = QPushButton('分割图片', self)
split_button.clicked.connect(self.splitImage)
button_layout = QHBoxLayout()
button_layout.addWidget(import_button)
button_layout.addWidget(split_button)
layout = QVBoxLayout()
layout.addWidget(self.image_frame)
layout.addWidget(self.info_label)
layout.addWidget(self.note_label)
layout.addLayout(button_layout)
self.setLayout(layout)
self.setAcceptDrops(True)
def importImage(self):
file_name, _ = QFileDialog.getOpenFileName(self, "选择图片", "", "图片文件 (*.png *.jpg *.bmp)")
if file_name:
self.loadImage(file_name)
def loadImage(self, file_name):
self.image = QImage(file_name)
if self.image.width() < 400 or self.image.height() < 400:
self.info_label.setText("图片大小不能小于400*400")
return
self.scale = min(600 / self.image.width(), 600 / self.image.height())
self.image_rect = QRect(0, 0, self.image.width() * self.scale, self.image.height() * self.scale)
self.updateImage()
self.info_label.setText(f"图片尺寸: {self.image.width()} x {self.image.height()}")
def updateImage(self):
if self.image:
scaled_image = self.image.scaled(self.image_rect.width(), self.image_rect.height(),
Qt.KeepAspectRatio, Qt.SmoothTransformation)
pixmap = QPixmap(600, 600)
pixmap.fill(Qt.white)
painter = QPainter(pixmap)
painter.drawImage(self.image_rect, scaled_image)
self.drawGrid(painter)
painter.end()
self.image_frame.setPixmap(pixmap)
def drawGrid(self, painter):
w, h = 600, 600
# 绘制圆形
pen = QPen(Qt.blue, 4, Qt.SolidLine)
painter.setPen(pen)
painter.setBrush(Qt.transparent)
painter.drawEllipse(0, 0, w, h)
# 绘制网格线
pen = QPen(Qt.red, 2, Qt.SolidLine)
painter.setPen(pen)
painter.drawLine(w/3, 0, w/3, h)
painter.drawLine(2*w/3, 0, 2*w/3, h)
painter.drawLine(0, h/3, w, h/3)
painter.drawLine(0, 2*h/3, w, 2*h/3)
# 添加半透明遮罩
mask = QImage(w, h, QImage.Format_ARGB32_Premultiplied)
mask.fill(Qt.transparent)
mask_painter = QPainter(mask)
mask_painter.setBrush(QColor(255, 255, 255, 128))
mask_painter.setPen(Qt.NoPen)
mask_painter.drawRect(0, 0, w, h)
mask_painter.setCompositionMode(QPainter.CompositionMode_DestinationOut)
mask_painter.setBrush(Qt.black)
mask_painter.drawEllipse(0, 0, w, h)
mask_painter.end()
painter.drawImage(0, 0, mask)
def splitImage(self):
if self.image:
save_dir = QFileDialog.getExistingDirectory(self, "选择保存位置", "")
if save_dir:
frame_rect = self.image_frame.rect()
visible_rect = self.image_rect.intersected(frame_rect)
# 计算可见区域在原图中的位置和大小
orig_x = max(0, int((visible_rect.x() - self.image_rect.x()) / self.scale))
orig_y = max(0, int((visible_rect.y() - self.image_rect.y()) / self.scale))
orig_w = min(self.image.width() - orig_x, int(visible_rect.width() / self.scale))
orig_h = min(self.image.height() - orig_y, int(visible_rect.height() / self.scale))
# 生成文件名前缀
date_prefix = datetime.now().strftime("%Y%m%d%H%M%S")
# 创建圆形遮罩
mask = QImage(orig_w, orig_h, QImage.Format_ARGB32_Premultiplied)
mask.fill(Qt.transparent)
mask_painter = QPainter(mask)
mask_painter.setBrush(Qt.white)
mask_painter.setPen(Qt.NoPen)
mask_painter.drawEllipse(0, 0, orig_w, orig_h)
mask_painter.end()
# 应用遮罩到整个图像,并用白色填充透明部分
result = QImage(orig_w, orig_h, QImage.Format_RGB32)
result.fill(Qt.white)
result_painter = QPainter(result)
result_painter.setCompositionMode(QPainter.CompositionMode_SourceOver)
result_painter.drawImage(0, 0, self.image.copy(orig_x, orig_y, orig_w, orig_h))
result_painter.setCompositionMode(QPainter.CompositionMode_DestinationIn)
result_painter.drawImage(0, 0, mask)
result_painter.setCompositionMode(QPainter.CompositionMode_DestinationOver)
result_painter.fillRect(result.rect(), Qt.white)
result_painter.end()
# 分割圆形图像
for j in range(3):
for i in range(3):
x = int(i * orig_w / 3)
y = int(j * orig_h / 3)
w = int(orig_w / 3)
h = int(orig_h / 3)
cropped = result.copy(x, y, w, h)
file_path = f'{save_dir}/{date_prefix}_{j*3+i+1:02d}.jpg'
cropped.save(file_path, 'JPEG', 100)
QMessageBox.information(self, "完成", f"圆形图片已分割并保存到 {save_dir}")
else:
print("取消保存")
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
files =
if files:
self.loadImage(files)
def mousePressEvent(self, event):
if self.image and self.image_frame.geometry().contains(event.pos()):
self.drag_start = event.pos()
def mouseMoveEvent(self, event):
if self.drag_start and self.image:
delta = event.pos() - self.drag_start
self.image_rect.translate(delta)
self.drag_start = event.pos()
self.updateImage()
def mouseReleaseEvent(self, event):
self.drag_start = None
def wheelEvent(self, event):
if self.image:
delta = event.angleDelta().y()
if delta > 0:
self.scale *= 1.1
else:
self.scale /= 1.1
self.image_rect.setWidth(int(self.image.width() * self.scale))
self.image_rect.setHeight(int(self.image.height() * self.scale))
self.updateImage()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = ImageSplitter()
ex.show()
sys.exit(app.exec_())
本帖最后由 康哥 于 2024-10-16 21:47 编辑
直接运行会报错
报错内容是PS H:\py> python .\9.py
Traceback (most recent call last):
File "H:\py\9.py", line 54, in importImage
self.loadImage(file_name)
File "H:\py\9.py", line 62, in loadImage
self.image_rect = QRect(0, 0, self.image.width() * self.scale, self.image.height() * self.scale)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: arguments did not match any overloaded call:
QRect(): too many arguments
QRect(aleft: int, atop: int, awidth: int, aheight: int): argument 3 has unexpected type 'float'
QRect(atopLeft: QPoint, abottomRight: QPoint): argument 1 has unexpected type 'int'
QRect(atopLeft: QPoint, asize: QSize): argument 1 has unexpected type 'int'
QRect(a0: QRect): argument 1 has unexpected type 'int'
把代码给ai修了一下,报错解决了
成品打包:
https://kang.lanzouo.com/i7b4V2cnivhe
*
http://cdn.kggzs.cn/BlackMythWukong/9.exe
import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QFileDialog, QMessageBox
from PyQt5.QtGui import QPixmap, QPainter, QPen, QImage
from PyQt5.QtCore import Qt, QPoint, QRect
from datetime import datetime
class ImageSplitter(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.image = None
self.image_rect = None
self.drag_start = None
self.scale = 1.0
def initUI(self):
self.setWindowTitle('微信朋友圈九宫格图片分割工具by林林柒')
self.setGeometry(100, 100, 650, 700)
self.image_frame = QLabel(self)
self.image_frame.setFixedSize(600, 600)
self.image_frame.setAlignment(Qt.AlignCenter)
self.image_frame.setStyleSheet("border: 1px solid black;")
self.info_label = QLabel(self)
self.info_label.setAlignment(Qt.AlignCenter)
self.note_label = QLabel("备注:可以拖入图片或导入图片,可以拖动和鼠标滚轮缩放进行分割", self)
self.note_label.setAlignment(Qt.AlignCenter)
import_button = QPushButton('导入图片', self)
import_button.clicked.connect(self.importImage)
split_button = QPushButton('分割图片', self)
split_button.clicked.connect(self.splitImage)
button_layout = QHBoxLayout()
button_layout.addWidget(import_button)
button_layout.addWidget(split_button)
layout = QVBoxLayout()
layout.addWidget(self.image_frame)
layout.addWidget(self.info_label)
layout.addWidget(self.note_label)
layout.addLayout(button_layout)
self.setLayout(layout)
self.setAcceptDrops(True)
def importImage(self):
file_name, _ = QFileDialog.getOpenFileName(self, "选择图片", "", "图片文件 (*.png *.jpg *.bmp)")
if file_name:
self.loadImage(file_name)
def loadImage(self, file_name):
self.image = QImage(file_name)
if self.image.width() < 400 or self.image.height() < 400:
self.info_label.setText("图片大小不能小于400*400")
return
self.scale = min(600 / self.image.width(), 600 / self.image.height())
self.image_rect = QRect(0, 0, int(self.image.width() * self.scale), int(self.image.height() * self.scale))
self.updateImage()
self.info_label.setText(f"图片尺寸: {self.image.width()} x {self.image.height()}")
def updateImage(self):
if self.image:
scaled_image = self.image.scaled(self.image_rect.width(), self.image_rect.height(),
Qt.KeepAspectRatio, Qt.SmoothTransformation)
pixmap = QPixmap(600, 600)
pixmap.fill(Qt.white)
painter = QPainter(pixmap)
painter.drawImage(self.image_rect, scaled_image)
self.drawGrid(painter)
painter.end()
self.image_frame.setPixmap(pixmap)
def drawGrid(self, painter):
pen = QPen(Qt.red, 2, Qt.SolidLine)
painter.setPen(pen)
w, h = 600, 600
painter.drawLine(w // 3, 0, w // 3, h)
painter.drawLine(2 * w // 3, 0, 2 * w // 3, h)
painter.drawLine(0, h // 3, w, h // 3)
painter.drawLine(0, 2 * h // 3, w, 2 * h // 3)
def splitImage(self):
if self.image:
save_dir = QFileDialog.getExistingDirectory(self, "选择保存位置", "")
if save_dir:
frame_rect = self.image_frame.rect()
visible_rect = self.image_rect.intersected(frame_rect)
# 计算可见区域在原图中的位置和大小
orig_x = max(0, int((visible_rect.x() - self.image_rect.x()) / self.scale))
orig_y = max(0, int((visible_rect.y() - self.image_rect.y()) / self.scale))
orig_w = min(self.image.width() - orig_x, int(visible_rect.width() / self.scale))
orig_h = min(self.image.height() - orig_y, int(visible_rect.height() / self.scale))
# 生成文件名前缀
date_prefix = datetime.now().strftime("%Y%m%d%H%M%S")
for j in range(3):# 先遍历行
for i in range(3):# 再遍历列
x = orig_x + int(i * orig_w / 3)
y = orig_y + int(j * orig_h / 3)
w = int(orig_w / 3)
h = int(orig_h / 3)
cropped = self.image.copy(x, y, w, h)
file_path = f'{save_dir}/{date_prefix}_{j*3+i+1:02d}.png'
cropped.save(file_path)
QMessageBox.information(self, "完成", f"图片已分割并保存到 {save_dir}")
else:
print("取消保存")
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
files =
if files:
self.loadImage(files)
def mousePressEvent(self, event):
if self.image and self.image_frame.geometry().contains(event.pos()):
self.drag_start = event.pos()
def mouseMoveEvent(self, event):
if self.drag_start and self.image:
delta = event.pos() - self.drag_start
self.image_rect.translate(delta)
self.drag_start = event.pos()
self.updateImage()
def mouseReleaseEvent(self, event):
self.drag_start = None
def wheelEvent(self, event):
if self.image:
delta = event.angleDelta().y()
if delta > 0:
self.scale *= 1.1
else:
self.scale /= 1.1
self.image_rect.setWidth(int(self.image.width() * self.scale))
self.image_rect.setHeight(int(self.image.height() * self.scale))
self.updateImage()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = ImageSplitter()
ex.show()
sys.exit(app.exec_()) 本帖最后由 kexing 于 2024-10-16 21:40 编辑
Liyang001013 发表于 2024-10-16 08:05
有成品吗?不会用
感谢分享 已收藏 打包成品分享到阿里云
https://www.alipan.com/s/jDiQ5xnsim5
有成品吗?不会用{:1_923:} 康哥 发表于 2024-10-16 09:00
直接运行会报错
报错内容是PS H:\py> python .\9.py
Traceback (most recen ...
直点两个链接连在一起出错,显示“https://kang.lanzouo.com/i7b4V2cnivhehttp://cdn.kggzs.cn/BlackMythWukong/9.exe”,分隔下可以下载。
地址一:https://kang.lanzouo.com/i7b4V2cnivhe
地址二:http://cdn.kggzs.cn/BlackMythWukong/9.exe 感谢分享。 我在py调试器里运行提示:
C:\Users\Administrator\AppData\Local\Programs\Python\Python312\python.exe C:\Users\Administrator\Desktop\圆形.py
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\圆形.py", line 2, in <module>
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QFileDialog, \
ModuleNotFoundError: No module named 'PyQt5'
进程已结束,退出代码为 1
原来分割图片还能这么玩,学习了。 原来是一张图切出来的,每次看到九宫格都会想怎么有这么凑巧的图片{:1_896:} 学习了,超级赞 需要一个成品最好啊 新手就是很难,让我再学一下 这个有点意思,赞一个