I'm working with satellites images and i开发者_高级运维 need to select one part of the image to work if. How can i do it? Im.crop doesn't seen to work. Resize?
Thanks
from PIL import Image
im = Image.open("test.jpg")
crop_rectangle = (50, 50, 200, 200)
cropped_im = im.crop(crop_rectangle)
cropped_im.show()
Note that the crop region must be given as a 4-tuple - (left, upper, right, lower).
More details here Using the Image Class
To get a smaller image from the larger image, just use the array indexes
subImage=Image[ miny:maxy, minx:maxx ]
Here you can draw a rectangle over the image, to get it cropped
import numpy as np
import cv2
import matplotlib.pyplot as plt
def downloadImage(URL):
"""Downloads the image on the URL, and convers to cv2 BGR format"""
from io import BytesIO
from PIL import Image as PIL_Image
import requests
response = requests.get(URL)
image = PIL_Image.open(BytesIO(response.content))
return cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
fig, ax = plt.subplots()
URL = "https://www.godan.info/sites/default/files/old/2015/04/Satellite.jpg"
img = downloadImage(URL)[:, :, ::-1] # BGR->RGB
plt.imshow(img)
selectedRectangle = None
# Capture mouse-drawing-rectangle event
def line_select_callback(eclick, erelease):
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
selectedRectangle = plt.Rectangle(
(min(x1, x2), min(y1, y2)),
np.abs(x1 - x2),
np.abs(y1 - y2),
color="r",
fill=False,
)
ax.add_patch(selectedRectangle)
imgOnRectangle = img[
int(min(y1, y2)) : int(max(y1, y2)), int(min(x1, x2)) : int(max(x1, x2))
]
plt.figure()
plt.imshow(imgOnRectangle)
plt.show()
from matplotlib.widgets import RectangleSelector
rs = RectangleSelector(
ax,
line_select_callback,
drawtype="box",
useblit=False,
button=[1],
minspanx=5,
minspany=5,
spancoords="pixels",
interactive=True,
)
plt.show()
精彩评论