• ckeditor


    from io import BytesIO
    
    from django.core.files.storage import FileSystemStorage
    from django.core.files.uploadedfile import InMemoryUploadedFile
    
    from PIL import Image, ImageDraw, ImageFont
    
    
    class WatermarkStorage(FileSystemStorage):
        def save(self, name, content, max_length=None):
            # 处理逻辑
            if 'image' in content.content_type:
                # 加水印
                image = self.watermark_with_text(content, 'the5fire.com', 'red')
                content = self.convert_image_to_file(image, name)
    
            return super().save(name, content, max_length=max_length)
    
        def convert_image_to_file(self, image, name):
            temp = BytesIO()
            image.save(temp, format='PNG')
            file_size = temp.tell()
            return InMemoryUploadedFile(temp, None, name, 'image/png', file_size, None)
    
        def watermark_with_text(self, file_obj, text, color, fontfamily=None):
            image = Image.open(file_obj).convert('RGBA')
            draw = ImageDraw.Draw(image)
            width, height = image.size
            margin = 10
            if fontfamily:
                font = ImageFont.truetype(fontfamily, int(height / 20))
            else:
                font = None
            textWidth, textHeight = draw.textsize(text, font)
            x = (width - textWidth - margin) / 2  # 计算横轴位置
            y = height - textHeight - margin  # 计算纵轴位置
            draw.text((x, y), text, color, font)
    
            return image
    

      

  • 相关阅读:
    [原]将工程由VC6迁移到VS2005
    [原]DirectDraw视频播放要点
    [原]代码优化学习笔记
    [原]Linux文件交换
    [原]计划
    [原]写在2006年的最后一天
    [原]技术发展规划
    FindBugs的安装和使用
    VirtualBox常用命令
    eclipse中统计代码行数
  • 原文地址:https://www.cnblogs.com/realadmin/p/12650402.html
Copyright © 2020-2023  润新知