многотекстовый водяной знак на изображении PIL django python - PullRequest
0 голосов
/ 30 января 2020

В настоящее время он рисует один текст на миниатюре, и я хочу нарисовать несколько небольших текстовых водяных знаков на миниатюре

models.py:

class Image(models.Model):
      license_type = (
            ('Royalty-Free','Royalty-Free'),
            ('Rights-Managed','Rights-Managed')
          )
      image_number = models.CharField(default=random_image_number,max_length=12,unique=True)
      title = models.CharField(default=random_image_number,max_length = 100)
      image = models.ImageField(upload_to = 'image' , default = 'demo/demo.png')
      thumbnail = models.ImageField(upload_to='thumbs', blank=True, null=True)
      category = models.ForeignKey('Category', null=True, blank=True, on_delete=models.CASCADE)
      shoot = models.ForeignKey(ImageShoot, on_delete=models.CASCADE, related_name='Image', null=True,blank=True)
      image_keyword = models.CharField(max_length=500)
      description = models.TextField(max_length=3000,null=True,blank=True)
      credit = models.CharField(max_length=150, null=True,blank=True)
      location = models.CharField(max_length=100, null=True,blank=True)
      license_type = models.CharField(max_length=20,choices=license_type, default='')
      uploaded_at = models.TimeField(auto_now_add=True)

      def __str__(self):
          return self.title

      def save(self, *args, **kwargs):    
         super(Image, self).save(*args, **kwargs)
         if not self.make_thumbnail():
            raise Exception('Could not create thumbnail - is the file type valid?')

      def make_thumbnail(self):
          fh = storage.open(self.image.path)   
          base = PILImage.open(fh).convert('RGBA')
          base.load()
          width, height = base.size



          txt = PILImage.new('RGBA', base.size, (255,255,255,0))


          fnt = ImageFont.truetype('arial.ttf', 300)

          d = ImageDraw.Draw(txt)           


         for x_ratio in range(0, 7): 
             x = width * x_ratio/7 + width/20
             for y_ratio in range(0, 7): 
                 y = height * y_ratio/7 + height/20
                 d.text((x,y), "liveimages.in", font=fnt, fill=(255,255,255,128))
                 txt = txt.rotate(45)
         out = PILImage.alpha_composite(base, txt)
         out.thumbnail((1000,1000),PILImage.ANTIALIAS)
         fh.close()
         thumb_name, thumb_extension = os.path.splitext(self.image.name)
         thumb_extension = thumb_extension.lower()
         thumb_filename = thumb_name + '_thumb' + thumb_extension
         if thumb_extension in ['.jpg', '.jpeg']:
            FTYPE = 'JPEG'
         elif thumb_extension == '.gif':
            FTYPE = 'GIF'
         elif thumb_extension == '.png':
            FTYPE = 'PNG'
         else:
             return False    # Unrecognized file type
         i_out = out.convert('RGB')

         temp_thumb = BytesIO()        
         i_out.save(temp_thumb, FTYPE)
         temp_thumb.seek(0)
         if not self.thumbnail:
         # Load a ContentFile into the thumbnail field so it gets saved
            self.thumbnail.save(thumb_filename, ContentFile(temp_thumb.read()), save=True)
            temp_thumb.close()
         return True

Мне нужен такой эскиз из загруженного изображения:

enter image description here

............................. .................................................. .................................................. .................................................. ............................

...