grenerate_main_image_test.py 23 KB

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