基于Python如何实现图片一键切割九宫格工具

工具使用   发布日期:2023年10月09日   浏览次数:645

本文小编为大家详细介绍“基于Python如何实现图片一键切割九宫格工具”,内容详细,步骤清晰,细节处理妥当,希望这篇“基于Python如何实现图片一键切割九宫格工具”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

实现代码

  1. """
  2. 1.将一张图片填充为正方形
  3. 2.将文字加到方形图片上
  4. 3.讲图片切为9张图并存储
  5. """
  6. import os
  7. from tkinter import filedialog
  8. from PIL import Image
  9. from future.moves import tkinter
  10. # 填充文字的库
  11. import PIL
  12. from PIL import ImageFont,Image,ImageDraw
  13. def open_img():
  14. """
  15. 打开图片
  16. :return:
  17. """
  18. root = tkinter.Tk() # 创建一个Tkinter.Tk()实例
  19. root.withdraw() # 将Tkinter.Tk()实例隐藏
  20. default_dir = r"文件路径"
  21. file_path = filedialog.askopenfilename(title=u'选择文件', initialdir=(os.path.expanduser(default_dir)))
  22. if len(file_path) != 0:
  23. image = Image.open(file_path)
  24. fill_image(image)
  25. else:
  26. SystemExit()
  27. def fill_image(img):
  28. """
  29. 将图片填充为正方形
  30. :param img: 图片
  31. :return:
  32. """
  33. width, height = img.size
  34. # 选取长和宽中较大值作为新图片的
  35. new_image_length = width if width > height else height
  36. # 生成新图片[白底]
  37. new_image = Image.new(img.mode, (new_image_length, new_image_length), color='white')
  38. # 将之前的图粘贴在新图上,居中
  39. if width > height: # 原图宽大于高,则填充图片的竖直维度
  40. # (x,y)二元组表示粘贴上图相对下图的起始位置
  41. new_image.paste(img, (0, int((new_image_length - height) / 2)))
  42. else:
  43. new_image.paste(img, (int((new_image_length - width) / 2), 0))
  44. # 图片上写上文字
  45. # 设置字体,如果没有,也可以不设置
  46. font = ImageFont.truetype(r"C:WindowsFontsSTHUPO.TTF", 50)
  47. datas='V:xlzcm88或xlzcm66'
  48. bytedatas=datas.encode('UTF-8')
  49. draw = ImageDraw.Draw(new_image)
  50. draw.text((0,new_image.size[1]/2), u'V:xlzcm88或xlzcm66', font=font)
  51. cut_image(new_image)
  52. def cut_image(img):
  53. """
  54. 切图
  55. :param img: 填充成方形后的图片
  56. :return:
  57. """
  58. width, height = img.size
  59. item_width = int(width / 3)
  60. box_list = []
  61. for i in range(0, 3): # 两重循环,生成9张图片基于原图的位置
  62. for j in range(0, 3):
  63. box = (j * item_width, i * item_width, (j + 1) * item_width, (i + 1) * item_width)
  64. box_list.append(box)
  65. img_list = [img.crop(box) for box in box_list]
  66. save_images(img_list)
  67. def save_images(img_list):
  68. """
  69. 保存切割完成的图片
  70. :param img_list: 切割后的图片列表
  71. :return:
  72. """
  73. index = 1
  74. files_path = 'Pic'
  75. # 若文件夹不存在,则创建
  76. if not os.path.exists(files_path):
  77. os.makedirs(files_path)
  78. for img in img_list:
  79. img.save('./Pic/' + str(index) + '.png', 'PNG')
  80. index += 1
  81. print('完成')
  82. if __name__ == '__main__':
  83. open_img()

方法补充

除了上文的方法,小编还给大家整理了其他图片切割成九宫格的方法,希望对大家有所帮助

  1. # -*- coding: utf-8 -*-
  2. from PIL import Image
  3. import sys
  4. # 将图片填充为正方形
  5. def fill_image(image):
  6. width, height = image.size
  7. # 选取长和宽中较大值作为新图片的
  8. new_image_length = width if width > height else height
  9. # 生成新图片[白底]
  10. new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white')
  11. # 将之前的图粘贴在新图上,居中
  12. if width > height: # 原图宽大于高,则填充图片的竖直维度
  13. new_image.paste(image, (0, int((new_image_length - height) / 2))) # (x,y)二元组表示粘贴上图相对下图的起始位置
  14. else:
  15. new_image.paste(image, (int((new_image_length - width) / 2), 0))
  16. return new_image
  17. # 切图
  18. def cut_image(image):
  19. width, height = image.size
  20. item_width = int(width / 3)
  21. box_list = []
  22. # (left, upper, right, lower)
  23. for i in range(0, 3):
  24. for j in range(0, 3):
  25. # print((i*item_width,j*item_width,(i+1)*item_width,(j+1)*item_width))
  26. box = (j * item_width, i * item_width, (j + 1) * item_width, (i + 1) * item_width)
  27. box_list.append(box)
  28. image_list = [image.crop(box) for box in box_list]
  29. return image_list
  30. # 保存
  31. def save_images(image_list):
  32. index = 1
  33. for image in image_list:
  34. image.save('./output/' + str(index) + '.jpg')
  35. index += 1
  36. if __name__ == '__main__':
  37. file_path = "./output/girl.jpg"
  38. image = Image.open(file_path)
  39. image.show()
  40. image = fill_image(image)
  41. image_list = cut_image(image)
  42. save_images(image_list)

以上就是基于Python如何实现图片一键切割九宫格工具的详细内容,更多关于基于Python如何实现图片一键切割九宫格工具的资料请关注九品源码其它相关文章!