在 django 模型中创建从另一个 ImageField 派生的 django ImageField
Creating django ImageField that is derived from another ImageField in django Models
我正在创建一个 Django 项目,我在其中使用 ImageField 在数据库中存储了一张图片(图像)作为...
original_pic = models.ImageField()
此外,我想存储一个包含相同图片(Image)的图像
as original_pic
在另一个 ImageField 中带有 Watermark as..
watermark_pic = models.ImageField(default=watermarkImage())
简而言之,我只想在 original_pic
上应用算法并使用 django 模型
将结果保存在 watermark_pic
中
将水印应用于图像的算法(逻辑)如下...
def watermarkImage(filename, text, color, fontfamily):
image = Image.open(filename).convert('RGBA')
imageWatermark = Image.new('RGBA', image.size, (255, 255, 255, 0))
draw = ImageDraw.Draw(imageWatermark)
width, height = image.size
font = ImageFont.truetype(fontfamily, int(height / 20))
textWidth, textHeight = draw.textsize(text, font)
x = width / 5
y = height / 6
draw.text((x, y), text, color, font)
return Image.alpha_composite(image, imageWatermark)
注意:我知道在图像上应用水印的算法。
我不确定你的水印逻辑是怎样的。您可以通过覆盖 save()
方法来实现。不管怎样,我为您制作了一个简单的 image rotation 示例。
from PIL import Image
def rotate_image(image_fp):
im = Image.open(image_fp)
rotate = im.rotate(45)
filename = "rotated_" + image_fp.name
rotate.save(filename)
return filename
class MyBaseImageModel(models.Model):
# your model
original_pic = models.ImageField()
watermark_pic = models.ImageField(null=True, blank=True)
def save(self, *args, **kwargs):
if not self.pk:
rotate_img_name = rotate_image(self.original_pic)
self.watermark_pic = rotate_img_name
super().save(*args, **kwargs)
创建图像时,您不想为 watermark_pic
设置任何值
更新
def watermarkImage(filename, text, color, fontfamily):
image = Image.open(filename).convert('RGBA')
imageWatermark = Image.new('RGBA', image.size, (255, 255, 255, 0))
draw = ImageDraw.Draw(imageWatermark)
width, height = image.size
font = ImageFont.truetype(fontfamily, int(height / 20))
textWidth, textHeight = draw.textsize(text, font)
x = width / 5
y = height / 6
draw.text((x, y), text, color, font)
my_img = Image.alpha_composite(image, imageWatermark)
my_img.save('water_'+filename.name)
return 'water_'+filename.name
class MyBaseImageModel(models.Model):
# your model
original_pic = models.ImageField()
watermark_pic = models.ImageField(null=True, blank=True)
def save(self, *args, **kwargs):
if not self.pk:
rotate_img_name = watermarkImage(your args)
self.watermark_pic = rotate_img_name
super().save(*args, **kwargs)
我正在创建一个 Django 项目,我在其中使用 ImageField 在数据库中存储了一张图片(图像)作为...
original_pic = models.ImageField()
此外,我想存储一个包含相同图片(Image)的图像
as original_pic
在另一个 ImageField 中带有 Watermark as..
watermark_pic = models.ImageField(default=watermarkImage())
简而言之,我只想在 original_pic
上应用算法并使用 django 模型
watermark_pic
中
将水印应用于图像的算法(逻辑)如下...
def watermarkImage(filename, text, color, fontfamily):
image = Image.open(filename).convert('RGBA')
imageWatermark = Image.new('RGBA', image.size, (255, 255, 255, 0))
draw = ImageDraw.Draw(imageWatermark)
width, height = image.size
font = ImageFont.truetype(fontfamily, int(height / 20))
textWidth, textHeight = draw.textsize(text, font)
x = width / 5
y = height / 6
draw.text((x, y), text, color, font)
return Image.alpha_composite(image, imageWatermark)
注意:我知道在图像上应用水印的算法。
我不确定你的水印逻辑是怎样的。您可以通过覆盖 save()
方法来实现。不管怎样,我为您制作了一个简单的 image rotation 示例。
from PIL import Image
def rotate_image(image_fp):
im = Image.open(image_fp)
rotate = im.rotate(45)
filename = "rotated_" + image_fp.name
rotate.save(filename)
return filename
class MyBaseImageModel(models.Model):
# your model
original_pic = models.ImageField()
watermark_pic = models.ImageField(null=True, blank=True)
def save(self, *args, **kwargs):
if not self.pk:
rotate_img_name = rotate_image(self.original_pic)
self.watermark_pic = rotate_img_name
super().save(*args, **kwargs)
创建图像时,您不想为 watermark_pic
设置任何值
更新
def watermarkImage(filename, text, color, fontfamily):
image = Image.open(filename).convert('RGBA')
imageWatermark = Image.new('RGBA', image.size, (255, 255, 255, 0))
draw = ImageDraw.Draw(imageWatermark)
width, height = image.size
font = ImageFont.truetype(fontfamily, int(height / 20))
textWidth, textHeight = draw.textsize(text, font)
x = width / 5
y = height / 6
draw.text((x, y), text, color, font)
my_img = Image.alpha_composite(image, imageWatermark)
my_img.save('water_'+filename.name)
return 'water_'+filename.name
class MyBaseImageModel(models.Model):
# your model
original_pic = models.ImageField()
watermark_pic = models.ImageField(null=True, blank=True)
def save(self, *args, **kwargs):
if not self.pk:
rotate_img_name = watermarkImage(your args)
self.watermark_pic = rotate_img_name
super().save(*args, **kwargs)