所以我有一个800 x 600的图像,我想用OpenCV 3.1.0垂直切割成两张大小相同的图片。这意味着在剪切结束时,我应该有两个400 x 600的图像,并存储在自己的PIL变量中。
以下是一个例子:
谢谢。
编辑:我想要最有效的解决方案,所以如果该解决方案使用numpy拼接或类似的东西,那就去吧。答案 0 :(得分:6)
您可以尝试以下代码,这将创建两个numpy.ndarray
实例,您可以轻松地显示或写入新文件。
from scipy import misc
# Read the image
img = misc.imread("face.png")
height, width = img.shape
# Cut the image in half
width_cutoff = width // 2
s1 = img[:, :width_cutoff]
s2 = img[:, width_cutoff:]
# Save each half
misc.imsave("face1.png", s1)
misc.imsave("face2.png", s2)
face.png
文件是一个示例,需要替换为您自己的图像文件。
答案 1 :(得分:1)
<?php
function show_products(){
global $connection;
$query = "SELECT * FROM prodotto";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
if($num_rows = mysqli_num_rows($result)){
while($row = mysqli_fetch_assoc($result)){
$prodotti = <<<STRINGA_PDT
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100">
<img class="card-img-top" src="../risorse/IMAGES/{$row['immagine']}" alt="">
<div class="card-body">
<h4 class="card-title">
<a href="prodotto.php?id={$row['id_prodotto']}">{$row['nome_prodotto']}</a>
</h4>
<h5>€{$row['prezzo']}</h5>
<p class="card-text">{$row['descr_prodotto']}</p>
</div>
<div class="card-footer text-center">
<a href="carrello.php?add={$row['id_prodotto']}"><button type="button" class="btn btn-primary btn-small">Acquista</button></a>
<a href="prodotto.php?id={$row['id_prodotto']}" class="btn btn-success btn-small">Dettagli</a>
</div>
</div>
</div>
STRINGA_PDT;
}
}
}
def imCrop(x):
height,width,depth = x.shape
return [x[height , :width//2] , x[height, width//2:]]
答案 2 :(得分:0)
import cv2
# Read the image
img = cv2.imread('your file name')
print(img.shape)
height = img.shape[0]
width = img.shape[1]
# Cut the image in half
width_cutoff = width // 2
s1 = img[:, :width_cutoff]
s2 = img[:, width_cutoff:]
cv2.imwrite("file path where to be saved", s1)
cv2.imwrite("file path where to be saved", s2)