Can't save image to django model - python

I try to resize image before save:
from PIL import Image
class UserAvatarUpdate(views.APIView):
serializer_class = UserSerializer
def patch(self, *args, **kwargs):
instance = ExtUser.objects.get(id=self.request.user.id)
instance.avatar = self.request.FILES['file']
size = 100,100
filename = instance.avatar.path
image = Image.open(filename)
image = image.resize(size,Image.ANTIALIAS)
instance.avatar = image
instance.save()
return Response(
UserSerializer(instance).data,
status=status.HTTP_200_OK
)
but I get the error below on save:
> Internal Server Error: /api/v1/update_user_avatar/ Traceback (most
> recent call last): File
> "/home/alexandr/sprutlabs_python/lib/python3.4/site-packages/django/core/handlers/base.py",
> line 149, in get_response
> response = self.process_exception_by_middleware(e, request) File "/home/alexandr/sprutlabs_python/lib/python3.4/site-packages/django/core/handlers/base.py",
> line 147, in get_response
> response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/alexandr/sprutlabs_python/lib/python3.4/site-packages/django/views/decorators/csrf.py",
> line 58, in wrapped_view
> return view_func(*args, **kwargs) File "/home/alexandr/sprutlabs_python/lib/python3.4/site-packages/django/views/generic/base.py",
> line 68, in view
> return self.dispatch(request, *args, **kwargs) File "/home/alexandr/sprutlabs_python/lib/python3.4/site-packages/rest_framework/views.py",
> line 466, in dispatch
> response = self.handle_exception(exc) File "/home/alexandr/sprutlabs_python/lib/python3.4/site-packages/rest_framework/views.py",
> line 463, in dispatch
> response = handler(request, *args, **kwargs) File "/home/alexandr/sprutlabs_python/sprutlabs/posts/views.py", line 123,
> in patch
> instance.save() File "/home/alexandr/sprutlabs_python/lib/python3.4/site-packages/django/contrib/auth/base_user.py", line 74, in save
> super(AbstractBaseUser, self).save(*args, **kwargs) File "/home/alexandr/sprutlabs_python/lib/python3.4/site-packages/django/db/models/base.py",
> line 708, in save
> force_update=force_update, update_fields=update_fields) File "/home/alexandr/sprutlabs_python/lib/python3.4/site-packages/django/db/models/base.py",
> line 736, in save_base
> updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) File
> "/home/alexandr/sprutlabs_python/lib/python3.4/site-packages/django/db/models/base.py",
> line 798, in _save_table
> for f in non_pks] File "/home/alexandr/sprutlabs_python/lib/python3.4/site-packages/django/db/models/base.py",
> line 798, in <listcomp>
> for f in non_pks] File "/home/alexandr/sprutlabs_python/lib/python3.4/site-packages/django/db/models/fields/files.py", line 309, in pre_save
> if file and not file._committed: File "/home/alexandr/sprutlabs_python/lib/python3.4/site-packages/PIL/Image.py",
> line 632, in __getattr__
> raise AttributeError(name) AttributeError: _committed
my model:
avatar = models.ImageField(
'Аватар',
blank=True,
null=True,
upload_to="user/avatar"
)

You can't directly assign a PIL image to an ImageField like so. You need a little workaround:
from io import BytesIO
from django.core.files.base import ContentFile
image = Image.open(filename)
image = image.resize(size,Image.ANTIALIAS)
image_io = BytesIO()
image.save(image_io, format='jpeg', quality=80) # you can change format and quality
# save to model
image_name = "my_image"
instance.avatar.save(image_name, ContentFile(image_io.getvalue()))

Related

how to resize an image and save it to storage using save function [duplicate]

I am trying to save a cropped image to a model. I am getting the following error:
Traceback (most recent call last):  File
"/mypath/lib/python2.7/site-packages/django/core/handlers/base.py",
line 132, in get_response    response = wrapped_callback(request,
*callback_args, **callback_kwargs)  File "/mypath/lib/python2.7/site-packages/django/contrib/auth/decorators.py",
line 22, in _wrapped_view    return view_func(request, *args,
**kwargs)  File "/mypath/views.py", line 236, in player_edit    player.save()  File
"/mypath/lib/python2.7/site-packages/django/db/models/base.py", line
734, in save    force_update=force_update,
update_fields=update_fields)  File
"/mypath/lib/python2.7/site-packages/django/db/models/base.py", line
762, in save_base    updated = self._save_table(raw, cls,
force_insert, force_update, using, update_fields)  File
"/mypath/lib/python2.7/site-packages/django/db/models/base.py", line
824, in _save_table    for f in non_pks]  File
"/mypath/lib/python2.7/site-packages/django/db/models/fields/files.py",
line 313, in pre_save    if file and not file._committed:  File
"/mypath/lib/python2.7/site-packages/PIL/Image.py", line 512, in
getattr    raise AttributeError(name) AttributeError: _committed
My view which handles the form submit looks like this:
if request.method == 'POST':
form = PlayerForm(request.POST, request.FILES, instance=current_player)
if form.is_valid():
temp_image = form.cleaned_data['profile_image2']
player = form.save()
cropped_image = cropper(temp_image, crop_coords)
player.profile_image = cropped_image
player.save()
return redirect('player')
The crop function looks like this:
from PIL import Image
import Image as pil
def cropper(original_image, crop_coords):
original_image = Image.open(original_image)
original_image.crop((0, 0, 165, 165))
original_image.save("img5.jpg")
return original_image
Is this correct process to save the cropped image to the model. If so, why am I getting the above error?
Thanks!
The function should look like this:
# The crop function looks like this:
from PIL import Image
from django.core.files.base import ContentFile
def cropper(original_image, crop_coords):
img_io = StringIO.StringIO()
original_image = Image.open(original_image)
cropped_img = original_image.crop((0, 0, 165, 165))
cropped_img.save(img_io, format='JPEG', quality=100)
img_content = ContentFile(img_io.getvalue(), 'img5.jpg')
return img_content
For Python version >= 3.5
from io import BytesIO, StringIO()
img_io = StringIO() # or use BytesIO() depending on the type
rest of the things works great with #phourxx answer

Django postgres full text search error "Unsupported lookup 'search' for CharField"

All similar problems have been solved by adding django.contrib.postgres to INSTALLED_APPS in settings.py, which is also all the docs mention on how to use the lookup. I've already done this and the lookup still isn't working, despite whether I use __search or search= for the filter. Any ideas? Do I need to register the lookup in my model myself?
settings.py:
INSTALLED_APPS = [
...
'django.contrib.postgres',
# my_project
'my_project.apps.appname',
'my_project.apps.appname',
...
error line:
x = y.objects.filter(description__search="example")
traceback:
File "d:\proj\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "d:\proj\env\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "d:\proj\env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "d:\proj\env\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "d:\proj\env\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "d:\proj\env\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "d:\proj\env\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "d:\proj\env\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "D:\proj\my_project\my_project\apps\appname\views.py", line 306, in get
x = y.objects.filter(description__search="example")
File "d:\proj\env\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "d:\proj\env\lib\site-packages\django\db\models\query.py", line 941, in filter
return self._filter_or_exclude(False, args, kwargs)
File "d:\proj\env\lib\site-packages\django\db\models\query.py", line 961, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "d:\proj\env\lib\site-packages\django\db\models\query.py", line 968, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "d:\proj\env\lib\site-packages\django\db\models\sql\query.py", line 1393, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "d:\proj\env\lib\site-packages\django\db\models\sql\query.py", line 1412, in _add_q
child_clause, needed_inner = self.build_filter(
File "d:\proj\env\lib\site-packages\django\db\models\sql\query.py", line 1347, in build_filter
condition = self.build_lookup(lookups, col, value)
File "d:\proj\env\lib\site-packages\django\db\models\sql\query.py", line 1187, in build_lookup
lhs = self.try_transform(lhs, lookup_name)
File "d:\proj\env\lib\site-packages\django\db\models\sql\query.py", line 1226, in try_transform
raise FieldError(
django.core.exceptions.FieldError: Unsupported lookup 'search' for CharField or join on the field not permitted.```
x = y.objects.filter(description__search="example")
Search lookup is specific to PostrgreSQL so you need to set up PosgreSQL db first.
https://docs.djangoproject.com/en/4.0/ref/contrib/postgres/search/
If you want to just look for a string in CharField or TextField you can use
x = y.objects.filter(description__icontains="example")
docs: https://docs.djangoproject.com/en/4.0/ref/models/querysets/#icontains

Download multiple files as zip in Django shows memory error

I am trying to download multiple files as a zip in Django. It works well when file sizes are small. If the file size is more than 1GB, it's showing memory error:
Traceback (most recent call last):
x-xxx-xx: File "/usr/lib64/python3.7/zipfile.py", line 1764, in write
x-xxx-xx: shutil.copyfileobj(src, dest, 1024*8)
x-xxx-xx: File "/usr/lib64/python3.7/shutil.py", line 82, in copyfileobj
x-xxx-xx: fdst.write(buf)
x-xxx-xx: File "/usr/lib64/python3.7/zipfile.py", line 1131, in write
x-xxx-xx: self._fileobj.write(data)
x-xxx-xx: MemoryError
x-xxx-xx: During handling of the above exception, another exception occurred:
x-xxx-xx: Traceback (most recent call last):
x-xxx-xx: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
x-xxx-xx: response = get_response(request)
x-xxx-xx: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
x-xxx-xx: response = self.process_exception_by_middleware(e, request)
x-xxx-xx: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
x-xxx-xx: response = wrapped_callback(request, *callback_args, **callback_kwargs)
x-xxx-xx: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
x-xxx-xx: response = view_func(request, *args, **kwargs)
x-xxx-xx: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
x-xxx-xx: response = view_func(request, *args, **kwargs)
x-xxx-xx: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 231, in inner
x-xxx-xx: return view(request, *args, **kwargs)
x-xxx-xx: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/views/decorators/cache.py", line 31, in _cache_controlled
x-xxx-xx: response = viewfunc(request, *args, **kw)
x-xxx-xx: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
x-xxx-xx: return view_func(request, *args, **kwargs)
x-xxx-xx: File "/var/app/current/fileupload/views.py", line 519, in downloadScanView
x-xxx-xx: zf.write(tmp.name, zip_path)
x-xxx-xx: File "/usr/lib64/python3.7/zipfile.py", line 1764, in write
x-xxx-xx: shutil.copyfileobj(src, dest, 1024*8)
x-xxx-xx: File "/usr/lib64/python3.7/zipfile.py", line 1143, in close
x-xxx-xx: self._fileobj.write(buf)
x-xxx-xx: ValueError: I/O operation on closed file.
This Django application is up and running in AWS. I understand that the problem arises because of insufficient memory allocation. I am using the following code to serve the files. Is it possible to pass the file content as a chunk to utilize the memory more efficiently? Any help would be appreciated.
zip_subdir = url.split('/')[-1]
zip_filename = zip_subdir + ".zip"
byte_stream = BytesIO()
zf = ZipFile(byte_stream, "w", zipfile.ZIP_DEFLATED)
for path in s3_file_path:
url = s3Client.generate_presigned_url('get_object', Params = {'Bucket': BUCKET_NAME, 'Key': path.key}, ExpiresIn = 100)
file_response = requests.get(url)
if file_response.status_code == 200:
try:
tmp = tempfile.NamedTemporaryFile()
tmp.name = path.key.split('/')[-1]
f1 = open(tmp.name, 'wb')
f1.write(file_response.content)
f1.close()
zip_path = os.path.join('/'.join(path.key.split('/')[1:-1]), tmp.name)
zf.write(tmp.name, zip_path)
finally:
os.remove(tmp.name)
zf.close()
response = HttpResponse(byte_stream.getvalue(), content_type="application/x-zip-compressed")
response['Content-Disposition'] = 'attachment; filename=%s' % zip_filename
return response

return int(value) ValueError: invalid literal for int() with base 10: 'Umi Falafel'

I have exported csv files from the database and would like to store the csv information in my django models. I am receiving the ValueError problem.
I have tried converting the string to an integer within my .py files
load_vendor_data.py
import csv
from polls.models import Vendors
with open('../data/csv/Vendors.csv') as cap:
reader = csv.reader(cap)
# i = 0
for row in reader:
vendors = Vendors(row[0],row[1],row[2])
vendors.save()
# i = i + 1
models.py
class Vendors(models.Model):
name = models.CharField(max_length=100)
location = models.CharField(max_length=100)
price_range = models.CharField(max_length=100)
def __str__(self):
return self.name
class Act(models.Model):
Name = models.CharField(max_length=100)
Stage = models.CharField(max_length=100)
Start_Time = models.TimeField()
End_Time = models.TimeField()
Date = models.DateField()
def __str__(self):
return self.name
Stacktrace
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\tawhi\project\2019-ca472-John-Tawhid\festimaps\polls\load_vendor_data.py", line 9, in <module>
vendors.save()
File "C:\Users\tawhi\project\cfehome\lib\site-packages\django\db\models\base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\tawhi\project\cfehome\lib\site-packages\django\db\models\base.py", line 837, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Users\tawhi\project\cfehome\lib\site-packages\django\db\models\base.py", line 904, in _save_table
forced_update)
File "C:\Users\tawhi\project\cfehome\lib\site-packages\django\db\models\base.py", line 934, in _do_update
filtered = base_qs.filter(pk=pk_val)
File "C:\Users\tawhi\project\cfehome\lib\site-packages\django\db\models\query.py", line 784, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\tawhi\project\cfehome\lib\site-packages\django\db\models\query.py", line 802, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\tawhi\project\cfehome\lib\site-packages\django\db\models\sql\query.py", line 1250, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\tawhi\project\cfehome\lib\site-packages\django\db\models\sql\query.py", line 1276, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "C:\Users\tawhi\project\cfehome\lib\site-packages\django\db\models\sql\query.py", line 1210, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Users\tawhi\project\cfehome\lib\site-packages\django\db\models\sql\query.py", line 1104, in build_lookup
return final_lookup(lhs, rhs)
File "C:\Users\tawhi\project\cfehome\lib\site-packages\django\db\models\lookups.py", line 24, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\tawhi\project\cfehome\lib\site-packages\django\db\models\lookups.py", line 74, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "C:\Users\tawhi\project\cfehome\lib\site-packages\django\db\models\fields\__init__.py", line 966, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'Umi Falafel'
I expect no errors when importing the csv files inside the django db
You should always use keyword arguments, not positional ones, when instantiating models.
vendors = Vendors(name=row[0], location=row[1], price_range=row[2])

Django/PIL: AttributeError: 'Image' object has no attribute 'read'

I'm testing example code validate image field (Django custom validation in model form for imagefield (max file size etc.)):
models.py
from django.db import models
from filer.fields.image import FilerImageField
class Event(models.Model):
# (...)
banner = FilerImageField(verbose_name='Banner')
admin.py
from django.contrib import admin
from django import forms
from PIL import Image
from .models import Event
class EventForm(forms.ModelForm):
class Meta:
model = Event
fields = '__all__'
def clean_banner(self):
image = self.cleaned_data.get('banner', False)
if image:
img = Image.open(image)
img.load()
width, height = img.size
max_width = 879
max_height = 392
if width > max_width or width < max_width or height > max_height or height < max_height:
raise forms.ValidationError('Imagem está com dimensão incorreta: %s x %s pixels. Insira uma imagem com %s x %s pixels.'
%(width, height, max_width, max_height))
# Máx 3MB
if len(image) > (3 * 1024 * 1024):
raise forms.ValidationError('Arquivo de imagem muito grande (máximo 3MB).')
name_type, ext = image.content_type.split('/')
if not (name_type == 'image' and ext.lower() in ['png', 'jpg', 'jpeg']):
raise forms.ValidationError('Por favor, use imagem em formato JPG, JPEG ou PNG.')
else:
raise forms.ValidationError('Não foi possível ler a imagem carregada.')
return image
error when registering image not django-admin. Version: Pillow==4.3.0, Python==3.6
Traceback (most recent call last):
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\PIL\Image.py", line 2534, in open
fp.seek(0)
AttributeError: 'Image' object has no attribute 'seek'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\contrib\admin\options.py", line 551, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\views\decorators\cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\contrib\admin\sites.py", line 224, in inner
return view(request, *args, **kwargs)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\contrib\admin\options.py", line 1508, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\utils\decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\contrib\admin\options.py", line 1408, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\contrib\admin\options.py", line 1440, in _changeform_view
if form.is_valid():
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\forms\forms.py", line 183, in is_valid
return self.is_bound and not self.errors
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\forms\forms.py", line 175, in errors
self.full_clean()
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\forms\forms.py", line 384, in full_clean
self._clean_fields()
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\django\forms\forms.py", line 405, in _clean_fields
value = getattr(self, 'clean_%s' % name)()
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\integrafundaj\events\admin.py", line 19, in clean_banner
img = Image.open(image)
File "C:\Users\Lidy Monteiro\Developer\integra-fundaj\.myvenv\lib\site-packages\PIL\Image.py", line 2536, in open
fp = io.BytesIO(fp.read())
AttributeError: 'Image' object has no attribute 'read'
I tried img = Image.open(image, mode='r') in function clean_banne, but it does not work. how to solve? the problem is in the python version?
According to Pillow documentation Python 3.6 is not yet supported. Please try another version of python.

Categories

Resources