def from_file(cls, file, user, site, desc=''): ''' Construct an ImageCopy from an uploaded file ''' img = ImageCopy(owner=user, site=site, description=desc) # strip exif from given image file.seek(0) exif = pyexiv2.ImageMetadata.from_buffer(file.read()) exif.read() exif_dict = dict() for k in exif.exif_keys: if exif[k].human_value: try: exif_dict[exif[k].label] = exif[k].human_value.decode('utf-8') except UnicodeDecodeError: # ignore invalid chars pass del exif[k] img.exif_str = json.dumps(exif_dict) img.file = ImageFile.from_file(file) img.save() img.id_str = struk.int2str(img.id) img.save() return img
def from_file(cls, file): ''' Construct an ImageFile from file ''' md5 = cls.md5sum(file) try: imgf = cls.objects.filter(md5=md5).get() except ImageFile.DoesNotExist: imgf = cls(md5=md5) imgf.save() try: imgf.id_str = struk.int2str(imgf.id) imgf.file = FieldFile(imgf, imgf.file, imgf.gen_filename()) imgf.save() dir = os.path.dirname(imgf.file.path) if not os.path.exists(dir): os.makedirs(dir) # make sure converted to jpg if isinstance(file, InMemoryUploadedFile): tmp = tempfile.NamedTemporaryFile() file.seek(0) tmp.write(file.read()) tmp.flush() img = Image.open(tmp.name) else: # TemporaryUploadedFile or File img = Image.open(file.file.name) #img.save(imgf.file.path, quality=95, optimize=True) img.save(imgf.file.path, quality=95) #img.save(imgf.file.path) imgf.width_f = img.size[0] imgf.height_f = img.size[1] del img imgf.resample() # save updated width and height of sizes imgf.save() except: imgf.delete() raise return imgf
def _post_post_save(instance, created, **kwargs): '''''' if not instance.id_str: instance.id_str = struk.int2str(instance.id) instance.save()