import sys
import os
from PIL import Image, ImageQt
from PyQt5 import QtWidgets, QtGui, QtCore
import pandas as pd
class ImageSectorApp(QtWidgets.QWidget):
def __init__(self, image_path, rows=16, cols=16):
super().__init__()
self.image_path = image_path
self.rows = rows
self.cols = cols
self.sectors = []
self.selected_sector = None
self.init_ui()
self.load_image_sectors()
def init_ui(self):
self.setWindowTitle("Image Sector Viewer")
self.setGeometry(100, 100, 800, 600)
# Layouts
self.layout = QtWidgets.QVBoxLayout(self)
self.image_layout = QtWidgets.QGridLayout()
self.command_layout = QtWidgets.QHBoxLayout()
# Image Display Area
self.image_area = QtWidgets.QScrollArea()
self.image_area.setWidgetResizable(True)
self.image_area.setWidget(QtWidgets.QWidget())
self.image_area.widget().setLayout(self.image_layout)
self.layout.addWidget(self.image_area)
# Command Panel
self.label_button = QtWidgets.QPushButton("Label Sector")
self.label_button.clicked.connect(self.label_sector)
self.save_button = QtWidgets.QPushButton("Save Sector Info")
self.save_button.clicked.connect(self.save_sector_info)
self.command_layout.addWidget(self.label_button)
self.command_layout.addWidget(self.save_button)
self.layout.addLayout(self.command_layout)
def load_image_sectors(self):
self.sectors = self.split_image(self.image_path, self.rows, self.cols)
if self.sectors:
self.display_sectors()
def split_image(self, image_path, rows, cols):
try:
img = Image.open(image_path)
except Exception as e:
QtWidgets.QMessageBox.critical(self, "Error", f"Error opening image: {e}")
return []
img_width, img_height = img.size
sector_width = img_width // cols
sector_height = img_height // rows
output_dir = "sectors"
os.makedirs(output_dir, exist_ok=True)
sector_info = []
for row in range(rows):
for col in range(cols):
left = col * sector_width
upper = row * sector_height
right = left + sector_width
lower = upper + sector_height
sector = img.crop((left, upper, right, lower))
sector_filename = f"sector_{row}_{col}.png"
sector.save(os.path.join(output_dir, sector_filename))
sector_info.append({
"sector": sector_filename,
"coordinates": (left, upper, right, lower),
"label": "" # Initialize label as empty
})
return sector_info
def display_sectors(self):
for i, sector in enumerate(self.sectors):
img = Image.open(os.path.join("sectors", sector['sector']))
img_qt = ImageQt.ImageQt(img) # Convert PIL image to ImageQt
qimage = QtGui.QImage(img_qt) # Convert ImageQt to QImage
img_label = QtWidgets.QLabel()
img_label.setPixmap(QtGui.QPixmap.fromImage(qimage))
img_label.mousePressEvent = lambda event, s=sector: self.select_sector(s)
self.image_layout.addWidget(img_label, i // self.cols, i % self.cols)
def select_sector(self, sector):
# Clear previous highlights
self.clear_highlights()
self.selected_sector = sector
self.highlight_sector(sector)
def clear_highlights(self):
for i in range(self.image_layout.count()):
label = self.image_layout.itemAt(i).widget()
label.setStyleSheet("") # Reset style
def highlight_sector(self, sector):
index = self.sectors.index(sector)
label = self.image_layout.itemAt(index).widget()
label.setStyleSheet("border: 2px solid red;") # Highlight the selected sector
def label_sector(self):
if self.selected_sector:
label, ok = QtWidgets.QInputDialog.getText(self, "Label Sector", "Enter label for the selected sector:")
if ok and label:
self.selected_sector['label'] = label # Store the label in the sector info
self.update_sector_label(self.selected_sector)
def update_sector_label(self, sector):
index = self.sectors.index(sector)
label_widget = self.image_layout.itemAt(index).widget()
label_widget.setText(sector['label']) # Display label on the sector
def save_sector_info(self):
df = pd.DataFrame(self.sectors)
df.to_csv("sector_info.csv", index=False)
QtWidgets.QMessageBox.information(self, "Success", "Sector information saved successfully.")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
image_path = "1111.png" # Specify the path to your image
window = ImageSectorApp(image_path)
window.show()
sys.exit(app.exec_())
import sys
import os
from PIL import Image, ImageQt
from PyQt5 import QtWidgets, QtGui, QtCore
import pandas as pd
class ImageSectorApp(QtWidgets.QWidget):
def __init__(self, image_path, rows=16, cols=16):
super().__init__()
self.image_path = image_path
self.rows = rows
self.cols = cols
self.sectors = []
self.selected_sector = None
self.init_ui()
self.load_image_sectors()
def init_ui(self):
self.setWindowTitle("Image Sector Viewer")
self.setGeometry(100, 100, 800, 600)
# Layouts
self.layout = QtWidgets.QVBoxLayout(self)
self.image_layout = QtWidgets.QGridLayout()
self.command_layout = QtWidgets.QHBoxLayout()
# Image Display Area
self.image_area = QtWidgets.QScrollArea()
self.image_area.setWidgetResizable(True)
self.image_area.setWidget(QtWidgets.QWidget())
self.image_area.widget().setLayout(self.image_layout)
self.layout.addWidget(self.image_area)
# Command Panel
self.label_button = QtWidgets.QPushButton("Label Sector")
self.label_button.clicked.connect(self.label_sector)
self.save_button = QtWidgets.QPushButton("Save Sector Info")
self.save_button.clicked.connect(self.save_sector_info)
self.command_layout.addWidget(self.label_button)
self.command_layout.addWidget(self.save_button)
self.layout.addLayout(self.command_layout)
def load_image_sectors(self):
self.sectors = self.split_image(self.image_path, self.rows, self.cols)
if self.sectors:
self.display_sectors()
def split_image(self, image_path, rows, cols):
try:
img = Image.open(image_path)
except Exception as e:
QtWidgets.QMessageBox.critical(self, "Error", f"Error opening image: {e}")
return []
img_width, img_height = img.size
sector_width = img_width // cols
sector_height = img_height // rows
output_dir = "sectors"
os.makedirs(output_dir, exist_ok=True)
sector_info = []
for row in range(rows):
for col in range(cols):
left = col * sector_width
upper = row * sector_height
right = left + sector_width
lower = upper + sector_height
sector = img.crop((left, upper, right, lower))
sector_filename = f"sector_{row}_{col}.png"
sector.save(os.path.join(output_dir, sector_filename))
sector_info.append({
"sector": sector_filename,
"coordinates": (left, upper, right, lower),
"label": "" # Initialize label as empty
})
return sector_info
def display_sectors(self):
for i, sector in enumerate(self.sectors):
img = Image.open(os.path.join("sectors", sector['sector']))
img_qt = ImageQt.ImageQt(img) # Convert PIL image to ImageQt (which is QImage)
pixmap = QtGui.QPixmap.fromImage(img_qt) # Create QPixmap directly from QImage
img_label = QtWidgets.QLabel()
img_label.setPixmap(pixmap)
img_label.mousePressEvent = lambda event, s=sector: self.select_sector(s)
self.image_layout.addWidget(img_label, i // self.cols, i % self.cols)
def select_sector(self, sector):
# Clear previous highlights
self.clear_highlights()
self.selected_sector = sector
self.highlight_sector(sector)
def clear_highlights(self):
for i in range(self.image_layout.count()):
label = self.image_layout.itemAt(i).widget()
label.setStyleSheet("") # Reset style
def highlight_sector(self, sector):
index = self.sectors.index(sector)
label = self.image_layout.itemAt(index).widget()
label.setStyleSheet("border: 2px solid red;") # Highlight the selected sector
def label_sector(self):
if self.selected_sector:
label, ok = QtWidgets.QInputDialog.getText(self, "Label Sector", "Enter label for the selected sector:")
if ok and label:
self.selected_sector['label'] = label # Store the label in the sector info
self.update_sector_label(self.selected_sector)
def update_sector_label(self, sector):
index = self.sectors.index(sector)
label_widget = self.image_layout.itemAt(index).widget()
label_widget.setText(sector['label']) # Display label on the sector
def save_sector_info(self):
df = pd.DataFrame(self.sectors)
df.to_csv("sector_info.csv", index=False)
QtWidgets.QMessageBox.information(self, "Success", "Sector information saved successfully.")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
image_path = "1111.png" # Specify the path to your image
window = ImageSectorApp(image_path)
window.show()
sys.exit(app.exec_())