Refactor Dockerfile and backend API for improved image processing and dependency management

This commit is contained in:
hlohaus 2025-10-31 03:52:10 +01:00
parent f571237eb7
commit 9b3e7a69d2
4 changed files with 9 additions and 22 deletions

View file

@ -3,7 +3,6 @@ FROM python:slim-bookworm
ARG G4F_VERSION
ARG G4F_USER=g4f
ARG G4F_USER_ID=1000
ARG PYDANTIC_VERSION=1.8.1
ENV G4F_VERSION $G4F_VERSION
ENV G4F_USER $G4F_USER
@ -37,25 +36,9 @@ COPY requirements-min.txt $G4F_DIR
COPY requirements-slim.txt $G4F_DIR
# Upgrade pip for the latest features and install the project's Python dependencies.
RUN pip install --no-cache-dir -r requirements-min.txt \
&& pip install --no-cache-dir --no-binary setuptools \
Cython==0.29.22 \
setuptools \
# Install PyDantic
&& pip install \
-vvv \
--no-cache-dir \
--no-binary :all: \
--global-option=build_ext \
--global-option=-j8 \
pydantic==${PYDANTIC_VERSION}
RUN pip install --no-cache-dir -r requirements-min.txt
RUN cat requirements-slim.txt | xargs -n 1 timeout 5m pip install --no-cache-dir || true
# Remove build packages
RUN pip uninstall --yes \
Cython \
setuptools
USER root
# Clean up build deps

View file

@ -21,7 +21,7 @@ from urllib.parse import quote_plus
from hashlib import sha256
try:
from PIL import Image
from PIL import Image, UnidentifiedImageError
has_pillow = True
except ImportError:
has_pillow = False
@ -452,7 +452,10 @@ class Backend_Api(Api):
image_size = {"width": width, "height": height}
thumbnail_dir = os.path.join(bucket_dir, "thumbnail")
os.makedirs(thumbnail_dir, exist_ok=True)
process_image(image, save=os.path.join(thumbnail_dir, filename))
width, height = process_image(image, save=os.path.join(thumbnail_dir, filename))
image_size = {"width": width, "height": height}
except UnidentifiedImageError:
pass
except Exception as e:
logger.exception(e)
if result:

View file

@ -341,7 +341,6 @@ def process_image(image: Image.Image, new_width: int = 400, new_height: int = 40
Image: The processed image.
"""
image = ImageOps.exif_transpose(image)
image.thumbnail((new_width, new_height))
# Remove transparency
if image.mode == "RGBA":
# image.load()
@ -352,8 +351,11 @@ def process_image(image: Image.Image, new_width: int = 400, new_height: int = 40
# Convert to RGB for jpg format
elif image.mode != "RGB":
image = image.convert("RGB")
image_size = image.size
image.thumbnail((new_width, new_height))
if save is not None:
image.save(save, exif=b"")
return image_size
return image
def to_bytes(image: ImageType) -> bytes:

View file

@ -14,7 +14,6 @@ flask
brotli
beautifulsoup4
setuptools
nodriver
python-multipart
a2wsgi
python-dotenv