def put(self, file_obj, **kwargs): """ Insert a image in database applying field properties (size, thumbnail_size) """ field = self.instance._fields[self.key] try: img = Image.open(file_obj) img_format = img.format except: raise ValidationError('Invalid image') if (field.size and (img.size[0] > field.size['width'] or img.size[1] > field.size['height'])): size = field.size if size['force']: img = ImageOps.fit(img, (size['width'], size['height']), Image.ANTIALIAS) else: img.thumbnail((size['width'], size['height']), Image.ANTIALIAS) thumbnail = None if field.thumbnail_size: size = field.thumbnail_size if size['force']: thumbnail = ImageOps.fit(img, (size['width'], size['height']), Image.ANTIALIAS) else: thumbnail = img.copy() thumbnail.thumbnail((size['width'], size['height']), Image.ANTIALIAS) if thumbnail: thumb_id = self._put_thumbnail(thumbnail, img_format) else: thumb_id = None w, h = img.size io = StringIO() img.save(io, img_format) io.seek(0) return super(ImageGridFsProxy, self).put(io, width=w, height=h, format=img_format, thumbnail_id=thumb_id, **kwargs)
def get_file(path): """Use a BytesIO instead of a file to allow to have a one-liner and avoid that the file remains opened""" bytes_io = StringIO() with open(path, "rb") as f: bytes_io.write(f.read()) bytes_io.seek(0) return bytes_io
def _put_thumbnail(self, thumbnail, format, **kwargs): w, h = thumbnail.size io = StringIO() thumbnail.save(io, format) io.seek(0) return self.fs.put(io, width=w, height=h, format=format, **kwargs)
def get_file(path): """Use a BytesIO instead of a file to allow to have a one-liner and avoid that the file remains opened""" bytes_io = StringIO() with open(path, 'rb') as f: bytes_io.write(f.read()) bytes_io.seek(0) return bytes_io
def handle_upload_img(self,post,files): size = (128,128) image = pil_image.open(files['img']) format = image.format width, height = image.size io = StringIO() image.save(io, format) io.seek(0) self.imageRepo.save(width, height, post['fileName'], format, io)
def handle_upload_img(self, post, files): size = (128, 128) image = pil_image.open(files['img']) format = image.format width, height = image.size io = StringIO() image.save(io, format) io.seek(0) self.imageRepo.save(width, height, post['fileName'], format, io)
def save_image_in_mapfield(cls, image_obj, image_format, instance, index): io = StringIO() image_obj.save(io, image_format) io.seek(0) image_proxy = cls._fields['images'].field.get_proxy_obj('images', instance) image_proxy.put(io) instance.images[index] = image_proxy
def test_file_fields(self): """Ensure that file fields can be written to and their data retrieved """ class PutFile(Document): the_file = FileField() PutFile.drop_collection() text = six.b("Hello, World!") content_type = "text/plain" putfile = PutFile() putfile.the_file.put(text, content_type=content_type, filename="hello") putfile.save() result = PutFile.objects.first() assert putfile == result assert ( "%s" % result.the_file == "<GridFSProxy: hello (%s)>" % result.the_file.grid_id ) assert result.the_file.read() == text assert result.the_file.content_type == content_type result.the_file.delete() # Remove file from GridFS PutFile.objects.delete() # Ensure file-like objects are stored PutFile.drop_collection() putfile = PutFile() putstring = StringIO() putstring.write(text) putstring.seek(0) putfile.the_file.put(putstring, content_type=content_type) putfile.save() result = PutFile.objects.first() assert putfile == result assert result.the_file.read() == text assert result.the_file.content_type == content_type result.the_file.delete()
def get_thumbnail(self): try: my_thumbnail = DBImageThumbnail.objects.get(DBImage=self.pk) return my_thumbnail except DBImageThumbnail.DoesNotExist: im = Image.open(self.path) thumbnail_height = 200 thumbnail_width = thumbnail_height * im.width / im.height im.thumbnail((thumbnail_width, thumbnail_height), Image.ANTIALIAS) io = StringIO() im.save(io, im.format) io.seek(0) my_thumbnail = DBImageThumbnail(DBImage=self.pk, file=io, width=thumbnail_width, height=thumbnail_height, mime_type=self.mime_type) my_thumbnail.save() return my_thumbnail
def test_file_fields(self): """Ensure that file fields can be written to and their data retrieved """ class PutFile(Document): the_file = FileField() PutFile.drop_collection() text = six.b('Hello, World!') content_type = 'text/plain' putfile = PutFile() putfile.the_file.put(text, content_type=content_type, filename="hello") putfile.save() result = PutFile.objects.first() self.assertTrue(putfile == result) self.assertEqual("%s" % result.the_file, "<GridFSProxy: hello>") self.assertEqual(result.the_file.read(), text) self.assertEqual(result.the_file.content_type, content_type) result.the_file.delete() # Remove file from GridFS PutFile.objects.delete() # Ensure file-like objects are stored PutFile.drop_collection() putfile = PutFile() putstring = StringIO() putstring.write(text) putstring.seek(0) putfile.the_file.put(putstring, content_type=content_type) putfile.save() result = PutFile.objects.first() self.assertTrue(putfile == result) self.assertEqual(result.the_file.read(), text) self.assertEqual(result.the_file.content_type, content_type) result.the_file.delete()