python-imaging-library - 裁剪图像的中心
问题描述
我如何使用crop()
来自的功能PIL
函数裁剪 224*224 图像的中心。
给定输入图像:
320*240,裁剪这个224*224尺寸图片的中心
预期输出:
裁剪尺寸为 224*224 的图像中心
解决方案
从这张图片开始,彩色部分为 224x224,黑色背景为 320x240。
我会numpy
像这样裁剪:
#!/usr/local/bin/python3
from PIL import Image
import numpy as np
# Open the image and convert to numpy array
im=Image.open('start.png')
im=np.array(im)
# Work out where top left corner is
y=int((320-224)/2)
x=int((240-224)/2)
# Crop, convert back from numpy to PIL Image and and save
cropped=im[x:x+224,y:y+224]
Image.fromarray(cropped).save('result.png')
推荐阅读
- r - R中所有国家的ISO-3
- javascript - 关于 JavaScript 中的 return 语句的混淆
- javascript - 即使在页面重新加载后也显示服务工作者更新通知
- shopify - 在 Shopify 产品页面上选择一种颜色选项时隐藏其他产品图片
- excel - 如何将表格从一张纸移动到另一张纸,同时将其重新排列为特定格式?
- python - 我使用 python pandas 来提取一些数据(页面标题),但输出的顺序与我在代码中输入的 URL 的顺序不同
- javascript - Highchart - 无法将我的 JSON 字符串解析为系列中的变量,但硬编码方式似乎工作正常
- mysql - 选择所有日期的计数小于值的行
- toml - 在 TOML 文件上创建新行
- sql-server - MS SQL Server 2016 - 如何记录记录锁定问题?