我在尝试调整图像大小时遇到问题。我让它以某种方式在使用 sqlite3 数据库的不同项目上工作,但它不适用于我最新使用 mysql 的项目。
class UserImage(models.Model):
author = models.OneToOneField(User, on_delete=models.CASCADE, null=True, default=True)
myimage = models.ImageField(default='profile_pics/default-profile-picture.jpg', upload_to=profile_upload)
def __str__(self):
return f'{self.author.username} image'
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
img_read = storage.open(self.myimage.name, 'r')
img = Image.open(img_read)
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
in_mem_file = io.BytesIO()
img.save(in_mem_file, format='JPEG')
img_write = storage.open(self.myimage.name, 'w+')
img_write.write(in_mem_file.getvalue())
img_write.close()
img_read.close()
错误状态
"'utf-8' 编解码器无法解码位置 0 的字节 0xff: 无效的起始字节。你传入 b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00H' ( )"
