grenerate_main_image_test.py 24 KB

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