r3hab commited on
Commit
f3fb9be
·
verified ·
1 Parent(s): 9d7f2a6

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +84 -0
  3. requirements.txt +3 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ RUN useradd -m -u 1000 user
7
+ USER user
8
+ ENV PATH="/home/user/.local/bin:$PATH"
9
+
10
+ WORKDIR /app
11
+
12
+ COPY --chown=user ./requirements.txt requirements.txt
13
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
+
15
+ COPY --chown=user . /app
16
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from PIL import Image
3
+ import io
4
+ import os
5
+ from typing import List, Optional
6
+
7
+ app = FastAPI()
8
+
9
+ # Supported output formats and their respective PIL modes (if applicable)
10
+ supported_formats = {
11
+ "png": "PNG",
12
+ "jpg": "JPEG",
13
+ "jpeg": "JPEG",
14
+ "webp": "WEBP",
15
+ "gif": "GIF", # Note: GIF conversion may lose animation if the input isn't GIF
16
+ "bmp": "BMP",
17
+ "tiff": "TIFF",
18
+ "pdf": None, # PDF conversion handled differently
19
+ }
20
+
21
+
22
+
23
+ async def convert_image(image: UploadFile, output_format: str, resize: Optional[List[int]] = None):
24
+
25
+ try:
26
+ img_bytes = await image.read() # Read image bytes
27
+ img = Image.open(io.BytesIO(img_bytes))
28
+
29
+ if resize: # Resize if requested
30
+ img = img.resize(tuple(resize))
31
+
32
+
33
+ output_buffer = io.BytesIO()
34
+
35
+ if output_format.lower() == "pdf":
36
+ img.save(output_buffer, "PDF", resolution=300.0) #Higher resolution PDF
37
+ content_type = "application/pdf"
38
+ filename = f"{image.filename.split('.')[0]}.pdf"
39
+
40
+ elif output_format.lower() in supported_formats :
41
+
42
+ mode = supported_formats[output_format.lower()]
43
+
44
+ # Convert image mode if necessary (e.g., for converting to grayscale)
45
+ # Example usage: to convert a PNG to a grayscale JPEG
46
+ # if mode:
47
+ # if img.mode != 'L': #L is for grayscale
48
+ # img = img.convert('L')
49
+
50
+ img.save(output_buffer, mode)
51
+ content_type = f"image/{output_format.lower()}" # Correct content type
52
+ filename = f"{image.filename.split('.')[0]}.{output_format.lower()}"
53
+ else:
54
+ return {"error": "Unsupported output format"}
55
+
56
+
57
+ return {
58
+ "filename": filename,
59
+ "file": output_buffer.getvalue(), # Return bytes
60
+ "content_type": content_type,
61
+ }
62
+
63
+
64
+
65
+ except Exception as e: # Handle errors like invalid image format
66
+ print(f"Error converting image: {e}")
67
+ return {"error": f"Error converting image: {e}"}
68
+
69
+
70
+ @app.post("/convert")
71
+ async def convert_image_endpoint(
72
+ image: UploadFile = File(...),
73
+ output_format: str = "png",
74
+ resize: Optional[List[int]] = None, #Example: ?resize=200&resize=300
75
+ ):
76
+
77
+ return await convert_image(image, output_format, resize)
78
+
79
+
80
+
81
+
82
+ @app.get("/")
83
+ async def root():
84
+ return {"message": "Advanced Image Converter"}
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ pillow