hnmr commited on
Commit
58ed11a
·
1 Parent(s): 0ed62ea
Files changed (6) hide show
  1. .gitignore +10 -0
  2. .python-version +1 -0
  3. README.md +9 -7
  4. app.py +74 -0
  5. pyproject.toml +10 -0
  6. uv.lock +0 -0
.gitignore ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python-generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+
9
+ # Virtual environments
10
+ .venv
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.12
README.md CHANGED
@@ -1,12 +1,14 @@
1
  ---
2
- title: Imgcvt
3
- emoji: 🏢
4
- colorFrom: green
5
- colorTo: indigo
6
  sdk: gradio
 
7
  sdk_version: 5.20.1
 
 
8
  app_file: app.py
9
  pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: 画像変換器
3
+ emoji: 👸
4
+ colorFrom: gray
5
+ colorTo: pink
6
  sdk: gradio
7
+ python_version: 3.12
8
  sdk_version: 5.20.1
9
+ suggested_hardware: cpu-basic
10
+ suggested_storage: small
11
  app_file: app.py
12
  pinned: false
13
+ license: wtfpl
14
+ ---
 
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import tempfile
4
+ import os
5
+
6
+ def convert_image(input_image: Image.Image, output_format: str, quality: int | float):
7
+ """画像を指定した形式に変換する関数"""
8
+ if input_image is None:
9
+ return None, None
10
+
11
+ img = input_image
12
+
13
+ # PNGの場合はRGBA、それ以外はRGBに変換
14
+ if output_format.lower() == 'png':
15
+ if img.mode != 'RGBA' and 'A' not in img.mode:
16
+ img = img.convert('RGBA')
17
+ elif img.mode == 'RGBA':
18
+ img = img.convert('RGB')
19
+
20
+ # 保存
21
+
22
+ args = {}
23
+
24
+ fmt = output_format.lower()
25
+ quality = int(quality)
26
+
27
+ if "webp" in fmt:
28
+ lossless = "lossless" in fmt
29
+ fmt = "WEBP"
30
+ args["quality"] = quality
31
+ if lossless:
32
+ args["lossless"] = True
33
+ elif "jpeg" in fmt:
34
+ fmt = "JPEG"
35
+ args["quality"] = quality
36
+
37
+ # 一時ファイルを作成
38
+ with tempfile.NamedTemporaryFile(delete=False, suffix=f'.{fmt.lower()}') as temp_file:
39
+ temp_filename = temp_file.name
40
+
41
+ img.save(temp_filename, format=fmt, **args)
42
+
43
+ return img, temp_filename
44
+
45
+ # Gradioインターフェースの作成
46
+ with gr.Blocks(title="画像形式変換ツール") as demo:
47
+ gr.Markdown("## お嬢様の画像形式変換ツール")
48
+ gr.Markdown("画像をアップロードして、変換したい形式を選んでくださいませ。")
49
+
50
+ with gr.Row():
51
+ with gr.Column():
52
+ output_format = gr.Dropdown(
53
+ choices=["png", "webp (lossless)", "jpeg", "webp (lossy)"],
54
+ value="webp (lossless)",
55
+ label="変換後の形式"
56
+ )
57
+ quality = gr.Slider(0, 100, step=1, value=100, label="品質 (JPEG および WebP のみ)")
58
+ convert_btn = gr.Button("変換する")
59
+ input_image = gr.Image(type="pil", label="入力画像", format="png")
60
+
61
+ with gr.Column():
62
+ download_btn = gr.Button("ダウンロード")
63
+ download_file = gr.File(label="変換ファイル")
64
+ output_image = gr.Image(label="変換された画像")
65
+
66
+ convert_btn.click(
67
+ fn=convert_image,
68
+ inputs=[input_image, output_format, quality],
69
+ outputs=[output_image, download_file]
70
+ )
71
+
72
+ # 実行
73
+ if __name__ == "__main__":
74
+ demo.launch()
pyproject.toml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "imgconv"
3
+ version = "0.1.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ requires-python = ">=3.12"
7
+ dependencies = [
8
+ "gradio>=5.20.1",
9
+ "pillow>=11.1.0",
10
+ ]
uv.lock ADDED
The diff for this file is too large to render. See raw diff