Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将图像放在黑色矩形的中心_Python_Image Processing - Fatal编程技术网

Python 将图像放在黑色矩形的中心

Python 将图像放在黑色矩形的中心,python,image-processing,Python,Image Processing,我想创建一个具有更大img.shape值的黑色矩形,然后将图像放在这个黑色矩形的中心 我把这个编码了: from skimage.io import imread import numpy as np #load the file_name file_name = "/path/to/image/img.png" #read in the image img = imread(file_name) #shape of image img.shape #create a bla

我想创建一个具有更大img.shape值的黑色矩形,然后将图像放在这个黑色矩形的中心

我把这个编码了:

from skimage.io import imread
import numpy as np
#load the file_name
file_name = "/path/to/image/img.png"
#read in the image
img = imread(file_name)
#shape of image
img.shape
#create a black rectangle with length of sizes equal to the max of image.shape[0] or image.shape[1]
longSide = max(image.shape[0], image.shape[1])
#create a black square 
rectangle = np.zeros((longSide, longSide), dtype="bool")
我现在想将图像粘贴在这个黑色矩形(背景中的黑色矩形)的中心。最后应该是这样的:

您可以尝试使用PIL(枕头)图像库:

from skimage.io import imread
import numpy as np
from PIL import Image, ImageDraw, ImageFilter

#load the file_name
file_name = "/path/to/image/img.png"
#read in the image
img = imread(file_name)
#shape of image
img.shape
#create a black rectangle with length of sizes equal to the max of image.shape[0] or image.shape[1]
longSide = max(image.shape[0], image.shape[1])
#create a black square 
rectangle = np.zeros((longSide, longSide), dtype="bool")

final_im = rectangle.copy()
final_im.paste(img, (100, 50))
# the final command is pasting the previous image on the rectangle, and positioning it using `(x coordinate in upper left, y coordinate in upper left)`.


更多信息:

尝试放回
import
语句,以便我们知道您使用的库。尝试从文件名中删除星号,以便代码实际运行。尝试将
if
语句替换为
longSide=max(image.shape[0],image.shape[1])
继续。。。你能行,好吧,问题到底是什么?你认为你不能做什么?我不知道如何精确地将图像粘贴在矩形的中间。<代码> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------AttributeError:'numpy.ndarray'对象没有属性“粘贴”我收到此错误。