initial commit
This commit is contained in:
29
Dockerfile
Normal file
29
Dockerfile
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
FROM python:3.11-slim
|
||||||
|
|
||||||
|
# Install system dependencies required for pyheif and libheif
|
||||||
|
RUN apt-get update && apt-get install -y \
|
||||||
|
libheif-dev \
|
||||||
|
libheif1 \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
# Set working directory
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy requirements file
|
||||||
|
COPY requirements.txt .
|
||||||
|
|
||||||
|
# Install Python dependencies
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
# Copy application code
|
||||||
|
COPY main.py .
|
||||||
|
|
||||||
|
# Expose port
|
||||||
|
EXPOSE 5000
|
||||||
|
|
||||||
|
# Set environment variables for Flask
|
||||||
|
ENV FLASK_APP=main.py
|
||||||
|
ENV FLASK_RUN_HOST=0.0.0.0
|
||||||
|
|
||||||
|
# Run the application
|
||||||
|
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "4", "main:app"]
|
||||||
35
main.py
Normal file
35
main.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
from flask import Flask, request, send_file
|
||||||
|
from PIL import Image
|
||||||
|
import pyheif
|
||||||
|
import io
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
@app.route('/convert-heic-to-jpg', methods=['POST'])
|
||||||
|
def convert_heic_to_jpg():
|
||||||
|
if 'file' not in request.files:
|
||||||
|
return "No file part", 400
|
||||||
|
file = request.files['file']
|
||||||
|
if file.filename == '':
|
||||||
|
return "No selected file", 400
|
||||||
|
if file and file.filename.lower().endswith('.heic'):
|
||||||
|
try:
|
||||||
|
heif_file = pyheif.read(file.stream)
|
||||||
|
image = Image.frombytes(
|
||||||
|
heif_file.mode,
|
||||||
|
heif_file.size,
|
||||||
|
heif_file.data,
|
||||||
|
"raw",
|
||||||
|
heif_file.mode,
|
||||||
|
heif_file.stride,
|
||||||
|
)
|
||||||
|
output_buffer = io.BytesIO()
|
||||||
|
image.save(output_buffer, format="JPEG")
|
||||||
|
output_buffer.seek(0)
|
||||||
|
return send_file(output_buffer, mimetype='image/jpeg', as_attachment=True, download_name='converted.jpg')
|
||||||
|
except Exception as e:
|
||||||
|
return f"Error converting file: {e}", 500
|
||||||
|
return "Invalid file format", 400
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(host='0.0.0.0', port=5000, debug=False)
|
||||||
4
requirements.txt
Normal file
4
requirements.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
Flask>=2.3.0
|
||||||
|
Pillow>=10.0.0
|
||||||
|
pyheif>=0.7.1
|
||||||
|
gunicorn>=21.2.0
|
||||||
Reference in New Issue
Block a user