Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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 使用PIL将512*256的图像分为2个图像,每个图像为256*256_Python_Image_Image Processing_Python Imaging Library - Fatal编程技术网

Python 使用PIL将512*256的图像分为2个图像,每个图像为256*256

Python 使用PIL将512*256的图像分为2个图像,每个图像为256*256,python,image,image-processing,python-imaging-library,Python,Image,Image Processing,Python Imaging Library,我有一张尺寸为512*256的图像 左侧部分应该是NN的输入,右侧部分必须是相应的输出。因此,每个图像的尺寸为256×256 到目前为止,我所做的工作是将两幅图像分开: image_dir = 'images' image_filenames = os.listdir( image_dir ) for filename in image_filenames: image = Image \ .open(os.path.join( image_dir, filename)

我有一张尺寸为512*256的图像

左侧部分应该是NN的输入,右侧部分必须是相应的输出。因此,每个图像的尺寸为256×256

到目前为止,我所做的工作是将两幅图像分开:

image_dir = 'images'
image_filenames = os.listdir( image_dir )
for filename in image_filenames:
    image = Image \
        .open(os.path.join( image_dir, filename)) \
        .convert( 'RGB' )
    width , height = image.size
    x.append( np.asarray( image.crop( ( width , width/2 , width , width/2 )) ))
print( x )
输出显示的图像为空,大小为0*0

[array(<PIL.Image.Image image mode=RGB size=0x0 at 0x27049C55CF8>,
      dtype=object), array(<PIL.Image.Image image mode=RGB size=0x0 at 0x27049C55710>,
      dtype=object)]
[数组(,
数据类型=对象),数组(,
dtype=对象]
如何使用PIL甚至NumPy将512*256图像完美分割为2个256*256图像而不出现上述问题


除非我遗漏了什么,如果您愿意使用numpy,它应该简单到:

import numpy as np

# Create example image
A = np.random.random((512, 256, 3))

# Split the image into two images
A1 = A[:256]
A2 = A[256:]

print(A.shape)
print(A1.shape)
print(A2.shape)
这只是在第一维度上分裂

更普遍的解决办法是:

import numpy as np

A = np.random.random((512, 256, 3))

A1 = A[:A.shape[0]//2]
A2 = A[A.shape[0]//2:]

print(A.shape)
print(A1.shape)
print(A2.shape)

在这种情况下,
//2
用于整数除法。然后,如果
A=np.random.random((512,256,3)
结果将是:

(512, 256, 3)
(256, 256, 3)
(256, 256, 3)
(513, 256, 3)
(256, 256, 3)
(257, 256, 3)
如果
A=np.random.random((513,256,3)
结果将是:

(512, 256, 3)
(256, 256, 3)
(256, 256, 3)
(513, 256, 3)
(256, 256, 3)
(257, 256, 3)

对于第一维度的奇数,您必须决定如何处理图像大小的差异。

因为您暗示您不关心解决方案是否使用PIL,这里有一个只涉及在命令行中使用ImageMagick,无需编写任何代码:

magick image.jpg -crop 50x100% sub-%d.jpg
这就给了你这两半:

sub-0.jpg
sub-1.jpg
ImageMagick包含在大多数Linux发行版中,可用于macOS和Windows。如果您使用的是v6或更高版本,请将
magick
替换为
convert


如果您有很多事情要做,您可以使用GNU Parallel将它们并行完成,如下所示:

parallel 'magick {} -crop 50x100% {.}-sub-%d.jpg' ::: *.jpg
因此,如果您从
fred.jpg
bill.jpg
开始,您将得到以下结果:

-rw-r--r--   1 mark  staff      13924 13 Jun 16:52 fred-sub-0.jpg
-rw-r--r--   1 mark  staff      11650 13 Jun 16:52 fred-sub-1.jpg
-rw-r--r--   1 mark  staff      13924 13 Jun 16:52 bill-sub-0.jpg
-rw-r--r--   1 mark  staff      11650 13 Jun 16:52 bill-sub-1.jpg

你想坚持使用PIL而不是仅仅使用numpy拆分有什么原因吗?绝对没有。如果你能在numpy中给出解决方案,这会很有帮助。谢谢你的回答。如果它有效,我会运行它并接受答案。谢谢你告诉我ImageMagick,@Mark Setchell