from io import BytesIO
from PIL import Image
screenshot=open('./test.png','rb').read()
#放入内存
img_io = BytesIO()
img_io.write(screenshot)
img = Image.open(img_io)
#切出题目
question = img.crop((200,300,900,520))
#新建一个画布
new_img = Image.new('RGB',(700,220))
#贴入新画布
new_img.paste(question,(0,0,700,220))
#内存对象
new_img_fb = BytesIO()
new_img.save(new_img_fb,'png')
with open('./test2.png','wb') as f:
f.write(new_img_fb.getvalue())
f.close()
img.crop切图
img.crop(x0,y0,x1,y1)
可能遇见的问题:
paste的坐标写的比画布大了,出现报错,拿个坐标尺好好算一下。
补充,贴两张图(注意计算画布和贴图大小):
#切出题目
question = img.crop((200,300,900,520))
#切出答案
answer = img.crop((200,530,900,700))
#新建一个画布
new_img = Image.new('RGB',(700,390))
#贴入新画布
new_img.paste(question,(0,0,700,220))
new_img.paste(answer,(0,220,700,390))