调整图像大小裁剪超出的部分
Resize the images crop the exceeding part
我有一张分辨率为 1221 x 821
的图像,如果部分超出此范围,我想将其调整为 1200 x 800
,只需将其裁剪即可。而且,我想保留中间部分。怎么做到的?
您可以简单地使用 opencv
:
import cv2
img = cv2.imread(<>) # 1221 x 821
top=0 # Crop from top
buttom=821 # Crop from buttom
left=0 # Crop from left
right=1221 # Crop from right
cropped_image = img[top:buttom,left:right] # 1221 x 821
cv2.imwrite(cropped_image,<file name>)
或使用PIL
:
from PIL import Image
im = Image.open(<>) # 1221 x 821
left=0 # Crop from left
top=0 # Crop from top
right=1221 # Crop from right
buttom=821 # Crop from buttom
im1 = im.crop((left, top, right, bottom)) # 1221 x 821
im1 = im1.save(<>)
有关它的更多信息,请访问 here
如果你想获取图像的宽度和高度,那么对于 opencv
dimensions = img.shape
img.shape returns (Height, Width, Number of Channels)
Height represents the number of pixel rows in the image or the number of pixels in each column of the image array.
Width represents the number of pixel columns in the image or the number of pixels in each row of the image array.
并使用 PIL
:
width, height = im.size
size
is a (width, height)
tuple
我有一张分辨率为 1221 x 821
的图像,如果部分超出此范围,我想将其调整为 1200 x 800
,只需将其裁剪即可。而且,我想保留中间部分。怎么做到的?
您可以简单地使用 opencv
:
import cv2
img = cv2.imread(<>) # 1221 x 821
top=0 # Crop from top
buttom=821 # Crop from buttom
left=0 # Crop from left
right=1221 # Crop from right
cropped_image = img[top:buttom,left:right] # 1221 x 821
cv2.imwrite(cropped_image,<file name>)
或使用PIL
:
from PIL import Image
im = Image.open(<>) # 1221 x 821
left=0 # Crop from left
top=0 # Crop from top
right=1221 # Crop from right
buttom=821 # Crop from buttom
im1 = im.crop((left, top, right, bottom)) # 1221 x 821
im1 = im1.save(<>)
有关它的更多信息,请访问 here
如果你想获取图像的宽度和高度,那么对于 opencv
dimensions = img.shape
img.shape returns (Height, Width, Number of Channels)
Height represents the number of pixel rows in the image or the number of pixels in each column of the image array.
Width represents the number of pixel columns in the image or the number of pixels in each row of the image array.
并使用 PIL
:
width, height = im.size
size
is a(width, height)
tuple