juyam commited on
Commit
d347322
·
verified ·
1 Parent(s): 30825a5

updated the code

Browse files
Files changed (1) hide show
  1. app.py +35 -25
app.py CHANGED
@@ -3,46 +3,56 @@ from PIL import Image
3
  import torch
4
  from diffusers import StableDiffusionInpaintPipeline
5
 
6
- def expand_image(image, prompt):
7
- # デフォルトのネガティブプロンプト
8
- negative_prompt = "nsfw, nudity, sexual, violence, explicit, harmful"
 
 
9
 
10
- # ユーザーが入力したプロンプトにネガティブプロンプトを追加
11
- prompt = f"{prompt}, {negative_prompt}"
 
 
 
 
 
 
12
 
13
- # 画像が512x512より大きい場合、表示用にリサイズ
14
- max_size = 512
15
- if image.size[0] > max_size or image.size[1] > max_size:
16
- image_resized = image.resize((max_size, max_size))
17
- else:
18
- image_resized = image
19
 
20
  # 画像サイズ(幅)を2.5倍に拡張
21
- width, height = image.size
22
  new_width = int(width * 2.5)
 
 
 
 
23
 
24
- # 新しいキャンバスを作成し、元画像を中央に配置
25
- new_image = Image.new("RGB", (new_width, height), (255, 255, 255))
26
- new_image.paste(image, ((new_width - width) // 2, 0))
27
-
28
- # マスク画像を作成(元画像の外側を黒く)
29
- mask = Image.new("L", (new_width, height), 0)
30
- mask.paste(255, ((new_width - width) // 2, 0, (new_width + width) // 2, height))
31
 
32
- model_id = "runwayml/stable-diffusion-inpainting"
33
  pipe = StableDiffusionInpaintPipeline.from_pretrained(model_id, torch_dtype=torch.float32, use_safetensors=False)
34
- pipe.to("cpu") # GPUが使えないのでCPUに変更
35
 
 
 
 
 
36
  output = pipe(prompt=prompt, negative_prompt=negative_prompt, image=new_image, mask_image=mask).images[0]
37
 
38
- return image_resized, output # 画像をリサイズ後、結果とともに返す
 
 
 
39
 
 
40
  iface = gr.Interface(
41
  fn=expand_image,
42
- inputs=[gr.Image(type="pil"), gr.Textbox(label="プロンプトを入力")],
43
- outputs=[gr.Image(), gr.Image()],
44
  title="画像拡張ツール",
45
- description="入力画像の幅を2.5倍に拡張します。"
46
  )
47
 
48
  iface.launch()
 
3
  import torch
4
  from diffusers import StableDiffusionInpaintPipeline
5
 
6
+ def expand_image(image):
7
+ # 画像が256x256より大きい場合、表示用にリサイズ
8
+ max_size = 256 # 最大表示サイズ
9
+ width, height = image.size
10
+ aspect_ratio = width / height # アスペクト比
11
 
12
+ # アスペクト比を維持しながらリサイズ
13
+ if width > max_size or height > max_size:
14
+ if width > height:
15
+ # 横幅が大きい場合、幅をmax_sizeに合わせて高さを自動で調整
16
+ image = image.resize((max_size, int(max_size / aspect_ratio)))
17
+ else:
18
+ # 縦幅が大きい場合、高さをmax_sizeに合わせて幅を自動で調整
19
+ image = image.resize((int(max_size * aspect_ratio), max_size))
20
 
21
+ print(f'{image.size=}') # リサイズ後の画像サイズを確認
 
 
 
 
 
22
 
23
  # 画像サイズ(幅)を2.5倍に拡張
 
24
  new_width = int(width * 2.5)
25
+
26
+ # 新しいキャンバスを白で作成し、元画像を中央に配置
27
+ new_image = Image.new("RGB", (new_width, height), (0, 0, 0)) # 黒背景
28
+ new_image.paste(image, ((new_width - width) // 2, 0)) # 元画像を中央に配置
29
 
30
+ # マスク画像を作成(変更する部分を白、変更しない部分を黒)
31
+ mask = Image.new("L", (new_width, height), 255) # 変更する部分を白
32
+ mask.paste(0, ((new_width - width) // 2, 0, (new_width + width) // 2, height)) # 元画像部分を黒 (変更しない部分)
 
 
 
 
33
 
34
+ model_id = "runwayml/stable-diffusion-inpainting"# Stable Diffusion Inpainting パイプラインの準備
35
  pipe = StableDiffusionInpaintPipeline.from_pretrained(model_id, torch_dtype=torch.float32, use_safetensors=False)
36
+ pipe.to("cpu") # huggingface は GPUが使えないのでCPUに変更
37
 
38
+ prompt = "retain the original image, Extend the current image by adding the most probable realistic elements based on the original content"
39
+ negative_prompt = "repeating, tiling, nsfw, nudity, sexual, violence, explicit, harmful"
40
+
41
+ # Inpainting 処理を実行
42
  output = pipe(prompt=prompt, negative_prompt=negative_prompt, image=new_image, mask_image=mask).images[0]
43
 
44
+ # 画像を保存して確認
45
+ output.save("E:/jamad_files/Temp_generated_image.png")
46
+
47
+ return output # 画像をリサイズ後、結果とともに返す
48
 
49
+ # Gradioインターフェースの作成
50
  iface = gr.Interface(
51
  fn=expand_image,
52
+ inputs=gr.Image(type="pil"),
53
+ outputs=gr.Image(),
54
  title="画像拡張ツール",
55
+ description="入力画像の幅を2.5倍に拡張します。 WIP yet"
56
  )
57
 
58
  iface.launch()