namelessai commited on
Commit
73c3107
·
verified ·
1 Parent(s): 5eed002

fix value error

Browse files
Files changed (1) hide show
  1. app.py +24 -18
app.py CHANGED
@@ -26,30 +26,32 @@ format_options = {
26
  }
27
 
28
 
29
- def convert_image(img_file, output_format,
30
- quality, compress_level,
31
- jpeg2000_layers, avif_speed,
32
- subsampling, professional_lossless):
33
- # img_file is now a path (string or Path)
 
 
34
  img_path = Path(img_file)
35
  img_bytes = img_path.read_bytes()
36
 
37
  # Load image and convert to RGB
38
  image = Image.open(io.BytesIO(img_bytes)).convert("RGB")
39
 
40
- save_kwargs = {}
41
  fmt = output_format
 
42
 
43
  if professional_lossless:
44
  # Force true lossless: use PNG
45
  fmt = "PNG"
46
  save_kwargs.update({"compress_level": 9, "optimize": True})
47
  else:
48
- # Base defaults
49
- opts = format_options.get(fmt, {})
50
- save_kwargs.update(opts)
51
 
52
- # Override with specialized controls
53
  if fmt == "JPEG":
54
  save_kwargs["quality"] = quality
55
  elif fmt == "WEBP":
@@ -67,31 +69,35 @@ def convert_image(img_file, output_format,
67
  save_kwargs["quality"] = quality
68
  save_kwargs["subsampling"] = subsampling
69
 
 
70
  buf = io.BytesIO()
71
  image.save(buf, format=fmt, **save_kwargs)
72
  buf.seek(0)
73
  return buf.getvalue()
74
 
75
 
76
- # Build Gradio app
77
  def build_app():
78
  with gr.Blocks() as demo:
79
  gr.Markdown("# Advanced Image Converter")
80
 
81
  with gr.Row():
82
- # Return a filesystem path instead of raw bytes
83
- inp = gr.File(label="Upload Image", file_types=["image"], type="file")
84
  out = gr.File(label="Download Converted")
85
 
86
  with gr.Row():
87
- fmt = gr.Dropdown(choices=offered_formats, value="PNG", label="Output Format")
88
  with gr.Row():
89
- quality = gr.Slider(1, 100, value=85, step=1, label="Quality (JPEG/AVIF/HEIF/WEBP)")
90
- compress = gr.Slider(0, 9, value=6, step=1, label="PNG Compress Level")
 
 
91
  with gr.Row():
92
- jpeg2000_layers = gr.Textbox(value="25,50,75", label="JPEG2000 Quality Layers (comma-separated)")
 
93
  avif_speed = gr.Slider(0, 8, value=4, step=1, label="AVIF Speed")
94
- subsample = gr.Dropdown(choices=["4:4:4", "4:2:2", "4:2:0"], value="4:2:0", label="HEIF Subsampling")
 
95
  professional = gr.Checkbox(label="Professional Lossless Mode",
96
  info="Force highest-quality lossless conversion as optimized PNG")
97
  convert_btn = gr.Button("Convert")
 
26
  }
27
 
28
 
29
+ def convert_image(
30
+ img_file, output_format,
31
+ quality, compress_level,
32
+ jpeg2000_layers, avif_speed,
33
+ subsampling, professional_lossless
34
+ ):
35
+ # img_file is a filesystem path (string)
36
  img_path = Path(img_file)
37
  img_bytes = img_path.read_bytes()
38
 
39
  # Load image and convert to RGB
40
  image = Image.open(io.BytesIO(img_bytes)).convert("RGB")
41
 
42
+ # Prepare save kwargs
43
  fmt = output_format
44
+ save_kwargs = {}
45
 
46
  if professional_lossless:
47
  # Force true lossless: use PNG
48
  fmt = "PNG"
49
  save_kwargs.update({"compress_level": 9, "optimize": True})
50
  else:
51
+ # Base defaults for chosen format
52
+ save_kwargs.update(format_options.get(fmt, {}))
 
53
 
54
+ # Overrides per-format
55
  if fmt == "JPEG":
56
  save_kwargs["quality"] = quality
57
  elif fmt == "WEBP":
 
69
  save_kwargs["quality"] = quality
70
  save_kwargs["subsampling"] = subsampling
71
 
72
+ # Save into a bytes buffer
73
  buf = io.BytesIO()
74
  image.save(buf, format=fmt, **save_kwargs)
75
  buf.seek(0)
76
  return buf.getvalue()
77
 
78
 
 
79
  def build_app():
80
  with gr.Blocks() as demo:
81
  gr.Markdown("# Advanced Image Converter")
82
 
83
  with gr.Row():
84
+ # Use 'filepath' so Gradio returns a local file path string
85
+ inp = gr.File(label="Upload Image", file_types=["image"], type="filepath")
86
  out = gr.File(label="Download Converted")
87
 
88
  with gr.Row():
89
+ fmt = gr.Dropdown(offered_formats, value="PNG", label="Output Format")
90
  with gr.Row():
91
+ quality = gr.Slider(1, 100, value=85, step=1,
92
+ label="Quality (JPEG/AVIF/HEIF/WEBP)")
93
+ compress = gr.Slider(0, 9, value=6, step=1,
94
+ label="PNG Compress Level")
95
  with gr.Row():
96
+ jpeg2000_layers = gr.Textbox(value="25,50,75",
97
+ label="JPEG2000 Quality Layers (comma-separated)")
98
  avif_speed = gr.Slider(0, 8, value=4, step=1, label="AVIF Speed")
99
+ subsample = gr.Dropdown(["4:4:4", "4:2:2", "4:2:0"], value="4:2:0",
100
+ label="HEIF Subsampling")
101
  professional = gr.Checkbox(label="Professional Lossless Mode",
102
  info="Force highest-quality lossless conversion as optimized PNG")
103
  convert_btn = gr.Button("Convert")