基于Python实现图片一键切割九宫格的工具
作者:徐浪老师 发布时间:2022-07-05 00:22:31
标签:Python,图片,九宫格
有时候发微博时候,需要裁切图片为九宫格,但是ps或者其他工具都太麻烦,这里写一个python一键切割九宫格的工具,以供大家学习和使用!
实现代码
"""
1.将一张图片填充为正方形
2.将文字加到方形图片上
3.讲图片切为9张图并存储
"""
import os
from tkinter import filedialog
from PIL import Image
from future.moves import tkinter
# 填充文字的库
import PIL
from PIL import ImageFont,Image,ImageDraw
def open_img():
"""
打开图片
:return:
"""
root = tkinter.Tk() # 创建一个Tkinter.Tk()实例
root.withdraw() # 将Tkinter.Tk()实例隐藏
default_dir = r"文件路径"
file_path = filedialog.askopenfilename(title=u'选择文件', initialdir=(os.path.expanduser(default_dir)))
if len(file_path) != 0:
image = Image.open(file_path)
fill_image(image)
else:
SystemExit()
def fill_image(img):
"""
将图片填充为正方形
:param img: 图片
:return:
"""
width, height = img.size
# 选取长和宽中较大值作为新图片的
new_image_length = width if width > height else height
# 生成新图片[白底]
new_image = Image.new(img.mode, (new_image_length, new_image_length), color='white')
# 将之前的图粘贴在新图上,居中
if width > height: # 原图宽大于高,则填充图片的竖直维度
# (x,y)二元组表示粘贴上图相对下图的起始位置
new_image.paste(img, (0, int((new_image_length - height) / 2)))
else:
new_image.paste(img, (int((new_image_length - width) / 2), 0))
# 图片上写上文字
# 设置字体,如果没有,也可以不设置
font = ImageFont.truetype(r"C:\Windows\Fonts\STHUPO.TTF", 50)
datas='V:xlzcm88或xlzcm66'
bytedatas=datas.encode('UTF-8')
draw = ImageDraw.Draw(new_image)
draw.text((0,new_image.size[1]/2), u'V:xlzcm88或xlzcm66', font=font)
cut_image(new_image)
def cut_image(img):
"""
切图
:param img: 填充成方形后的图片
:return:
"""
width, height = img.size
item_width = int(width / 3)
box_list = []
for i in range(0, 3): # 两重循环,生成9张图片基于原图的位置
for j in range(0, 3):
box = (j * item_width, i * item_width, (j + 1) * item_width, (i + 1) * item_width)
box_list.append(box)
img_list = [img.crop(box) for box in box_list]
save_images(img_list)
def save_images(img_list):
"""
保存切割完成的图片
:param img_list: 切割后的图片列表
:return:
"""
index = 1
files_path = 'Pic'
# 若文件夹不存在,则创建
if not os.path.exists(files_path):
os.makedirs(files_path)
for img in img_list:
img.save('./Pic/' + str(index) + '.png', 'PNG')
index += 1
print('完成')
if __name__ == '__main__':
open_img()
方法补充
除了上文的方法,小编还给大家整理了其他图片切割成九宫格的方法,希望对大家有所帮助
# -*- coding: utf-8 -*-
from PIL import Image
import sys
# 将图片填充为正方形
def fill_image(image):
width, height = image.size
# 选取长和宽中较大值作为新图片的
new_image_length = width if width > height else height
# 生成新图片[白底]
new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white')
# 将之前的图粘贴在新图上,居中
if width > height: # 原图宽大于高,则填充图片的竖直维度
new_image.paste(image, (0, int((new_image_length - height) / 2))) # (x,y)二元组表示粘贴上图相对下图的起始位置
else:
new_image.paste(image, (int((new_image_length - width) / 2), 0))
return new_image
# 切图
def cut_image(image):
width, height = image.size
item_width = int(width / 3)
box_list = []
# (left, upper, right, lower)
for i in range(0, 3):
for j in range(0, 3):
# print((i*item_width,j*item_width,(i+1)*item_width,(j+1)*item_width))
box = (j * item_width, i * item_width, (j + 1) * item_width, (i + 1) * item_width)
box_list.append(box)
image_list = [image.crop(box) for box in box_list]
return image_list
# 保存
def save_images(image_list):
index = 1
for image in image_list:
image.save('./output/' + str(index) + '.jpg')
index += 1
if __name__ == '__main__':
file_path = "./output/girl.jpg"
image = Image.open(file_path)
image.show()
image = fill_image(image)
image_list = cut_image(image)
save_images(image_list)
来源:https://blog.csdn.net/liaozp88/article/details/129622294


猜你喜欢
- 本文实例讲述了Python3读取UTF-8文件及统计文件行数的方法。分享给大家供大家参考。具体实现方法如下:'''&
- 1.什么是Pillow首先我们需要了解一下PIL(Python Imaging Library),它是Python2中非常强大的图像处理标准
- 众神殿内,依次坐着Editplus、Atom、Sublime、Vscode、JetBrains家族、Comodo等等一众编辑器界的大佬们,偌
- Python break 语句Python break语句,就像在C语言中,打破了最小封闭for或while循环。break语句用来终止循环
- 1.首先通过控制面板应用卸载当前环境下的Node.js相关安装,并清理磁盘残存的文件夹等文件2.下载nvm来管理node版本 &
- juypter notebook中直接使用log_device_placement=True打印不出来device信息# Creates a
- 1. 你必须有自己的服务器,可以在服务器上建立站点。2. 域名管理里 你的域名必须支持泛解析。(现在好像除了 双线双I
- 首先此问题来自向这个帖子http://hi.baidu.com/fire_love_live/item/247276cfda421217b6
- shp2sqlserver用法简析 官方说明: shp2sqlserver is a command line tool for loadi
- 或许马上,或许几年之后,但是有迹象显示IE浏览器占统治地位的时代即将结束。在数据分析公司Net Applications的排名中,IE的市场
- AlexNet介绍AlexNet是2012年ISLVRC 2012(ImageNet Large Scale Visual Recognit
- 我就废话不多说了,大家还是直接看代码吧!import tensorflow as tfimport numpy as npinput = t
- 在看到7yue博客——“换手来用”的思考 有这么一句话:RIA是一个更趋向于“体验”设计的领域,不仅仅包括“开发人员”,还包括“设计人员”,
- 业务场景因为项目刚上线,目前暂不打算引入其他中间件,所以打算通过 mysql 来实现分布式读写锁;而该业务场景也满足分布式读写锁的场景,抽象
- 前言将Selenium程序编写为 .bat 可执行文件,从此一键启动封装好的Selenium程序,省时省力还可以复用,岂不美哉应用场景写好
- 引言本文重点探讨前端组件设计。我相信好的组件设计不仅需要考虑技术实现,同时也需要考虑用户体验、可扩展性和易用性等多个方面。因此,我会重点强调
- 如果只是想实现将jenkins的构建结果发送到企业微信进行通知,最简便的方式是安装Qy Wechat Notification Plugin
- 方法一 :这个是我在站长工具的查询页面使用的防止频繁查询,刷新页面的代码!下面函数的功能是3秒内查询页面即刷新了页面,超过2次就提示!sea
- 1创建窗口1 turtle.setup(width,height,startx,starty)设置主窗口的大小和位置,width如果是整数,
- 简介MQTT.js 是一个开源的 MQTT 协议的客户端库,使用 JavaScript 编写,主要用于 Node.js