找回密码
 立即注册
搜索
热搜: Excel discuz
查看: 169|回复: 2

[项目代码] 裁切代码

[复制链接]

494

主题

7万

元宝

77万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
775276
发表于 2025-2-8 10:54:41 | 显示全部楼层 |阅读模式
先获取图片大小
  1. from PIL import Image

  2. # 打开图片
  3. image = Image.open(r"C:\Users\截图\104032.jpg")

  4. width, height = image.size

  5. print(f"图片宽度: {width}, 图片高度: {height}")
复制代码




回复

使用道具 举报

494

主题

7万

元宝

77万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
775276
 楼主| 发表于 2025-2-8 10:55:44 | 显示全部楼层
获取图片的大小,并在图片上每隔100个像素,画一条红色的线
  1. from PIL import Image, ImageDraw

  2. # 打开图片
  3. image_path = r'C:\Users\Administrator.DESKTOP-OPNMMBL\Desktop\截图\2025-02-08_104032.jpg'  # 替换为你的图片路径
  4. image = Image.open(image_path)

  5. # 获取图片大小
  6. width, height = image.size
  7. print(f"图片大小: {width}x{height}")

  8. # 创建一个可以在图片上绘制的对象
  9. draw = ImageDraw.Draw(image)

  10. # 在每隔100个像素的位置画红色的线
  11. for x in range(0, width, 100):
  12.     draw.line([(x, 0), (x, height)], fill='red', width=2)  # 竖线
  13. for y in range(0, height, 100):
  14.     draw.line([(0, y), (width, y)], fill='red', width=2)  # 横线

  15. # 保存修改后的图片
  16. image.save('output_image.jpg')  # 可以更改为你想保存的文件名
  17. image.show()  # 显示图片
复制代码




回复

使用道具 举报

494

主题

7万

元宝

77万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
775276
 楼主| 发表于 2025-2-8 10:56:08 | 显示全部楼层
裁切图片
  1. import os
  2. from PIL import Image


  3. def crop_images(input_folder, output_folder, coords):
  4.     # 确保输出文件夹存在
  5.     if not os.path.exists(output_folder):
  6.         os.makedirs(output_folder)

  7.     # 遍历输入文件夹中的所有图片
  8.     for filename in os.listdir(input_folder):
  9.         if filename.endswith(('.png', '.jpg', '.jpeg')):  # 只处理图片文件
  10.             img_path = os.path.join(input_folder, filename)
  11.             with Image.open(img_path) as img:
  12.                 # 裁切区域的坐标 (左, 上, 右, 下)
  13.                 left, top, right, bottom = coords
  14.                 cropped_img = img.crop((left, top, right, bottom))

  15.                 # 保存裁切后的图片
  16.                 cropped_img.save(os.path.join(output_folder, filename))
  17.                 print(f'裁切并保存: {filename}')


  18. if __name__ == "__main__":
  19.     input_folder = r'C:\Users\Administrator.DESKTOP-OPNMMBL\Desktop\截图'  # 输入你的图片文件夹路径
  20.     output_folder = r'D:\FFOutput'  # 输入你想保存裁切后图片的文件夹路径
  21.     coords = (215, 127,1900, 1000)  # 输入裁切的坐标  (左, 上, 右, 下)

  22.     crop_images(input_folder, output_folder, coords)
复制代码




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|五花八门论坛 ( 豫ICP备15031300号-3 )

GMT+8, 2025-2-22 02:06 , Processed in 0.064219 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表