class User: name = "John" group_set = FakeManager([1, 2, 3]) avatar = ImageFieldFile(None, Mock(), name=None) @property def tags(self): return FakeQS([Tag(1, "foo"), Tag(2, "bar")])
def hydrate(self, bundle): try: bundle.obj.last_frame.file except ValueError: bundle.obj.last_frame = ImageFieldFile( bundle.obj, Question._meta.get_field_by_name('last_frame')[0], "") return bundle
def get_image_display(self): if self.type == 'video' and not hasattr(self.image, 'url'): if self._video_update_thumbnail(): return self.image else: return ImageFieldFile(instance=None, field=FileField(), name='processing.jpg') else: return self.image
def _create_thumbnail(image_field: ImageFieldFile, thumbnail_image_field: ImageFieldFile, size: tuple): image = Image.open(image_field.file.file) image.thumbnail(size=size) image_file = BytesIO() image.save(image_file, image.format) thumbnail_image_field.save( image_field.name, InMemoryUploadedFile( image_file, None, "", image_field.file.content_type, image.size, image_field.file.charset, ), save=False, )
def makeThumb(pic): base, ext = os.path.splitext( os.path.basename('./FileDir/Thumb/' + pic.name)) thumb_pixbuf = make_thumb_img(os.path.join('./FileDir/Thumb', pic.name)) relate_thumb_path = os.path.join('/Thumb', base + '.thumb' + ext) thumb_path = os.path.join('./FileDir', relate_thumb_path) thumb_pixbuf.save(thumb_path) thumb = ImageFieldFile(relate_thumb_path) return thumb
def test_custom_file_widget(self): """ ``ClearableFileInput`` respects its ``file_widget`` argument. """ widget = ClearableFileInput(file_widget=ImageWidget()) html = widget.render('fieldname', ImageFieldFile(None, ImageField(), 'tiny.png')) self.assertTrue('<img' in html)
def save(self, commit=True): instance = super(SettingsForm, self).save(commit=False) if self.instance.type == Settings.Type.BOOLEAN: # if it is checkbox and value is False - save as empty string if self.instance.value == 'False': self.instance.value = '' elif self.instance.type == Settings.Type.IMAGE and 'value' in self.changed_data: new_field = ImageFieldFile(instance=self.instance, field=models.ImageField(upload_to=UPLOAD_TO_DIRECTORY), name=self.instance.value.name) new_field.field.name = 'value' new_field.save(self.instance.value.name, self.instance.value, True) self.instance.value = self.instance.value.lower() if commit: instance.save() return instance
def test_custom_template(self): """ ``ImageWidget`` respects a custom template. """ widget = ImageWidget(template='<div>%(image)s</div>' '<div>%(input)s</div>') html = widget.render('fieldname', ImageFieldFile(None, ImageField(), 'tiny.png')) self.assertTrue(html.startswith('<div><img'))
def save(self): super(File, self).save() #将上传的图片先保存一下,否则报错 base, ext = os.path.splitext(os.path.basename(self.image.path)) thumb_pixbuf = make_thumb(os.path.join(MEDIA_ROOT, self.image.name)) relate_thumb_path = os.path.join(THUMB_ROOT, base + '.thumb' + ext).replace("\\","/") thumb_path = os.path.join(MEDIA_ROOT, relate_thumb_path).replace("\\","/") thumb_pixbuf.save(thumb_path) self.thumb = ImageFieldFile(self, self.thumb, relate_thumb_path) super(File, self).save() #再保存一下,包括缩略图等
def save(self): super(PicManage, self).save() # 将上传的图片先保存一下,否则报错 base, ext = os.path.splitext(os.path.basename(self.img.path)) thumb_pixbuf = make_thumb(os.path.join(MEDIA_ROOT, self.img.name)) relate_thumb_path = os.path.join(THUMB_ROOT, base + '_thumb' + ext) thumb_path = os.path.join(MEDIA_ROOT, relate_thumb_path) thumb_pixbuf.save(thumb_path) self.imgthumb = ImageFieldFile(self, self.imgthumb, relate_thumb_path) super(PicManage, self).save() # 再保存一下,包括缩略图等
def save(self): super(equipment, self).save() # 将上传的图片先保存一下,否则报错 base, ext = os.path.splitext(os.path.basename(self.epic.path)) thumb_pixbuf = make_thumb(os.path.join(MEDIA_ROOT, self.epic.name)) relate_thumb_path = os.path.join(THUMB_ROOT, base + '.thumb' + ext) thumb_path = os.path.join(MEDIA_ROOT, relate_thumb_path) thumb_pixbuf.save(thumb_path) self.epic = ImageFieldFile(self, self.epic, relate_thumb_path) super(equipment, self).save()
def value_from_datadict(self, data, files, name): value = super(CustomClearableFileInput, self).value_from_datadict(data, files, name) # value is false if the clear image check box is set if value is False: userModelAvatarField = get_user_model().avatar.field value = self.__class__.defaultAvatar = ImageFieldFile(instance=None, field=userModelAvatarField, name=defaultAvatar) return value
def save(self): super(product, self).save() #将上传的图片先保存一下,否则报错 base, ext = os.path.splitext(os.path.basename(self.image.path)) thumb_pixbuf = make_thumb(os.path.join(MEDIA_ROOT, self.image.name)) relate_thumb_path = os.path.join(THUMB_ROOT, "small/" + base + '.thumb' + ext) thumb_path = os.path.join(MEDIA_ROOT, relate_thumb_path) thumb_pixbuf.save(thumb_path) self.thumb = ImageFieldFile(self, self.thumb, relate_thumb_path) super(product, self).save()
def save(self, *args, **kwargs): base, ext = os.path.splitext(os.path.basename(self.image.path)) super(Images, self).save() thumbBuf = make_thumb(os.path.join(MEDIA_ROOT, self.image.name)) if thumbBuf: relate_thumb_path = os.path.join('upload_images/thumb_images/', base + '.thumb' + ext) thumb_path = os.path.join(MEDIA_ROOT, relate_thumb_path) thumbBuf.save(thumb_path) self.thumb = ImageFieldFile(self, self.thumb, relate_thumb_path) super(Images, self).save()
def test_custom_file_widget_via_subclass(self): """ Default ``file_widget`` class can also be customized by subclassing. """ class ClearableImageWidget(ClearableFileInput): default_file_widget_class = ImageWidget widget = ClearableImageWidget() html = widget.render('fieldname', ImageFieldFile(None, ImageField(), 'tiny.png')) self.assertTrue('<img' in html)
def save(self): super(PicInfo, self).save() if self.audit==False and self.image: img_name = self.image.name.split('/')[-1] thumb_pixbuf = make_thumb_fixed(self.image.path) rel_thumb_path = 'thumb_trashimg/'+ img_name thumb_path=os.path.join(MEDIA_ROOT,"thumb_trashimg",img_name) with open(thumb_path,"wb") as f: thumb_pixbuf.save(f) self.thumb = ImageFieldFile(self, self.thumb, rel_thumb_path) super(PicInfo, self).save()
def save(self): # 重写图片保存方法 super(PollingItemImages, self).save() if self.image: img_name = self.image.name if self.is_avatar == True: thumb_avatar = MakeThumb('media/' + img_name, size=300) thumb_path = 'polling/item/img/' + str( self.id) + '_300*height.' + img_name.split('.')[-1] thumb_full_path = 'media/' + thumb_path thumb_avatar.save(thumb_full_path) self.thumb = ImageFieldFile(self, self.thumb, thumb_path) super(PollingItemImages, self).save() thumb_preview = MakeThumb('media/' + img_name) thumb_path = 'polling/item/img/' + str( self.id) + '_128*height.' + img_name.split('.')[-1] thumb_full_path = 'media/' + thumb_path thumb_preview.save(thumb_full_path) self.preview = ImageFieldFile(self, self.preview, thumb_path) super(PollingItemImages, self).save()
def save(self, *args, **kwargs): # 重写图片保存方法 super(ActivityImages, self).save() base, ext = os.path.splitext(self.img.name) thumb_preview = MakeThumb(self.img.path, 300) thumb_path = base + '_tiny' + ext #调用Image.save这个函数的时候需要使用绝对路径 thumb_preview.save( '/home/webapp/wechat/running_association_wx/media/' + thumb_path) self.tiny_img = ImageFieldFile(self, self.tiny_img, thumb_path) super(ActivityImages, self).save()
def test_render(self): """ ``ImageWidget`` renders the file input and the image thumb. """ widget = ImageWidget() html = widget.render('fieldname', ImageFieldFile(None, ImageField(), 'tiny.png')) # test only this much of the html, because the remainder will # vary depending on whether we have sorl-thumbnail self.assertTrue('<img' in html) self.assertTrue('/media/tiny' in html)
def save(self): super(Device, self).save() #将上传的图片先保存一下,否则报错 base, ext = os.path.splitext(os.path.basename(self.devimg.path)) if base != 'default': thumb_pixbuf = make_thumb( os.path.join(MEDIA_ROOT, self.devimg.name)) relate_thumb_path = os.path.join(THUMB_ROOT, base + '.thumb' + ext) thumb_path = os.path.join(MEDIA_ROOT, relate_thumb_path) thumb_pixbuf.save(thumb_path) thumb_path1 = 'thumb/' + base + '.thumb' + ext self.devthumb = ImageFieldFile(self, self.devthumb, thumb_path1) super(Device, self).save() #再保存一下,包括缩略图等
def save(self): super(Photo, self).save() #base,ext = os.path.splitext(os.path.basename(self.photo.path)) img_name = self.photo.name.split("/")[1] thumb_photo = make_thumb( os.path.join(settings.MEDIA_ROOT, "photo/" + img_name)) thumb_path = os.path.join(settings.MEDIA_ROOT, "photo_thumb/" + img_name) thumb_photo.save(thumb_path) self.photo_thumb = ImageFieldFile(self, self.photo_thumb, "photo_thumb/" + img_name) super(Photo, self).save()
def save(self, *args, **kwargs): if not self.profilepicture: im = Image.open(DEFAULT_PROFILE_IMAGE_PATH) full_path = PROJECT_ROOT + MEDIA_URL + 'profilepictures/' + self.user.username + '.png' print full_path + " :This is the fullpath\n" im.save(full_path, 'PNG') short_path = 'profilepictures/' + self.user.username + '.png' self.profilepicture = ImageFieldFile(instance=None, name=short_path, field=FileField()) super(UserProfile, self).save(*args, **kwargs)
class User: name = "John Smith" group_set = FakeManager([1, 2, 3]) avatar = ImageFieldFile(None, Mock(), name=None) boss: Optional[Boss] = Boss() @property def tags(self): return FakeQS([Tag(1, "foo"), Tag(2, "bar")]) def get_boss_title(self): return self.boss and self.boss.title
def save(self, *args, **kwargs): super().save() if self.avator.name == 'avator/default.jpg': return base, ext = os.path.splitext(self.avator.name) thumb_pixbuf = make_thumb(os.path.join(MEDIA_ROOT, self.avator.name), size=THUMB_SIZE) thumb_path = os.path.join(MEDIA_ROOT, base + f'.{THUMB_SIZE}x{THUMB_SIZE}' + ext) relate_thumb_path = os.path.join('/'.join(self.avator.name.split('/')[:-1]), os.path.basename(thumb_path)) relate_thumb_path = base + f'.{THUMB_SIZE}x{THUMB_SIZE}' + ext thumb_pixbuf.save(thumb_path) self.avator_sm = ImageFieldFile(self, self.avator_sm, relate_thumb_path) super().save() # 再保存一下,包括缩略图等
def save(self): super(Product, self).save() img_name, extension = os.path.splitext( os.path.basename(self.image.path)) thumb_pixbuf = make_thumb( os.path.join(settings.MEDIA_ROOT, self.image.name)) relate_thumb_path = os.path.join(THUMB_ROOT, img_name + '_thumb' + extension) thumb_path = os.path.join(settings.MEDIA_ROOT, relate_thumb_path) thumb_pixbuf.save(thumb_path) self.thumb = ImageFieldFile(self, self.thumb, relate_thumb_path) super(Product, self).save()
def test_custom_template(self): """ ``ClearableFileInput`` respects its ``template`` argument. """ widget = ClearableFileInput(template='Clear: %(checkbox)s %(input)s') html = widget.render('fieldname', ImageFieldFile(None, ImageField(), 'tiny.png')) self.assertHTMLEqual( html, 'Clear: ' '<input type="checkbox" name="fieldname_1" /> ' '<input type="file" name="fieldname_0" />')
def test_schema_with_image(): user = User() field = Mock() field.storage.url = Mock(return_value="/smile.jpg") user.avatar = ImageFieldFile(None, field, name="smile.jpg") schema = UserSchema.from_orm(user) assert schema.dict() == { "name": "John", "groups": [1, 2, 3], "tags": [{"id": "1", "title": "foo"}, {"id": "2", "title": "bar"}], "avatar": "/smile.jpg", }
def save(self, update_fields=None): # 重写图片保存方法 super(Activity, self).save() if self.thumb_image or self.image: img_name = self.image.name thumb_preview = MakeThumb('media/' + img_name) #image对象 thumb_path = img_name.split('.')[0] + '_160*90.' + img_name.split( '.')[-1] thumb_full_path = 'media/' + thumb_path thumb_preview.save(thumb_full_path) #将图片保存到这个位置 self.thumb_image = ImageFieldFile(self, self.thumb_image, thumb_path) super(Activity, self).save()
def set_variations(self, instance=None, **kwargs): """ Creates a "variation" object as attribute of the ImageField instance. Variation attribute will be of the same class as the original image, so "path", "url"... properties can be used :param instance: FileField """ if getattr(instance, self.name): field = getattr(instance, self.name) for variation in self.variations: variation_name = self.attr_class.get_variation_name(instance, self, variation) variation_field = ImageFieldFile(instance, self, variation_name) setattr(field, variation['name'], variation_field)
def save(self, *args, **kwargs): super(Blog, self).save(*args, **kwargs) try: img = Image.open(self.picture_small) img.thumbnail((130, 100000000), Image.ANTIALIAS) img = crop_center(img, (103, 77)) name = add_file_termination(self.picture.name, '_crop') path = add_file_termination(self.picture.path, '_crop') self.picture_cropped = ImageFieldFile(self, self.picture_cropped, name) img.save(self.picture_cropped.path, quality=100) super(Blog, self).save(*args, **kwargs) except: pass
def create_pic(cls, image: ImageFieldFile, size=None) -> 'Picture': """ Creates class instance. :param image: uploaded image :param size: size to shrink uploaded image :return: Picture class new instance """ with image.open() as image: with Image.open(image) as image_opened: file_format = image_opened.format image_rgb = cls.convert_to_rgb(image_opened) instance = cls(image_rgb, file_format, size) instance._picture = instance.rotate_when_turned() return instance
def __init__(self, instance, field, attname, source_file): ImageFieldFile.__init__(self, instance, field, None) self.storage = field.storage or source_file.storage self.attname = attname self.source_file = source_file