image_pic_deal.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. """
  2. """
  3. import cv2
  4. import numpy as np
  5. from PIL import Image, ImageDraw, ImageFont
  6. from .data import DataModeAutoDealPics
  7. class OnePicDeal(object):
  8. def __init__(self):
  9. # 数据模型
  10. self.data_mode_auto_deal_pics = DataModeAutoDealPics()
  11. def check_shoe_is_right(self, im=None, image_path=None):
  12. # 先进行API识别左右脚
  13. if im is None:
  14. im = Image.open(image_path)
  15. try:
  16. r_data = self.data_mode_auto_deal_pics.check_is_right_foot_by_api(image=im)
  17. except BaseException as e:
  18. r_data = None
  19. print("20", e)
  20. flag = self.check_shoe_is_right_by_pixel(im=im)
  21. if r_data:
  22. if "拖鞋" in r_data:
  23. flag = flag is not True
  24. if flag:
  25. print("自动识别----->这是左脚")
  26. else:
  27. print("自动识别----->这是右脚")
  28. return flag
  29. def check_shoe_is_right_by_pixel(self, im=None, image_path=None):
  30. if im is None:
  31. im = Image.open(image_path)
  32. # 注意,只支持透明图
  33. # 打开图像文件
  34. im = im.crop(im.getbbox())
  35. # image.show()
  36. # 获取图像第一行的像素数据
  37. pixel_data = im.load()
  38. pix_list = []
  39. h = int(im.height / 20)
  40. for i in range(im.width):
  41. _r, _g, _b, _a = pixel_data[i, h]
  42. if _a > 10:
  43. pix_list.append(i)
  44. left_f_num = 0
  45. middle_w = int(im.width / 2)
  46. for i in pix_list:
  47. if i < middle_w:
  48. left_f_num += 1
  49. else:
  50. left_f_num -= 1
  51. if left_f_num > 0:
  52. return True
  53. else:
  54. return False
  55. def how_to_use(self):
  56. # 单图缩放处理
  57. a = {"command": "resize",
  58. "plugins_mode": "relative", # pixel 相对(宽度、高度、其他参考图),或绝对像素
  59. "base_im": {"im": "im"}, # 参考基于其他图 PIL格式
  60. "base": "width", # base:pixel,width,height,by_im 基于长边、基于短边 (基于短边时,则缩放到指定尺寸)by_im确保能塞进参考图内
  61. "value": 649, # 固定值,如果为百分比,则为0
  62. "percentage": 0, } # 百分比
  63. # 单图圆角处理
  64. a = {"command": "radius", # radius
  65. "plugins_mode": "relative", # pixel 相对(短边),或绝对像素
  66. "circular_pos": (0, 1, 0, 1), # 从左上角顺时针,记录圆角数量
  67. "value": 649, # 固定值,如果为百分比,则为0
  68. "percentage": 0, } # 百分比
  69. # 单图处理成圆形
  70. a = {"command": "circular", # circular
  71. }
  72. # 单图旋转处理
  73. a = {"command": "rotate",
  74. "plugins_mode": "",
  75. "value": 649, # 固定值 顺时针
  76. }
  77. # 图片粘贴处理
  78. a = {
  79. "command": "paste_img",
  80. "img": {"im": "im"},
  81. "pos": {"plugins_mode": "relative", # pixel
  82. "base": "center", # nw,nc,ne,ec ... 各个方向参考点
  83. "value": (100, 100),
  84. "percentage": (0.5, 0.5),
  85. },
  86. "margins": (0, 0, 0, 0), # 上下左右边距
  87. }
  88. # 图片剪裁处理
  89. a = {
  90. "command": "crop_img",
  91. "img": {"im": "im"},
  92. "pos": {"plugins_mode": "relative", # pixel
  93. "base": "center", # nw,nc,ne,ec ... 各个方向参考点
  94. "value": (100, 100, 10, 10),
  95. },
  96. "color_fill": (255, 255, 255)
  97. }
  98. # 图片添加文字
  99. a = {
  100. "command": "add_text",
  101. "pos": {"plugins_mode": "relative", # pixel
  102. "base": "center", # nw,nc,ne,ec ... 各个方向参考点
  103. "value": (100, 100),
  104. "percentage": 0, },
  105. "font": "",
  106. "text": "",
  107. "anchor": "", # mm 为居中 ma 为左右居中,上面居顶
  108. "align": "对齐方式",
  109. "direction": "文本的方向",
  110. "max_len_one_line": "单行长度",
  111. "spacing": 10,
  112. "fill": "文字颜色",
  113. }
  114. def add_text(self, img: Image, command):
  115. draw_1 = ImageDraw.Draw(img)
  116. # 定义字体,你需要有一个.ttf字体文件
  117. font = command["font"]
  118. text = command["text"]
  119. spacing = 4 if not command["spacing"] else command["spacing"]
  120. fill = command["fill"]
  121. anchor = None if not command["anchor"] else command["anchor"]
  122. align = "left" if not command["align"] else command["align"] # left, center 或 right
  123. _, _, text_width, text_height = draw_1.textbbox((0, 0), text, font=font)
  124. xy = (0, 0)
  125. if command["pos"]["plugins_mode"] == "pixel":
  126. value = command["pos"]["value"]
  127. xy = value
  128. draw_1.multiline_text(xy, text,
  129. fill=fill,
  130. font=font,
  131. anchor=anchor,
  132. spacing=spacing,
  133. align=align,
  134. direction=None,
  135. features=None,
  136. language=None,
  137. stroke_width=0,
  138. stroke_fill=None,
  139. embedded_color=False)
  140. return img
  141. def resize(self, img: Image, command):
  142. if command["plugins_mode"] == "pixel":
  143. if command["base"] == "width":
  144. img = self.to_resize(img, width=command["value"])
  145. if command["base"] == "high":
  146. img = self.to_resize(img, height=command["value"])
  147. # 相对值
  148. if command["plugins_mode"] == "relative":
  149. base_im = command["base_im"]["im"]
  150. if command["base"] == "width":
  151. img = self.to_resize(img, width=img.width * command["percentage"] if not base_im else int(
  152. base_im.width * command["percentage"]))
  153. if command["base"] == "height":
  154. img = self.to_resize(img, width=img.height * command["percentage"] if not base_im else int(
  155. base_im.height * command["percentage"]))
  156. # by_im确保能塞进参考图内
  157. if command["base"] == "by_im":
  158. percentage = 1 if not command["percentage"] else command["percentage"]
  159. box_width, box_height = int(base_im.width * percentage), int(base_im.height * percentage)
  160. width, height = img.width, img.height
  161. if box_width / box_height < width / height:
  162. scale = box_width / width
  163. else:
  164. scale = box_height / height
  165. img = img.resize((int(width * scale), int(height * scale)))
  166. # img.show()
  167. return img
  168. def paste_img(self, img: Image, command):
  169. # 粘贴绝对像素
  170. base = "nw" if not command["pos"]["base"] else command["pos"]["base"]
  171. value = command["pos"]["value"]
  172. percentage = (0, 0) if not command["pos"]["percentage"] else command["pos"]["percentage"]
  173. if command["margins"]:
  174. top, down, left, right = command["margins"]
  175. else:
  176. top, down, left, right = 0, 0, 0, 0
  177. if percentage != (0, 0): # percentage 不按占比模式
  178. if base in ("nw", "wn", "wc", "cw", "nc", "cn", "center"):
  179. value = (int(img.width), int(img.height))
  180. if base in ("sw", "ws", "sc", "cs", "center"):
  181. value = (int(img.width), -1 * int(img.height))
  182. if base in ("ec", "ce"):
  183. value = (int(img.width), int(img.height))
  184. img_1 = command["img"]["im"]
  185. if command["pos"]["plugins_mode"] == "pixel":
  186. if base == "ec" or "ce":
  187. p_x = int(img.width - img_1.width) + value[0]
  188. p_y = value[1]
  189. if base == "nw" or "wn":
  190. deviation_x, deviation_y = 0, 0
  191. p_x, p_y = value
  192. if base == "cs" or "sc":
  193. p_x, p_y = value
  194. if base == "center":
  195. deviation_x, deviation_y = int((img.width - img_1.width) / 2), int((img.height - img_1.height) / 2)
  196. p_x = deviation_x + value[0] + left
  197. p_y = deviation_y + value[1] + top
  198. if base == "sw" or base == "ws":
  199. # deviation_x, deviation_y = 0, int((img.height - img_1.height))
  200. p_x = value[0] + left
  201. p_y = img.height - (img_1.height + value[1] + down)
  202. if base == "wc" or base == "cw":
  203. p_x = value[0] + left
  204. p_y = int((img.height - img_1.height) / 2) + value[1] + top
  205. try:
  206. img.paste(img_1, (p_x, p_y), img_1)
  207. except:
  208. img.paste(img_1, (p_x, p_y), img_1.convert("RGBA"))
  209. return img
  210. def crop_img(self, img: Image, command):
  211. base = "nw" if not command["pos"]["base"] else command["pos"]["base"]
  212. # print(base)
  213. value = command["pos"]["value"]
  214. percentage = command["pos"]["percentage"]
  215. if command["margins"]:
  216. top, down, left, right = command["margins"]
  217. else:
  218. top, down, left, right = 0, 0, 0, 0
  219. out_img_size = (value[2], value[3])
  220. # 默认填充色
  221. color_fill = command["color_fill"]
  222. if not color_fill:
  223. color_fill = (0, 0, 0)
  224. if command["pos"]["plugins_mode"] == "pixel":
  225. if base == "nw" or "wn":
  226. box = value
  227. if base == "sw" or base == "ws":
  228. # deviation_x, deviation_y = 0, int((img.height - img_1.height))
  229. box = (value[0], img.height - (value[1] + value[3]), value[2], value[3])
  230. print(box)
  231. if base == "se" or base == "es":
  232. box = (img.width - (value[0] + value[2]), img.height - (value[1] + value[3]), value[2], value[3])
  233. print(box)
  234. box = [box[0], box[1], box[0] + box[2], box[1] + box[3]]
  235. print("box", box)
  236. print("img.width", img.width)
  237. print("img.height", img.height)
  238. out_img = img.crop(box=box)
  239. print(out_img.size)
  240. # out_img.show()
  241. if box[0] < 0:
  242. out_img.paste(Image.new("RGB", (-1 * box[0], out_img.height), color_fill), (0, 0))
  243. # print(img.width, box)
  244. if box[2] > img.width:
  245. # print(box[2] - img.width, img.height)
  246. i = Image.new("RGB", (box[2] - img.width, out_img.height), color_fill)
  247. out_img.paste(i, (img.width - box[0], 0))
  248. if box[1] < 0:
  249. out_img.paste(Image.new("RGB", (img.width, -1 * box[1]), color_fill), (0, 0))
  250. if box[3] > img.height:
  251. out_img.paste(Image.new("RGB", (out_img.width, box[3] - img.height), color_fill),
  252. (0, img.height - box[1]))
  253. # bg_img = Image.new("RGB", out_img_size, color_fill)
  254. # if box[0] < 0:
  255. # x = -1 * box[0]
  256. # elif box[2] > img.width:
  257. # x = img.width - box[2]
  258. # else:
  259. # x = 0
  260. #
  261. # if box[1] < 0:
  262. # y = -1 * box[1]
  263. # elif box[2] > img.height:
  264. # y = img.height - box[1]
  265. # else:
  266. # y = 0
  267. # bg_img.paste(img, box=(x, y), )
  268. # print(box)
  269. # img = img.crop(box=box)
  270. # out_img.show()
  271. return out_img
  272. def radius(self, img: Image, command):
  273. # 单图圆角处理
  274. radii = command["value"]
  275. if radii > img.width / 2:
  276. radii = int(img.width / 2)
  277. if radii > img.height / 2:
  278. radii = int(img.height / 2)
  279. # 画圆(用于分离4个角)
  280. circle = Image.new('L', (radii * 2, radii * 2), 0) # 创建一个黑色背景的画布
  281. draw = ImageDraw.Draw(circle)
  282. draw.ellipse((0, 0, radii * 2, radii * 2), fill=255) # 画白色圆形
  283. # 原图
  284. img = img.convert("RGBA")
  285. w, h = img.size
  286. # 画4个角(将整圆分离为4个部分)
  287. alpha = Image.new('L', img.size, 255)
  288. _pos = command["circular_pos"]
  289. if not _pos:
  290. _pos = (1, 1, 1, 1)
  291. for index, i in enumerate(_pos):
  292. if index == 0 and i == 1:
  293. alpha.paste(circle.crop((0, 0, radii, radii)), (0, 0)) # 左上角
  294. if index == 1 and i == 1:
  295. alpha.paste(circle.crop((radii, 0, radii * 2, radii)), (w - radii, 0)) # 右上角
  296. if index == 2 and i == 1:
  297. alpha.paste(circle.crop((radii, radii, radii * 2, radii * 2)), (w - radii, h - radii)) # 右下角
  298. if index == 3 and i == 1:
  299. alpha.paste(circle.crop((0, radii, radii, radii * 2)), (0, h - radii)) # 左下角
  300. # alpha.show()
  301. img.putalpha(alpha) # 白色区域透明可见,黑色区域不可见
  302. return img
  303. def to_resize(self, _im, width=None, height=None):
  304. _im_x, _im_y = _im.size
  305. if width and height:
  306. if _im_x >= _im_y:
  307. height = None
  308. else:
  309. width = None
  310. if width:
  311. re_x = int(width)
  312. re_y = int(_im_y * re_x / _im_x)
  313. else:
  314. re_y = int(height)
  315. re_x = int(_im_x * re_y / _im_y)
  316. _im = _im.resize((re_x, re_y))
  317. return _im
  318. def add_pic(self, detailed_images):
  319. if not detailed_images:
  320. return
  321. page_len = 0
  322. for index, im in enumerate(detailed_images):
  323. page_len += im.height
  324. bg_im = Image.new("RGB", (im.width, page_len), (255, 255, 255))
  325. n = 0
  326. for index, im in enumerate(detailed_images):
  327. bg_im.paste(im, (0, n))
  328. n += im.height
  329. # bg_im.show()
  330. return bg_im
  331. def get_goods_pos(self, im, cut_image):
  332. # 保留多余内容
  333. old_x, old_y = im.size
  334. x1, y1, x2, y2 = cut_image.getbbox()
  335. goods_w, goods_h = x2 - x1, y2 - y1
  336. _w, _h = int(goods_w / 10), int(goods_h / 10) # 上下左右扩展位置
  337. new_x1, new_y1, new_x2, new_y2 = x1 - _w, y1 - _h, x2 + _w, y2 + _h # 防止超限
  338. new_x1 = 0 if new_x1 < 0 else new_x1
  339. new_y1 = 0 if new_y1 < 0 else new_y1
  340. new_x2 = old_x if new_x2 > old_x else new_x2
  341. new_y2 = old_y if new_y2 > old_y else new_y2
  342. # 剪切掉多余的内容,保留阴影
  343. im = im.crop((new_x1, new_y1, new_x2, new_y2)) # 切图
  344. return im
  345. def deal_one_pic(self, data=None, is_show=False):
  346. """
  347. 通用图片处理器
  348. 1、图片位置处理
  349. 输出拼接后的图片,以及拼接后商品所属位置
  350. """
  351. for command in data:
  352. if command["command"] == "paste_img":
  353. img1 = command["img1"]["im"]
  354. img2 = command["img2"]["im"]
  355. if "resize" in command["img2"]:
  356. if "width" in command["img2"]["resize"]:
  357. img2 = self.to_resize(img2, width=command["img2"]["resize"]["width"])
  358. if is_show:
  359. img2.show()
  360. img1.paste(img2, command["img2"]["pos"])
  361. if command["command"] == "image_crop":
  362. img1 = img1.crop(command["image_crop"])
  363. img = img1
  364. return img
  365. def deal_one_pic_2(self, orign_im, data=None):
  366. """
  367. 通用图片处理器
  368. 1、基于某原始图,进行加工,包括粘贴、单图处理等逻辑
  369. """
  370. # 单图缩放处理
  371. a = {"command": "resize",
  372. "plugins_mode": "relative", # pixel 相对(宽度、高度、其他参考图),或绝对像素
  373. "base_im": "im", # 参考基于其他图 PIL格式
  374. "base": "width", # base:pixel,width,height,by_long_side,by_short_side基于长边、基于短边 (基于短边时,则缩放到指定尺寸)
  375. "value": 649, # 固定值,如果为百分比,则为0
  376. "percentage": 0, } # 百分比
  377. # 单图圆角处理
  378. a = {"command": "radius", # radius
  379. "plugins_mode": "relative", # pixel 相对(短边),或绝对像素
  380. "circular_pos": (0, 1, 0, 1), # 从左上角顺时针,记录圆角数量
  381. "value": 649, # 固定值,如果为百分比,则为0
  382. "percentage": 0, } # 百分比
  383. # 单图处理成圆形
  384. a = {"command": "circular", # circular
  385. }
  386. # 单图旋转处理
  387. a = {"command": "rotate",
  388. "plugins_mode": "",
  389. "value": 649, # 固定值 顺时针
  390. }
  391. # 图片粘贴处理
  392. a = {
  393. "command": "paste_img",
  394. "img": {"im": "im"},
  395. "pos": {"plugins_mode": "relative", # pixel
  396. "base": "center", # nw,nc,ne,ec ... 各个方向参考点
  397. "value": (100, 100),
  398. "percentage": 0, },
  399. "margins": (0, 0, 0, 0), # 上下左右边距
  400. }
  401. # 图片剪裁处理
  402. a = {
  403. "command": "crop_img",
  404. "img": {"im": "im"},
  405. "plugins_mode": "relative", # pixel
  406. "base": "center", # nw,nc,ne,ec ... 各个方向参考点
  407. "box": (0, 0, 0, 0),
  408. }
  409. # 图片添加文字
  410. a = {
  411. "command": "add_text",
  412. "pos": {"plugins_mode": "relative", # pixel
  413. "base": "center", # nw,nc,ne,ec ... 各个方向参考点
  414. "value": (100, 100),
  415. "percentage": 0, },
  416. "font": "",
  417. "text": "",
  418. "align": "对齐方式",
  419. "direction": "文本的方向",
  420. "fill": "文字颜色",
  421. }
  422. # 图片渐变处理
  423. # https://blog.csdn.net/skying159/article/details/119532479
  424. def get_value(data_dict, key):
  425. return 1
  426. r_dict = {"nw": 0,
  427. }
  428. _r = 0
  429. for command in data:
  430. if command["command"] == "paste_img":
  431. img1 = command["img1"]["im"]
  432. img2 = command["img2"]["im"]
  433. if "resize" in command["img2"]:
  434. if "width" in command["img2"]["resize"]:
  435. img2 = self.to_resize(img2, width=command["img2"]["resize"]["width"])
  436. if "pos" in command["img2"]:
  437. base = command["img2"]["base"]
  438. value = command["img2"]["value"]
  439. if command["img2"]["plugins_mode"] == "relative":
  440. # 相对位置处理,居中比较特殊
  441. # 其他的先进行旋转与镜像,处理后,进行反向操作
  442. x, y = 0, 0
  443. if base == "center":
  444. x, y = int((img1.width - img2.width) / 2), int((img1.height - img2.height) / 2)
  445. if base == "nw" or base == "nw":
  446. pass
  447. w, h = img1.width * value[0], img1.height * value[0]
  448. img1.paste(img2, command["img2"]["pos"])
  449. if _r != 0:
  450. img1 = img1.rotate(_r * -1)
  451. continue
  452. if command["command"] == "image_crop":
  453. img1 = img1.crop(command["image_crop"])
  454. continue
  455. img = img1
  456. return img
  457. def get_overlay_pic(self, pil_img_1, pil_img_2, color):
  458. im_w, im_h = pil_img_1.size
  459. cv_im = cv2.cvtColor(np.asarray(pil_img_1), cv2.COLOR_RGB2BGR)
  460. # 创建一张纯色底图
  461. image_white = Image.new("RGB", (im_w, im_h), color)
  462. cv_image_white = cv2.cvtColor(np.asarray(image_white), cv2.COLOR_RGB2BGR)
  463. new_im = self.to_color_2(cv_image_white, cv_im)
  464. # new_im = cv2.addWeighted(new_im, 0.7, cv_im_2, 0.3, 0)
  465. # new_im = cv2.add(new_im, cv_im_2)
  466. new_im = Image.fromarray(cv2.cvtColor(new_im, cv2.COLOR_BGR2RGB))
  467. new_im.paste(pil_img_2, (0, 0), pil_img_2)
  468. return new_im
  469. def to_color_2(self, target, blend): # 正片叠底
  470. return np.array(np.multiply(target / 256, blend / 256) * 256, dtype=np.uint8)
  471. def to_color_1(self, img1, img2): # PS颜色模式
  472. img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV)
  473. img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV)
  474. img2[:, :, 0] = img1[:, :, 0]
  475. img2[:, :, 1] = img1[:, :, 1]
  476. res = cv2.cvtColor(img2, cv2.COLOR_HSV2BGR)
  477. return res
  478. if __name__ == '__main__':
  479. from collections import defaultdict
  480. def set_dict(one_dict):
  481. _ = defaultdict(str)
  482. for i, v in one_dict.items():
  483. if isinstance(v, dict):
  484. v = set_dict(v)
  485. _[i] = v
  486. return _
  487. def deal_one_pic(img: Image, data=None):
  488. """
  489. 通用图片处理器
  490. 1、基于某原始图,进行加工,包括粘贴、单图处理等逻辑
  491. """
  492. data = [set_dict(x) for x in data]
  493. for command in data:
  494. if command["command"] == "resize":
  495. img = OnePicDeal().resize(img, command)
  496. continue
  497. if command["command"] == "paste_img":
  498. img = OnePicDeal().paste_img(img, command)
  499. continue
  500. if command["command"] == "crop_img":
  501. img = OnePicDeal().crop_img(img, command)
  502. continue
  503. if command["command"] == "radius":
  504. img = OnePicDeal().radius(img, command)
  505. continue
  506. if command["command"] == "add_text":
  507. img = OnePicDeal().add_text(img, command)
  508. continue
  509. return img
  510. image_path = r"D:\MyDocuments\PythonCode\MyPython\red_dragonfly\deal_pics\auto_capture_V2\IPC\output\-秋季订货会夏季补充给葛明明\NUM24106316\阴影图处理\NUM24106316(3)_后跟_阴影.png"
  511. to_paste_img = Image.open(image_path)
  512. data = []
  513. bg_color = (246, 246, 246)
  514. # 单图缩放处理
  515. view = "后跟"
  516. data.append({"command": "resize",
  517. "plugins_mode": "pixel", # pixel 相对(宽度、高度、其他参考图),或绝对像素
  518. "base_im": {"im": ""}, # 参考基于其他图 PIL格式
  519. "base": "width", # base:pixel,width,height,by_im 基于长边、基于短边 (基于短边时,则缩放到指定尺寸)by_im确保能塞进参考图内
  520. "value": 1300 if view == "后跟" else 2200, # 固定值,如果为百分比,则为0
  521. "percentage": 0, }) # 百分比
  522. view_dict = {"俯视": (0, 0, 1100, 1200),
  523. "内里": (-1, -100, 1100, 1200),
  524. "后跟": (0, -100, 1100, 1200),
  525. "鞋底": (0, -100, 1100, 1200),
  526. }
  527. data.append({
  528. "command": "crop_img",
  529. "img": "",
  530. "pos": {"plugins_mode": "pixel", # pixel
  531. "base": "sw", # nw,nc,ne,ec ... 各个方向参考点
  532. "value": view_dict[view],
  533. },
  534. "color_fill": bg_color,
  535. })
  536. # 处理圆角
  537. data.append({"command": "radius", # radius
  538. "plugins_mode": "relative", # pixel 相对(短边),或绝对像素
  539. "circular_pos": (1, 0, 0, 1), # 从左上角顺时针,记录圆角数量
  540. "value": 100, # 固定值,如果为百分比,则为0
  541. "percentage": 0, }) # 百分比
  542. to_paste_img = deal_one_pic(to_paste_img, data)
  543. # print(to_paste_img.size)
  544. # 粘贴到白底图上
  545. img = Image.new("RGB", (1200, 1316), (255, 255, 255))
  546. data = []
  547. data.append({
  548. "command": "paste_img",
  549. "img": {"im": to_paste_img},
  550. "pos": {"plugins_mode": "pixel", # pixel relative
  551. "base": "wc", # nw,nc,ne,ec,... 各个方向参考点
  552. "value": (100, 0),
  553. "percentage": "",
  554. },
  555. "margins": (0, 0, 0, 0), # 上下左右边距
  556. })
  557. img = deal_one_pic(img, data)
  558. img.show()