使用python在图像中插入文本

时间:2017-04-08 04:52:21

标签: python python-2.7 python-imaging-library

我正在使用PIL库和python-qrcode package为每个Ticket对象生成一个qrcode。大多数情况下,我关注this tutorial。这里作者在qrcode中插入了文本。

enter image description here

如何使用上述库实现相同的效果?实际上我想要做的是在qrcode下面放置一个文本(Ticket show id),如下所示:

------------|
|           |
|           |
|           |
|___________|

Seat id: 23c
Show id: 250

现在,这就是我创建qrcode的方式:

class MovieTicket(models.Model):
    show = models.ForeignKey(Show)
    seat = models.ForeignKey(Seat)
    user = models.ForeignKey(User)
    purchased_at = models.DateTimeField(default=timezone.now)
    qrcode = models.ImageField(upload_to='qrcode', blank=True, null=True)
    qrcode_data = models.CharField(max_length=255, default=str(time()).replace('.', '_'))

    def save(self, *args, **kwargs):
        if self.show and self.seat and self.user and self.purchased_at:
            self.qrcode_data = str(self.user.mobile) + '_' + str(self.show.id) + '_' + str(self.seat.id) + '_' + str(
                time()).replace('.', '_')
            qr = qrcode.QRCode(version=None, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=10, border=4)
            qr.add_data(self.qrcode_data)
            qr.make(fit=True)
            img = qr.make_image()
            buffer = StringIO.StringIO()
            img.save(buffer)
            filebuffer = InMemoryUploadedFile(
                buffer, None, self.qrcode_data + '.jpg', 'image/jpg', buffer.len, None)
            self.qrcode = filebuffer
        super(MovieTicket, self).save(*args, **kwargs)

是否有可能达到我想要的目标?

0 个答案:

没有答案