import os
import shutil
from PIL import Image
def get_imlist(path):
return [os.path.join(path, f) for f in os.listdir(path)]
def cut_img(path="demo"):
paths = path +"/"
if os.path.exists(paths):
shutil.rmtree(path)
os.mkdir(path)
else:
os.mkdir(path)
directorys = get_imlist(paths)
for directory in directorys:
if not (directory.endswith('.jpg') or directory.endswith('.png')):
pass
else:
s = "/"
try:
img = Image.open(directory)
oimage_name = directory[directory.rfind(s) + 1:]
(oimage_width, oimage_height) = img.size
l = int(oimage_width / 40)
u = int(oimage_height / 9)
r = 39 * int(oimage_width / 40)
d = 39 * int(oimage_height / 40)
cropped = img.crop((l, u, r, d))
cropped.save("./" + paths + "/" + "%s" % oimage_name)
except:
oimage_name = directory[directory.rfind(s) + 1:]
with open(directory, "rb") as f:
response = f.read()
with open("./" + paths + "/" + "%s" % oimage_name, "wb") as f1:
f1.write(response)
if __name__ == '__main__':
cut_img("./path")