前言
最近需要对图片进行批量截取处理,所以写下来作为笔记
代码
(1)对每张图片都需要点选然后截图
import matplotlib.pyplot as plt
import os
from PIL import Image
path = "./"
filelist = os.listdir(path)
for pictureName in filelist:
if pictureName[-3:] != "png":
continue
newPath = path + "\\" + pictureName
im = Image.open(newPath)
plt.imshow(im, cmap = plt.get_cmap("gray"))
pos = plt.ginput(2)
cropped = im.crop((pos[0][0], pos[0][1], pos[1][0], pos[1][1]))
cropped.save("./"+ pictureName )
(2)先手选一个截图范围 然后批量对所有图执行相同操作
import matplotlib.pyplot as plt
from PIL import Image
im = Image.open("??")
plt.imshow(im, cmap = plt.get_cmap("gray"))
pos=plt.ginput(2)
print(pos)
cropped = im.crop((pos[0][0], pos[0][1], pos[1][0], pos[1][1]))
cropped.save("./??")
完整代码:
import os
import matplotlib.pyplot as plt
from PIL import Image
im = Image.open("/1.png")
plt.imshow(im, cmap = plt.get_cmap("gray"))
pos=plt.ginput(2)
print(pos)
path = "./"
filelist = os.listdir(path)
for pictureName in filelist:
if pictureName[-3:] != "png":
continue
newPath = path + "\\" + pictureName
im = Image.open(newPath)
cropped = im.crop((pos[0][0], pos[0][1], pos[1][0], pos[1][1]))
cropped.save("./"+ pictureName )
(3)我就比较倒霉了 matlab的plotlib的老问题,前后端不能交互
那就手动指定位置呗 可以参考报错之后仍然会显示的灰度图 有一个所谓的坐标 自己设置 pos
|