前段时间突然想做表情包,大概流程是通过sd生成图片然后做成表情包发布在微信平台。在发布之前需要将图片背景抠成透明,为了方便批量操作,我打算搞个脚本进行一键抠图。
抠图功能Koutu.py
from PIL import Image import os import shutil
input_folder = 'xxx\\input'
output_folder = 'xxx\\output'
if not os.path.exists(output_folder): os.makedirs(output_folder)
for filename in os.listdir(input_folder): if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')): file_path = os.path.join(input_folder, filename) with Image.open(file_path) as img: rgba_img = img.convert("RGBA") pixels = rgba_img.load() width, height = img.size for y in range(height): for x in range(width): r, g, b, a = pixels[x, y] if r == 255 and g == 255 and b == 255: pixels[x, y] = (r, g, b, 0) output_file_path = os.path.join(output_folder, filename) rgba_img.save(output_file_path, "PNG") print(f"Processed {filename} and saved to {output_file_path}")
def delete_images_in_folder(folder_path, extensions): for filename in os.listdir(folder_path): file_path = os.path.join(folder_path, filename) if os.path.isfile(file_path): _, ext = os.path.splitext(filename) if ext.lower() in extensions: try: os.remove(file_path) print(f"Deleted {file_path}") except Exception as e: print(f"Failed to delete {file_path}: {e}")
image_extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff'}
delete_images_in_folder(r"D:\82462\Desktop\newFile\input", image_extensions) delete_images_in_folder(r"D:\82462\Desktop\newFile", image_extensions)
|
做完抠图功能之后上传的时候发现微信有限制图片的格式大小,所以我又加上了一个缩放功能。
并且实现缩放完成后再将图片存入input文件夹内。以便后续抠图。
以下是代码。
缩放功能suofang.py
from PIL import Image import os
input_folder = 'xxx\\newFile'
output_folder = 'xxx\\input'
if not os.path.exists(output_folder): os.makedirs(output_folder)
for filename in os.listdir(input_folder): if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')): file_path = os.path.join(input_folder, filename) with Image.open(file_path) as img: resized_img = img.resize((240, 240), Image.BICUBIC) output_file_path = os.path.join(output_folder, filename) resized_img.save(output_file_path) print(f"Resized {filename} to 240x240px and saved to {output_file_path}") print("所有图片保存在input文件夹中.")
|
最后做一个gui页面方便使用play.py
import tkinter as tk from tkinter import messagebox import subprocess def koutu(): script_path = "D:\\82462\\Desktop\\newFile\\koutu.py" try: subprocess.run(["python", script_path], check=True) except subprocess.CalledProcessError as e: messagebox.showerror("Error", f"Script execution failed with error: {e}") except Exception as e: messagebox.showerror("Error", f"An unexpected error occurred: {e}") def suofang(): script_path = "D:\\82462\\Desktop\\newFile\\suofang.py" try: subprocess.run(["python", script_path], check=True) except subprocess.CalledProcessError as e: messagebox.showerror("Error", f"Script execution failed with error: {e}") except Exception as e: messagebox.showerror("Error", f"An unexpected error occurred: {e}") root = tk.Tk() root.title("我的脚本") button1 = tk.Button(root, text="点击抠图", command=koutu) button2 = tk.Button(root, text="点击缩放", command=suofang) button2.pack() button1.pack() root.mainloop()
|
通过pyinstaller --onefile --windowed player.py
打包我们的py代码。--onefile
代表单文件,--windowed
代表不显示控制台。打包完的文件在dist
文件夹中。
操作:
首先在文件夹中放一张白色背景的图片
双击打开脚本
先缩放图片,图片会保存在input中,再点击抠图,就能完成了。
扣完图长这样,我觉得还是挺清晰的。
ZHY
除了生病以外,你感受到的痛苦,都是你的价值观带来的,而非真实存在。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Niaoyu!