python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.32k stars 2.23k forks source link

Memory error #8480

Closed paulocoutinhox closed 1 month ago

paulocoutinhox commented 1 month ago

What did you do?

Im trying upload an image from my phone or local file and im get MemoryError.

It work when use runserver but memory error when use inside docker, on local docker, on hostinger docker, on fly.io etc, the same error happen on Pillow. Only on runserver mode it works.

What did you expect to happen?

Upload without problem.

What actually happened?

Traceback (most recent call last):
  File "/app/.local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/app/.local/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/app/.local/lib/python3.10/site-packages/django/contrib/auth/decorators.py", line 60, in _view_wrapper
    return view_func(request, *args, **kwargs)
  File "/app/./apps/web/views/ai/flux_model.py", line 259, in flux_model_upload_photos_view
    photo.save()
  File "/app/.local/lib/python3.10/site-packages/django/db/models/base.py", line 891, in save
    self.save_base(
  File "/app/.local/lib/python3.10/site-packages/django/db/models/base.py", line 997, in save_base
    updated = self._save_table(
  File "/app/.local/lib/python3.10/site-packages/django/db/models/base.py", line 1160, in _save_table
    results = self._do_insert(
  File "/app/.local/lib/python3.10/site-packages/django/db/models/base.py", line 1201, in _do_insert
    return manager._insert(
  File "/app/.local/lib/python3.10/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/app/.local/lib/python3.10/site-packages/django/db/models/query.py", line 1847, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/app/.local/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1835, in execute_sql
    for sql, params in self.as_sql():
  File "/app/.local/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1758, in as_sql
    value_rows = [
  File "/app/.local/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1759, in <listcomp>
    [
  File "/app/.local/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1760, in <listcomp>
    self.prepare_value(field, self.pre_save_val(field, obj))
  File "/app/.local/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1708, in pre_save_val
    return field.pre_save(obj, add=True)
  File "/app/.local/lib/python3.10/site-packages/django/db/models/fields/files.py", line 338, in pre_save
    file.save(file.name, file.file, save=False)
  File "/app/.local/lib/python3.10/site-packages/django_resized/forms.py", line 88, in save
    img = normalize_rotation(img)
  File "/app/.local/lib/python3.10/site-packages/django_resized/forms.py", line 53, in normalize_rotation
    image = image.rotate(270, expand=True)
  File "/app/.local/lib/python3.10/site-packages/PIL/Image.py", line 2409, in rotate
    return self.transpose(
  File "/app/.local/lib/python3.10/site-packages/PIL/Image.py", line 2960, in transpose
    return self._new(self.im.transpose(method))
MemoryError

What are your OS, Python and Pillow versions?

docker exec -it cool_pascal python3 -m PIL.report
--------------------------------------------------------------------
Pillow 11.0.0
Python 3.10.15 (main, Sep 27 2024, 21:42:47) [GCC 12.2.0]
--------------------------------------------------------------------
Python executable is /usr/local/bin/python3
System Python files loaded from /usr/local
--------------------------------------------------------------------
Python Pillow modules loaded from /app/.local/lib/python3.10/site-packages/PIL
Binary Pillow modules loaded from /app/.local/lib/python3.10/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 11.0.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.13.2
--- LITTLECMS2 support ok, loaded 2.16
--- WEBP support ok, loaded 1.4.0
--- JPEG support ok, compiled for libjpeg-turbo 3.0.4
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.2
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.13
--- LIBTIFF support ok, loaded 4.6.0
--- RAQM (Bidirectional Text) support ok, loaded 0.10.1, fribidi 1.0.8, harfbuzz 10.0.1
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------
radarhere commented 1 month ago

Are you able to provide a simple Dockerfile that reproduces it, so that we can also trigger the MemoryError?

paulocoutinhox commented 1 month ago

Hi,

Dockerfile: https://github.com/paulocoutinhox/pyaa

I discover the problem. The file uwsgi.ini is limited to 256mb, so is not a problem with docker or Dockerfile.

Thanks.

paulocoutinhox commented 1 month ago

I use 512mb and it solve the problem: https://github.com/paulocoutinhox/pyaa/blob/main/uwsgi.ini#L17