1。使用ImageField获取上传的图像的名称: 我正在使用ImageField存储从发布请求中获取的图像在特定路径中。它返回的是图像路径,而不是图像名称
这是我的模型:image_ref是全局变量
class IMGResize(models.Model):
image = models.ImageField(upload_to='ImgResize', blank=True)
class Meta:
db_table = 'imgresize'
def __str__(self):
return self.image
def save(self,force_insert=False, force_update=False, using=None):
if not self.image:
return
super(IMGResize, self).save()
global image_ref
image_ref = self.image
super(IMGResize, self).save()
这里正在为图像生成一些默认名称,如何获得图像名称?
2。如何将名称自动生成为调整大小的图像:
在以前的模型中,我得到一些图像,我试图使用相同的models.py文件中的某些其他模型来调整大小并将其存储在其他路径中。
这是我的模特
from .resize_code import image_resize
class Profile(models.Model):
image = models.ForeignKey(IMGResize, related_name='profiles', on_delete=models.CASCADE)
name=models.CharField(max_length=10)
image_width=models.PositiveIntegerField(null=True, blank=False)
image_height=models.PositiveIntegerField(null=True, blank=False)
class Meta:
db_table = 'profiles'
unique_together=('name','image')
def __str__(self):
return self.name
def save(self,force_insert=False, force_update=False, using=None):
global image_ref
super(Profile, self).save()
image_resize(self,image_ref)
super(Profile, self).save()
这是我的resize_code.py文件:
def image_resize(self,image,force_insert=False, force_update=False, using=None):
if not image:
return
img = Image.open(os.path.abspath(str(image)), 'r')
img = resize_contain(img, [self.image_width, self.image_height])
path='resized_images/contain.jpeg'
img.save(path, img.format)
在resize_code.py中,我正在对重新调整大小的图像的名称进行硬编码,而不是每次都生成(path ='resized_images / contain.jpeg')
这是我的serializer.py文件:
class profilesSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ('name','image_width','image_height',)
class IMGResizeSerializer(serializers.ModelSerializer):
profiles = profilesSerializer(many=True,read_only=False)
image = Base64ImageField()
class Meta:
model = IMGResize
fields = ('id','image','profiles',)
def create(self, validated_data):
profiles_data = validated_data.pop('profiles')
image = IMGResize.objects.create(**validated_data)
for profile_data in profiles_data:
Profile.objects.create(image=image, **profile_data)
return image
谢谢
答案 0 :(得分:0)
您可以从相应的视图本身检索上载图像的名称。参见下面的示例,
function renderPDF(url, canvasContainer, options) {
options = options || { scale: 1.75 };
function renderPage(page) {
var viewport = page.getViewport(options.scale);
var wrapper = document.createElement("div");
wrapper.className = "canvas-wrapper";
wrapper.id = "editor";
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
canvas.height = viewport.height;
canvas.width = viewport.width+200;
wrapper.appendChild(canvas)
canvasContainer.appendChild(wrapper);
page.render(renderContext);
}
function renderPages(pdfDoc) {
var pagenos="";//page numbers to display on the canvas
var dataarr;
if (pagenos.indexOf(',') > -1) {
dataarr = pagenos.split(',');
for(var i=0;i<dataarr.length;i++){
pdfDoc.getPage(parseInt(dataarr[i])).then(renderPage);
}
}else{
dataarr = pagenos;
pdfDoc.getPage(parseInt(dataarr)).then(renderPage);
}
}
PDFJS.disableWorker = true;
PDFJS.getDocument(url).then(renderPages);
}
function getblobfrombase64(){
var b64Data="";//Base64 pdf to place
var byteCharacters = atob(b64Data);
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
debugger;
var byteArray = new Uint8Array(byteNumbers);
var blob = new window.Blob([byteArray], {type: "application/pdf"});
var pdf_url = URL.createObjectURL(blob);
renderPDF(pdf_url,document.getElementById('holder'));
}
上载的文件将为InMemoryUploadedFile
类型的UploadedFile
类型。它具有name
属性,可提供上传文件的名称