grenerate_main_image_test.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. import os
  2. import copy
  3. import time
  4. from .image_deal_base_func import *
  5. from PIL import Image, ImageDraw
  6. from blend_modes import multiply
  7. import os
  8. import settings
  9. from functools import wraps
  10. from .multi_threaded_image_saving import ImageSaver
  11. from .get_mask_by_green import GetMask
  12. from middleware import UnicornException
  13. def time_it(func):
  14. @wraps(func) # 使用wraps来保留原始函数的元数据信息
  15. def wrapper(*args, **kwargs):
  16. start_time = time.time() # 记录开始时间
  17. result = func(*args, **kwargs) # 调用原始函数
  18. end_time = time.time() # 记录结束时间
  19. print(
  20. f"Executing {func.__name__} took {end_time - start_time:.4f} seconds."
  21. ) # 打印耗时
  22. return result
  23. return wrapper
  24. class GeneratePic(object):
  25. def __init__(self, is_test=False):
  26. # self.logger = MyLogger()
  27. self.is_test = is_test
  28. self.saver = ImageSaver()
  29. pass
  30. @time_it
  31. def get_mask_and_config(self, im_jpg: Image, im_png: Image, curve_mask: bool):
  32. """
  33. 步骤:
  34. 1、尺寸进行对应缩小
  35. 2、查找并设定鞋底阴影蒙版
  36. 3、自动色阶检查亮度
  37. 4、输出自动色阶参数、以及放大的尺寸蒙版
  38. """
  39. # ===================尺寸进行对应缩小(提升处理速度)
  40. im_jpg = to_resize(im_jpg, width=800)
  41. im_png = to_resize(im_png, width=800)
  42. x1, y1, x2, y2 = im_png.getbbox()
  43. cv2_png = pil_to_cv2(im_png)
  44. # =====================设定鞋底阴影图的蒙版
  45. # 查找每列的最低非透明点
  46. min_y_values = find_lowest_non_transparent_points(cv2_png)
  47. # 在鞋底最低处增加一条直线蒙版,蒙版宽度为有效区域大小
  48. image_high = im_jpg.height
  49. print("图片高度:", image_high)
  50. cv2_jpg = pil_to_cv2(im_jpg)
  51. # 返回线条图片,以及最低位置
  52. print("返回线条图片,以及最低位置")
  53. # crop_image_box=(x1, y1, x2, y2),
  54. if curve_mask:
  55. crop_image_box = None
  56. else:
  57. # 不需要曲线部分的蒙版
  58. crop_image_box = (x1, y1, x2, y2)
  59. img_with_shifted_line, lowest_y = draw_shifted_line(
  60. image=cv2_jpg,
  61. min_y_values=min_y_values,
  62. shift_amount=15,
  63. one_line_pos=(x1, x2),
  64. line_color=(0, 0, 0),
  65. line_thickness=20,
  66. app=None,
  67. crop_image_box=crop_image_box,
  68. )
  69. print("66 制作蒙版")
  70. # 制作蒙版
  71. mask_line = cv2_to_pil(img_with_shifted_line)
  72. mask = mask_line.convert("L") # 转换为灰度图
  73. mask = ImageOps.invert(mask)
  74. # 蒙版扩边
  75. print("72 蒙版扩边")
  76. # 默认expansion_radius 65 blur_radius 45
  77. mask = expand_or_shrink_mask(
  78. pil_image=mask, expansion_radius=50, blur_radius=35
  79. )
  80. # =============使用绿色蒙版进行处理
  81. if settings.IS_GET_GREEN_MASK:
  82. print("============使用绿色蒙版进行处理")
  83. mask = mask.convert("RGB")
  84. white_bg = Image.new(mode="RGB", size=im_png.size, color=(0, 0, 0))
  85. green_areas_mask_pil = GetMask().find_green_areas(cv2_jpg)
  86. green_areas_mask_pil = expand_or_shrink_mask(
  87. pil_image=green_areas_mask_pil, expansion_radius=15, blur_radius=5
  88. )
  89. mask.paste(white_bg, mask=green_areas_mask_pil.convert("L"))
  90. mask = mask.convert("L")
  91. # ====================生成新的图片
  92. print("84 生成新的图片")
  93. bg = Image.new(mode="RGBA", size=im_png.size, color=(255, 255, 255, 255))
  94. bg.paste(im_png, mask=im_png)
  95. bg.paste(im_jpg, mask=mask) # 粘贴有阴影的地方
  96. if image_high > y2 + 20:
  97. lowest_y = y2 + 20
  98. if self.is_test:
  99. _bg = bg.copy()
  100. draw = ImageDraw.Draw(_bg)
  101. # 定义直线的起点和终点坐标
  102. start_point = (0, lowest_y) # 直线的起始点
  103. end_point = (_bg.width, lowest_y) # 直线的结束点
  104. # 定义直线的颜色(R, G, B)
  105. line_color = (255, 0, 0) # 红色
  106. _r = Image.new(mode="RGBA", size=im_png.size, color=(246, 147, 100, 255))
  107. # mask_line = mask_line.convert('L') # 转换为灰度图
  108. # mask_line = ImageOps.invert(mask_line)
  109. # _bg.paste(_r, mask=mask)
  110. # 绘制直线
  111. draw.line([start_point, end_point], fill=line_color, width=1)
  112. _bg.show()
  113. # bg.save(r"C:\Users\gymmc\Desktop\data\bg.png")
  114. # bg.show()
  115. # ==================自动色阶处理======================
  116. # 对上述拼接后的图片进行自动色阶处理
  117. bg = bg.convert("RGB")
  118. _im = cv2.cvtColor(np.asarray(bg), cv2.COLOR_RGB2BGR)
  119. # 背景阴影
  120. im_shadow = cv2.cvtColor(_im, cv2.COLOR_BGR2GRAY)
  121. print("image_high lowest_y", image_high, lowest_y)
  122. if lowest_y < 0 or lowest_y >= image_high:
  123. lowest_y = image_high - 1
  124. print("image_high lowest_y", image_high, lowest_y)
  125. rows = [lowest_y] # 需要检查的像素行
  126. print("copy.copy(im_shadow)")
  127. _im_shadow = copy.copy(im_shadow)
  128. Midtones = 0.7
  129. Highlight = 235
  130. k = copy.copy(settings.COLOR_GRADATION_CYCLES)
  131. print("循环识别")
  132. xunhuan = 0
  133. while k:
  134. xunhuan += 1
  135. # if settings.app:
  136. # settings.app.processEvents()
  137. k -= 1
  138. Midtones += 0.035
  139. if Midtones > 1.7:
  140. Midtones = 1.7
  141. Highlight -= 3
  142. _im_shadow = levels_adjust(
  143. img=im_shadow,
  144. Shadow=0,
  145. Midtones=Midtones,
  146. Highlight=Highlight,
  147. OutShadow=0,
  148. OutHighlight=255,
  149. Dim=3,
  150. )
  151. brightness_list = calculate_average_brightness_opencv(
  152. img_gray=_im_shadow, rows_to_check=rows
  153. )
  154. print(
  155. "循环识别:{},Midtones:{},Highlight:{},brightness_list:{}".format(
  156. xunhuan, Midtones, Highlight, brightness_list
  157. )
  158. )
  159. if brightness_list[0] >= settings.GRENERATE_MAIN_PIC_BRIGHTNESS:
  160. break
  161. im_shadow = cv2_to_pil(_im_shadow)
  162. # ========================================================
  163. # 计算阴影的亮度,用于确保阴影不要太黑
  164. # 1、图片预处理,只保留阴影
  165. only_shadow_img = im_shadow.copy()
  166. only_shadow_img.paste(
  167. Image.new(
  168. mode="RGBA", size=only_shadow_img.size, color=(255, 255, 255, 255)
  169. ),
  170. mask=im_png,
  171. )
  172. average_brightness = calculated_shadow_brightness(only_shadow_img)
  173. print("average_brightness:", average_brightness)
  174. config = {
  175. "Midtones": Midtones,
  176. "Highlight": Highlight,
  177. "average_brightness": average_brightness,
  178. }
  179. return mask, config
  180. def get_mask_and_config_1_2025_05_18(self, im_jpg: Image, im_png: Image):
  181. """
  182. 步骤:
  183. 1、尺寸进行对应缩小
  184. 2、查找并设定鞋底阴影蒙版
  185. 3、自动色阶检查亮度
  186. 4、输出自动色阶参数、以及放大的尺寸蒙版
  187. """
  188. # ===================尺寸进行对应缩小(提升处理速度)
  189. im_jpg = to_resize(im_jpg, width=800)
  190. im_png = to_resize(im_png, width=800)
  191. x1, y1, x2, y2 = im_png.getbbox()
  192. cv2_png = pil_to_cv2(im_png)
  193. # =====================设定鞋底阴影图的蒙版
  194. # 查找每列的最低非透明点
  195. min_y_values = find_lowest_non_transparent_points(cv2_png)
  196. # 在鞋底最低处增加一条直线蒙版,蒙版宽度为有效区域大小
  197. image_high = im_jpg.height
  198. print("图片高度:", image_high)
  199. cv2_jpg = pil_to_cv2(im_jpg)
  200. # 返回线条图片,以及最低位置
  201. print("返回线条图片,以及最低位置")
  202. img_with_shifted_line, lowest_y = draw_shifted_line(
  203. image=cv2_jpg,
  204. min_y_values=min_y_values,
  205. shift_amount=15,
  206. one_line_pos=(x1, x2),
  207. line_color=(0, 0, 0),
  208. line_thickness=20,
  209. app=None,
  210. crop_image_box=(x1, y1, x2, y2),
  211. )
  212. print("66 制作蒙版")
  213. # 制作蒙版
  214. mask_line = cv2_to_pil(img_with_shifted_line)
  215. mask = mask_line.convert("L") # 转换为灰度图
  216. mask = ImageOps.invert(mask)
  217. # 蒙版扩边
  218. print("72 蒙版扩边")
  219. # 默认expansion_radius 65 blur_radius 45
  220. mask = expand_or_shrink_mask(
  221. pil_image=mask, expansion_radius=50, blur_radius=35
  222. )
  223. # mask1 = expand_mask(mask, expansion_radius=30, blur_radius=10)
  224. # mask1.save("mask1.png")
  225. # mask2 = expand_or_shrink_mask(pil_image=mask, expansion_radius=60, blur_radius=30)
  226. # mask2.save("mask2.png")
  227. # raise 11
  228. # ====================生成新的图片
  229. print("84 生成新的图片")
  230. bg = Image.new(mode="RGBA", size=im_png.size, color=(255, 255, 255, 255))
  231. bg.paste(im_png, mask=im_png)
  232. bg.paste(im_jpg, mask=mask) # 粘贴有阴影的地方
  233. if self.is_test:
  234. _bg = bg.copy()
  235. draw = ImageDraw.Draw(_bg)
  236. # 定义直线的起点和终点坐标
  237. start_point = (0, lowest_y) # 直线的起始点
  238. end_point = (_bg.width, lowest_y) # 直线的结束点
  239. # 定义直线的颜色(R, G, B)
  240. line_color = (255, 0, 0) # 红色
  241. # 绘制直线
  242. draw.line([start_point, end_point], fill=line_color, width=1)
  243. # mask.show()
  244. # bg = pil_to_cv2(bg)
  245. # cv2.line(bg, (x1, lowest_y + 5), (x2, lowest_y + 5), color=(0, 0, 0),thickness=2)
  246. # bg = cv2_to_pil(bg)
  247. _r = Image.new(mode="RGBA", size=im_png.size, color=(246, 147, 100, 255))
  248. mask_line = mask_line.convert("L") # 转换为灰度图
  249. mask_line = ImageOps.invert(mask_line)
  250. _bg.paste(_r, mask=mask)
  251. _bg.show()
  252. # bg.save(r"C:\Users\gymmc\Desktop\data\bg.png")
  253. # bg.show()
  254. # ==================自动色阶处理======================
  255. # 对上述拼接后的图片进行自动色阶处理
  256. bg = bg.convert("RGB")
  257. _im = cv2.cvtColor(np.asarray(bg), cv2.COLOR_RGB2BGR)
  258. # 背景阴影
  259. im_shadow = cv2.cvtColor(_im, cv2.COLOR_BGR2GRAY)
  260. print("image_high lowest_y", image_high, lowest_y)
  261. if lowest_y < 0 or lowest_y >= image_high:
  262. lowest_y = image_high - 1
  263. print("image_high lowest_y", image_high, lowest_y)
  264. rows = [lowest_y] # 需要检查的像素行
  265. print("copy.copy(im_shadow)")
  266. _im_shadow = copy.copy(im_shadow)
  267. Midtones = 0.7
  268. Highlight = 235
  269. k = 12
  270. print("循环识别")
  271. while k:
  272. print("循环识别:{}".format(k))
  273. # if settings.app:
  274. # settings.app.processEvents()
  275. k -= 1
  276. Midtones += 0.1
  277. if Midtones > 1:
  278. Midtones = 1
  279. Highlight -= 3
  280. _im_shadow = levels_adjust(
  281. img=im_shadow,
  282. Shadow=0,
  283. Midtones=Midtones,
  284. Highlight=Highlight,
  285. OutShadow=0,
  286. OutHighlight=255,
  287. Dim=3,
  288. )
  289. brightness_list = calculate_average_brightness_opencv(
  290. img_gray=_im_shadow, rows_to_check=rows
  291. )
  292. print(brightness_list)
  293. if brightness_list[0] >= settings.GRENERATE_MAIN_PIC_BRIGHTNESS:
  294. break
  295. print("Midtones,Highlight:", Midtones, Highlight)
  296. im_shadow = cv2_to_pil(_im_shadow)
  297. # ========================================================
  298. # 计算阴影的亮度,用于确保阴影不要太黑
  299. # 1、图片预处理,只保留阴影
  300. only_shadow_img = im_shadow.copy()
  301. only_shadow_img.paste(
  302. Image.new(
  303. mode="RGBA", size=only_shadow_img.size, color=(255, 255, 255, 255)
  304. ),
  305. mask=im_png,
  306. )
  307. average_brightness = calculated_shadow_brightness(only_shadow_img)
  308. print("average_brightness:", average_brightness)
  309. config = {
  310. "Midtones": Midtones,
  311. "Highlight": Highlight,
  312. "average_brightness": average_brightness,
  313. }
  314. return mask, config
  315. def my_test(self, **kwargs):
  316. if "output_queue" in kwargs:
  317. output_queue = kwargs["output_queue"]
  318. else:
  319. output_queue = None
  320. time.sleep(3)
  321. if output_queue is not None:
  322. output_queue.put(True)
  323. @time_it
  324. def run(
  325. self,
  326. image_path,
  327. cut_image_path,
  328. out_path,
  329. image_deal_mode=0,
  330. image_index=99,
  331. out_pic_size=1024,
  332. is_logo=True,
  333. out_process_path_1=None,
  334. out_process_path_2=None,
  335. resize_mode=None,
  336. max_box=None,
  337. logo_path="",
  338. curve_mask=False,
  339. **kwargs,
  340. ): # im 为cv对象
  341. """
  342. image_path:原始图
  343. cut_image_path:抠图结果 与原始图尺寸相同
  344. out_path:输出主图路径
  345. image_deal_mode:图片处理模式,1表示需要镜像处理
  346. image_index:图片顺序索引
  347. out_pic_size:输出图片宽度大小
  348. is_logo=True 是否要添加logo水印
  349. out_process_path_1=None, 有阴影的图片,白底非透明
  350. out_process_path_2=None, 已抠图的图片
  351. resize_mode=0,1,2 主体缩小尺寸
  352. curve_mask 为True时,表示为对鞋曲线部分的mask,不做剪裁
  353. """
  354. if "output_queue" in kwargs:
  355. output_queue = kwargs["output_queue"]
  356. else:
  357. output_queue = None
  358. # ==========先进行剪切原图
  359. _s = time.time()
  360. orign_im = Image.open(image_path) # 原始图
  361. print("242 need_time_1:{}".format(time.time() - _s))
  362. orign_x, orign_y = orign_im.size
  363. cut_image = Image.open(cut_image_path) # 原始图的已扣图
  364. cut_image, new_box = get_mini_crop_img(img=cut_image)
  365. im_shadow = orign_im.crop(new_box) # 切图
  366. new_x, new_y = im_shadow.size
  367. # ================自动色阶处理
  368. _s = time.time()
  369. shadow_mask, config = self.get_mask_and_config(
  370. im_jpg=im_shadow, im_png=cut_image, curve_mask=curve_mask
  371. )
  372. print("242 need_time_2:{}".format(time.time() - _s))
  373. shadow_mask = shadow_mask.resize(im_shadow.size)
  374. # =====抠图,形成新的阴影背景图=====
  375. _new_im_shadow = Image.new(
  376. mode="RGBA", size=im_shadow.size, color=(255, 255, 255, 255)
  377. )
  378. _new_im_shadow.paste(im_shadow, mask=shadow_mask) # 粘贴有阴影的地方
  379. # _new_im_shadow.show()
  380. _new_im_shadow = pil_to_cv2(_new_im_shadow)
  381. _new_im_shadow = cv2.cvtColor(_new_im_shadow, cv2.COLOR_BGR2GRAY)
  382. _new_im_shadow = levels_adjust(
  383. img=_new_im_shadow,
  384. Shadow=0,
  385. Midtones=config["Midtones"],
  386. Highlight=config["Highlight"],
  387. OutShadow=0,
  388. OutHighlight=255,
  389. Dim=3,
  390. )
  391. im_shadow = cv2_to_pil(_new_im_shadow)
  392. # ================处理阴影的亮度==================
  393. average_brightness = config["average_brightness"]
  394. if config["average_brightness"] < 180:
  395. # 调整阴影亮度
  396. backdrop_prepped = np.asfarray(
  397. Image.new(mode="RGBA", size=im_shadow.size, color=(255, 255, 255, 255))
  398. )
  399. im_shadow = im_shadow.convert("RGBA")
  400. source_prepped = np.asfarray(im_shadow)
  401. # im_shadow.show()
  402. opacity = (average_brightness - 30) / 160
  403. opacity = max(0.5, min(opacity, 1))
  404. print("阴影透明度:{}%".format(int(opacity * 100)))
  405. blended_np = multiply(
  406. backdrop_prepped, source_prepped, opacity=int(opacity * 100) / 100
  407. )
  408. im_shadow = Image.fromarray(np.uint8(blended_np)).convert("RGB")
  409. # im_shadow.show()
  410. # 把原图粘贴回去,避免色差
  411. im_shadow.paste(cut_image, (0, 0), mask=cut_image)
  412. # _new_im_shadow.show()
  413. # ===========处理其他====================
  414. # 保存带有阴影的底图,没有logo
  415. if out_process_path_1:
  416. out_image_1 = im_shadow.copy()
  417. if image_deal_mode == 1:
  418. out_image_1 = out_image_1.transpose(Image.FLIP_LEFT_RIGHT)
  419. self.saver.save_image(
  420. image=out_image_1, file_path=out_process_path_1, save_mode="png"
  421. )
  422. # save_image_by_thread(image=out_image_1, out_path=out_process_path_1)
  423. # out_image_1.save(out_process_path_1)
  424. # 保存抠图结果,没有底图,没有logo
  425. if out_process_path_2:
  426. out_image_2 = cut_image.copy()
  427. if image_deal_mode == 1:
  428. out_image_2 = out_image_2.transpose(Image.FLIP_LEFT_RIGHT)
  429. self.saver.save_image(
  430. image=out_image_2, file_path=out_process_path_2, save_mode="png"
  431. )
  432. # save_image_by_thread(image=out_image_2, out_path=out_process_path_2, save_mode="png")
  433. # out_image_2.save(out_process_path_2)
  434. # 不生成主图时直接退出
  435. if not out_path:
  436. return True
  437. # im_shadow.show()
  438. # =====================主图物体的缩放依据大小
  439. if max_box:
  440. im_shadow = to_resize(_im=im_shadow, width=max_box[0], high=max_box[1])
  441. cut_image = to_resize(_im=cut_image, width=max_box[0], high=max_box[1])
  442. else:
  443. if resize_mode is None:
  444. im_shadow = to_resize(_im=im_shadow, width=1400, high=1400)
  445. cut_image = to_resize(_im=cut_image, width=1400, high=1400)
  446. elif resize_mode == 1:
  447. im_shadow = to_resize(_im=im_shadow, width=1400, high=1400)
  448. cut_image = to_resize(_im=cut_image, width=1400, high=1400)
  449. elif resize_mode == 2:
  450. # todo 兼容长筒靴等,将图片大小限制在一个指定的box内
  451. im_shadow = to_resize(_im=im_shadow, width=650)
  452. cut_image = to_resize(_im=cut_image, width=650)
  453. # 再次检查需要约束缩小到一定高度,适应长筒靴
  454. _im_x, _im_y = cut_image.size
  455. if _im_y > 1400:
  456. im_shadow = to_resize(_im=im_shadow, high=1400)
  457. cut_image = to_resize(_im=cut_image, high=1400)
  458. # if im_shadow.height <= im_shadow.width * 1.2:
  459. # im_shadow = to_resize(_im=im_shadow, width=650)
  460. # cut_image = to_resize(_im=cut_image, width=650)
  461. # else:
  462. # im_shadow = to_resize(_im=im_shadow, high=1400)
  463. # cut_image = to_resize(_im=cut_image, high=1400)
  464. if image_deal_mode == 1:
  465. # 翻转
  466. im_shadow = im_shadow.transpose(Image.FLIP_LEFT_RIGHT)
  467. cut_image = cut_image.transpose(Image.FLIP_LEFT_RIGHT)
  468. # 创建底层背景
  469. image_bg = Image.new("RGB", (1600, 1600), (255, 255, 255))
  470. image_bg_x, image_bg_y = image_bg.size
  471. image_x, image_y = im_shadow.size
  472. _x = int((image_bg_x - image_x) / 2)
  473. _y = int((image_bg_y - image_y) / 2)
  474. image_bg.paste(im_shadow, (_x, _y))
  475. image_bg.paste(cut_image, (_x, _y), cut_image) # 再叠加原图避免色差
  476. if "小苏" in settings.Company:
  477. # 所有主图加logo
  478. is_logo = True
  479. if is_logo:
  480. # logo_path = ""
  481. # if settings.PROJECT == "红蜻蜓":
  482. # logo_path = r"resources\LOGO\HQT\logo.png"
  483. # elif settings.PROJECT == "惠利玛":
  484. # if "小苏" in settings.Company:
  485. # logo_path = r"resources\LOGO\xiaosushuoxie\logo.png"
  486. # elif "惠利玛" in settings.Company:
  487. # logo_path = r"resources\LOGO\HLM\logo.png"
  488. # else:
  489. # pass
  490. if not logo_path:
  491. logo_im = Image.new("RGBA", (1600, 1600), (0, 0, 0, 0))
  492. else:
  493. if os.path.exists(logo_path):
  494. logo_im = Image.open(logo_path)
  495. if logo_im.mode != 'RGBA':
  496. logo_im = logo_im.convert('RGBA')
  497. else:
  498. logo_im = Image.new("RGBA", (1600, 1600), (0, 0, 0, 0))
  499. try:
  500. image_bg.paste(logo_im, (0, 0), logo_im)
  501. except Exception as e:
  502. alpha_mask = logo_im.split()[3]
  503. image_bg.paste(logo_im, (0, 0), alpha_mask)
  504. out_pci_factor = float(
  505. 1
  506. if settings.getSysConfigs("basic_configs", "image_sharpening", "1") == ""
  507. else settings.getSysConfigs("basic_configs", "image_sharpening", "1")
  508. )
  509. # image_bg = image_bg.resize((out_pic_size, out_pic_size), Image.BICUBIC)
  510. if out_pci_factor > 1.0:
  511. print("图片锐化处理")
  512. image_bg = sharpen_image(image_bg, factor=out_pci_factor)
  513. out_pci_mode = "." + settings.getSysConfigs(
  514. "basic_configs", "image_out_format", "png"
  515. )
  516. for imageSize in out_pic_size:
  517. dot_index = out_path.rfind(".")
  518. if dot_index != -1:
  519. # 拆分文件路径和后缀
  520. file_without_suffix = out_path[:dot_index]
  521. suffix = out_path[dot_index + 1 :]
  522. else:
  523. file_without_suffix = out_path
  524. suffix = ""
  525. # 单独拼接字符串示例
  526. new_file_path = f"{file_without_suffix}_{imageSize}.{suffix}"
  527. if imageSize < 1600:
  528. image_bg = image_bg.resize(
  529. (imageSize, imageSize), resample=settings.RESIZE_IMAGE_MODE
  530. )
  531. if out_pci_mode == ".jpg":
  532. self.saver.save_image(
  533. image=image_bg,
  534. file_path=new_file_path,
  535. save_mode="jpg",
  536. quality=None,
  537. dpi=None,
  538. _format="JPEG",
  539. )
  540. # save_image_by_thread(image_bg, out_path, save_mode="jpg", quality=None, dpi=None, _format="JPEG")
  541. # image_bg.save(out_path, format="JPEG")
  542. elif out_pci_mode == ".png":
  543. self.saver.save_image(
  544. image=image_bg,
  545. file_path=new_file_path,
  546. save_mode="jpg",
  547. quality=100,
  548. dpi=(300, 300),
  549. _format="JPEG",
  550. )
  551. else:
  552. new_format = out_pci_mode.split(".")[-1]
  553. self.saver.save_image(
  554. image=image_bg,
  555. file_path=new_file_path,
  556. save_mode=new_format,
  557. quality=100,
  558. dpi=(300, 300),
  559. _format=new_format,
  560. )
  561. # save_image_by_thread(image_bg, out_path, save_mode="jpg", quality=100, dpi=(300, 300), _format="JPEG")
  562. # image_bg.save(out_path, quality=100, dpi=(300, 300), format="JPEG")
  563. else:
  564. new_format = out_pci_mode.split(".")[-1]
  565. self.saver.save_image(
  566. image=image_bg,
  567. file_path=new_file_path,
  568. save_mode=new_format,
  569. _format=new_format,
  570. )
  571. # image_bg.save(out_path)
  572. if output_queue is not None:
  573. output_queue.put(True)
  574. return True