我想有一个方法可以给我调整大小的图像,如果我在对象上调用这个方法,我怎么能用Pillow做到这一点?
我的模特:
class Item(models.Model):
title = models.CharField(max_length=255, null=True)
img = models.ImageField()
def __str__(self):
return self.owner.first_name
def get_image(self):
image = Image.open(self.img)
resized_image = image.resize((128, 128))
return resized_image
这个总是给我这样的东西
<PIL.Image.Image image mode=RGB size=128x128 at 0x7F001CF92D30>
答案 0 :(得分:0)
如果您只想显示调整大小的图像(不调整实际图像本身的大小),您可以直接在html / css中执行此操作:
例如:
HTML中的
ffi_node = ffi.new("Node *")
ffi_node.value = some_value
在Css中:
struct Node** children
我希望这会有所帮助。
答案 1 :(得分:0)
来源https://www.youtube.com/watch?v=5t6kgAEMIB8
我最近发现这可能会有所帮助:
在您的forms.py中:
from PIL import Image
from django import forms
from django.core.files import FILE
from .models import YourModel
class YourModelForm(forms.ModelForm):
x = forms.FloatField(widget=forms.HiddenInput())
y = forms.FloatField(widget=forms.HiddenInput())
width = forms.FloatField(widget=forms.HiddenInput())
height = forms.FloatField(widget=forms.HiddenInput())
class Meta:
model = YourModel
fields = '__all__'
exclude = ['user', 'date_joined','Credits_earned','skills']
def save(self):
photo = super(YourModelForm,self).save()
x = self.cleaned_data.get('x')
y = self.cleaned_data.get('y')
w = self.cleaned_data.get('width')
h = self.cleaned_data.get('height')
image = Image.open(photo.file)
cropped_image = image.crop((x,y,w+x,w+y))
resized_image = cropped_image.resize((1200,600), Image.ANTIALIAS)
resized_image.save(photo.file.path)
return photo
这是JS在你的模板中处理它:
<script>
$(function () {
// SCRIPT TO OPEN THE MODAL WITH THE PREVIEW
$("id_file").change(function () {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$("#image").attr("src", e.target.result);
$("#modalCrop").modal("show");
}
reader.readAsDatarURL(this.files[0]);
}
});
// SCRIPTS TO HANDLE THE CROPPER BOX
var $image = $("image");
var cropBoxData;
var canvasData;
$("#modalCrop").on("shown.bs.modal", function () {
$image.cropper({
viewMode: 3,
movable: false,
zoomable: false,
scalable: false,
ready: function () {
$image.cropper("setCanvasData", canvasData);
$image.cropper("setCropBoxData", cropBoxData);
}
});
}).on("hidden.bs.modal", function () {
cropBoxData = $image.cropper("getCropBoxData");
canvasData = $image.cropper("getCanvasData");
$image.cropper("destroy");
});
$(".js-zoom-in").click(function () {
$image.cropper("zoom", 0.1);
});
$(".js-zoom-out").click(function () {
$image.cropper("zoom", -0.1)
});
// SCRIPT TO COLLECT THE DATA AND POST TO THE SERVER
$(".js-crop-and-upload").click(function () {
$("#id_x").val(cropData["x"]);
$("#id_y").val(cropData["y"]);
$("#id_height").val(cropData["height"]);
$("#id_width").val(cropData["width"]);
$("#formUpload").submit;
});
});
</script>
您可以在脚本中手动设置ID以匹配您的模板,或者您可以使用视频()中的ID,尽管您必须暂停并对其进行编码。此外,如果您使用基于函数的视图而不是基于类的视图,则必须将文件添加到表单验证中,例如:
def YourView(request)
if request.method == 'POST':
form = YourModelForm(request.POST or None, request.FILES or None)
if form.is_valid():
form.save()
return redirect('Your_redirect_URL')
else:
form = YourModelForm()
return render(request, 'YourTemplate.html', {'form': form})