def generate_thumbnails(self) -> bool:
if not os.path.exists(self.zipped.path):
return False
try:
my_zip = zipfile.ZipFile(self.zipped.path, 'r')
except (zipfile.BadZipFile, NotImplementedError):
return False
if my_zip.testzip():
my_zip.close()
return False
self.thumbnail.delete(save=False)
filtered_files = list(filter(discard_zipfile_contents, sorted(my_zip.namelist(), key=zfill_to_three)))
if not filtered_files:
my_zip.close()
return False
if self.image_set.all():
first_file = filtered_files[self.image_set.all()[0].archive_position - 1]
else:
first_file = filtered_files[0]
with my_zip.open(first_file) as current_img:
im = PImage.open(current_img)
if im.mode != 'RGB':
im = im.convert('RGB')
im.thumbnail((200, 290), PImage.ANTIALIAS)
thumb_name = thumb_path_handler(self, "thumb2.jpg")
os.makedirs(os.path.dirname(pjoin(settings.MEDIA_ROOT, thumb_name)), exist_ok=True)
im.save(pjoin(settings.MEDIA_ROOT, thumb_name), "JPEG")
self.thumbnail.name = thumb_name
my_zip.close()
super(Archive, self).save()
return True
评论列表
文章目录