rogerxavier commited on
Commit
1e2d513
1 Parent(s): 0aee47a

Create 1removeMask.py

Browse files
Files changed (1) hide show
  1. 1removeMask.py +107 -0
1removeMask.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+
3
+ import requests
4
+ import os
5
+ from PIL import Image
6
+ import io
7
+
8
+ def inpaint(img_path:str,mask_path:str)->"img content (resp.content)":
9
+ image_bytes = open(img_path, 'rb')
10
+ mask_bytes = open(mask_path, 'rb')
11
+ # 将字节数据转换为Base64编码的字符串
12
+
13
+ files = {
14
+ "image": image_bytes,
15
+ "mask":mask_bytes
16
+ }
17
+ payload = {
18
+ "ldmSteps": 25,
19
+ "ldmSampler": "plms",
20
+ "zitsWireframe": True,
21
+ "hdStrategy": "Crop",
22
+ "hdStrategyCropMargin": 196,
23
+ "hdStrategyCropTrigerSize": 800,
24
+ "hdStrategyResizeLimit": 2048,
25
+ "prompt": "",
26
+ "negativePrompt": "",
27
+ "croperX": 307,
28
+ "croperY": 544,
29
+ "croperHeight": 512,
30
+ "croperWidth": 512,
31
+ "useCroper": False,
32
+ "sdMaskBlur": 5,
33
+ "sdStrength": 0.75,
34
+ "sdSteps": 50,
35
+ "sdGuidanceScale": 7.5,
36
+ "sdSampler": "uni_pc",
37
+ "sdSeed": -1,
38
+ "sdMatchHistograms": False,
39
+ "sdScale": 1,
40
+ "cv2Radius": 5,
41
+ "cv2Flag": "INPAINT_NS",
42
+ "paintByExampleSteps": 50,
43
+ "paintByExampleGuidanceScale": 7.5,
44
+ "paintByExampleSeed": -1,
45
+ "paintByExampleMaskBlur": 5,
46
+ "paintByExampleMatchHistograms": False,
47
+ "p2pSteps": 50,
48
+ "p2pImageGuidanceScale": 1.5,
49
+ "p2pGuidanceScale": 7.5,
50
+ "controlnet_conditioning_scale": 0.4,
51
+ "controlnet_method": "control_v11p_sd15_canny"
52
+ }#payload用data
53
+
54
+ #不使用header
55
+
56
+ # resp = requests.post("https://sanster-lama-cleaner-lama.hf.space/inpaint",data= payload,files=files)##huggingface版本
57
+ resp = requests.post("https://sanster-lama-cleaner-lama.hf.space/inpaint", data=payload, files=files)
58
+ return resp.content
59
+
60
+ def save_img(img_content:"要处理的图片数据",new_save_path:"新文件的保存路径(包含后缀)",old_img_path:"旧文件路径")->"void生成新的文件保存 ,传入旧文件路径是为了删除有问题的旧文件":
61
+ print(new_save_path)
62
+ try:
63
+ img = Image.open(io.BytesIO(img_content))
64
+ # 如果需要指定图像格式,可以在保存时指定
65
+ img.save(new_save_path, format="JPEG")
66
+ except Exception as e:
67
+ #对于可能异常的图片->比如因为不合规导致resp.content没有正常返回
68
+ print(e,new_save_path,"图片返回有问题,跳过并删除图片")
69
+ os.remove(old_img_path)
70
+
71
+
72
+
73
+
74
+
75
+ if __name__ == '__main__':
76
+ # 获取当前目录的子目录的路径
77
+ img_path = 'manga'
78
+ subdir_path = os.path.join(os.getcwd(), img_path)
79
+
80
+ # 图片素材获取(包含子目录下所有图片)
81
+ image_files = []
82
+ for root, dirs, files in os.walk(subdir_path):
83
+ for file in files:
84
+ if file.endswith(".jpg") or file.endswith(".png"):
85
+ image_files.append(os.path.relpath(os.path.join(root, file)))
86
+
87
+ # 创建处理后的子目录在与image_files同级目录下
88
+ processed_subdir_path = os.path.join(os.path.dirname(subdir_path), f"{img_path}1")
89
+ os.makedirs(processed_subdir_path, exist_ok=True)
90
+
91
+ # 对image_files进行某种处理,生成新图片,并保存在处理后的子目录中
92
+ for img_file in image_files:
93
+ # 处理图片的代码(这里仅作示例)
94
+ # 假设处理后的图片为new_img
95
+ img_dir = os.path.dirname(img_file)
96
+ new_img_dir = os.path.join(processed_subdir_path, img_dir)
97
+ os.makedirs(new_img_dir, exist_ok=True)
98
+
99
+ new_img_path = os.path.join(new_img_dir, os.path.basename(img_file))
100
+
101
+ if not os.path.exists(new_img_path):
102
+ #如果已经处理过那么跳过
103
+ # 处理图片并保存
104
+ img_inpainted = inpaint(img_path=img_file, mask_path='all_mask.jpg')
105
+ save_img(img_content=img_inpainted, new_save_path=new_img_path,old_img_path=img_file)
106
+ else:
107
+ print(f"Skipping {new_img_path} as it already exists.")