import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QLineEdit, QPushButton, QVBoxLayout, QWidget, QFileDialog
from PyQt5.QtGui import QPixmap
from PIL import Image, ImageDraw, ImageFont

class WatermarkApp(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setWindowTitle('Watermark App')

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        self.layout = QVBoxLayout(self.central_widget)

        self.text_input = QLineEdit(self)
        self.text_input.setPlaceholderText('Enter text for watermark')
        self.layout.addWidget(self.text_input)

        self.select_image_button = QPushButton('Select Image', self)
        self.select_image_button.clicked.connect(self.select_image)
        self.layout.addWidget(self.select_image_button)

        self.image_label = QLabel(self)
        self.layout.addWidget(self.image_label)

        self.apply_watermark_button = QPushButton('Apply Watermark', self)
        self.apply_watermark_button.clicked.connect(self.apply_watermark)
        self.layout.addWidget(self.apply_watermark_button)

        self.image_path = None

    def select_image(self):
        options = QFileDialog.Options()
        options |= QFileDialog.ReadOnly
        file_name, _ = QFileDialog.getOpenFileName(self, "Select Image", "", "Images (*.png *.xpm *.jpg *.jpeg);;All Files (*)", options=options)
        if file_name:
            self.image_path = file_name
            pixmap = QPixmap(file_name)
            self.image_label.setPixmap(pixmap.scaled(400, 400))

    def apply_watermark(self):
        if not self.image_path:
            return

        text = self.text_input.text()
        if not text:
            return

        image = Image.open(self.image_path).convert('RGBA')
        watermark = Image.new('RGBA', image.size, (255, 255, 255, 0))
        draw = ImageDraw.Draw(watermark)

        # Calculate the font size based on the image size
        font_size = min(image.width, image.height) // 10
        font = ImageFont.truetype("arial.ttf", font_size)

        bbox = draw.textbbox((0, 0), text, font=font)
        text_width = bbox[2] - bbox[0]
        text_height = bbox[3] - bbox[1]

        # Draw the text repeatedly to cover the entire image
        for y in range(0, image.height, text_height):
            for x in range(0, image.width, text_width):
                draw.text((x, y), text, font=font, fill=(255, 255, 255, 10))

        watermark = watermark.rotate(30, expand=True)

        # Ensure the watermark image matches the original image size
        if watermark.size != image.size:
            watermark = watermark.resize(image.size)

        watermarked_image = Image.alpha_composite(image, watermark)

        watermarked_image.show()
        watermarked_image.save('watermarked_image.png')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = WatermarkApp()
    ex.show()
    sys.exit(app.exec_())
