0
点赞
收藏
分享

微信扫一扫

记一次放内存切图|BytesIO|PIL库

尤克乔乔 2022-01-04 阅读 66
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的坐标写的比画布大了,出现报错,拿个坐标尺好好算一下。

举报

相关推荐

0 条评论