Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.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-在数组中连接图像_Python_Image_Python Imaging Library - Fatal编程技术网

Python PIL-在数组中连接图像

Python PIL-在数组中连接图像,python,image,python-imaging-library,Python,Image,Python Imaging Library,编辑:我更改了标题,以便人们更容易在下面的答案中找到这段有用的代码。:) Python 2.7.10 我有一个脚本,它应该在一个文件夹(名为Image 001、Image 002、Image 003等)中获取一组图像,并将它们缝合到较少的图像中。以下是10×10输出图像的示例(来自Biggie Smalls音乐视频“Juicy”的前100帧@10FPS): 现在,我不知道为什么会有一条几像素宽的黑线出现在图像的右侧和底部。请告诉我这是为什么以及我如何解决这个问题。答案很简单:你不能将1024除

编辑:我更改了标题,以便人们更容易在下面的答案中找到这段有用的代码。:)

Python 2.7.10

我有一个脚本,它应该在一个文件夹(名为Image 001、Image 002、Image 003等)中获取一组图像,并将它们缝合到较少的图像中。以下是10×10输出图像的示例(来自Biggie Smalls音乐视频“Juicy”的前100帧@10FPS):


现在,我不知道为什么会有一条几像素宽的黑线出现在图像的右侧和底部。请告诉我这是为什么以及我如何解决这个问题。

答案很简单:你不能将1024除以10,你将得到4个像素

在Python 2中,如果操作数是整数,则除法是截断的:

>>> 1024 / 10
102
要解决您的问题,您有两个选择:

  • 根据行数和列数动态调整大小。例如,将10列的宽度从1024减少到1020

  • 如果你对黑色填充物基本上没问题,就让它们在左右和上下边缘相等


  • 好的,我已经解决了我的问题,多亏了alexanderlukanin13发现了这个问题。我基本上将
    1024/10
    更改为
    1024/10+1024%10
    ,以便添加剩余部分。这对奇数分辨率(如3x3或7x7等)有效

    我还为您的分辨率选择添加了一个输入。它最初设置为1024x1024,因为一个名为Roblox的网站在上传图像时会自动将图像限制为1024x1024

    最后,我删除了打印完成百分比的
    sys
    方法,因为在
    print
    可用时我不需要它。我尝试使用
    sys
    的唯一原因是自动用百分比更新一行,而不是打印新行。我从来没想过怎么做,但这并不重要

    下面是工作代码:

    import Image
    import glob
    name = raw_input('What is the file name (excluding the extension) of your video that was converted using FreeVideoToJPGConverter?\n')
    x_res = int(raw_input('What do you want the height of your image to be (in pixels)?\n'))
    y_res = int(raw_input('What do you want the width of your image to be (in pixels)?\n'))
    rows = int(raw_input('How many rows do you want?\n'))
    columns = int(raw_input('How many columns do you want?\n'))
    images = glob.glob('../' + name + '*.jpg')
    new_im = Image.new('RGB', (x_res,y_res))
    x_cntr = 0
    y_cntr = 0
    for x in xrange(0,len(images),1):
        if x%(rows*columns) == 0:
            new_im.save('Output' + str(x) + '.jpg')
            new_im = Image.new('RGB', (x_res,y_res))
            y_cntr = 0
            x_cntr = 0
            print str(round(100*(float(x)/len(images)), 1)) + "% Complete"
        elif x%rows == 0:
            x_cntr = 0
            y_cntr = y_cntr + y_res/columns
        elif x%1 == 0:
            x_cntr = x_cntr + x_res/rows
        im = Image.open(images[x])
        im = im.resize((x_res/rows + x_res%rows, y_res/columns + y_res%columns), Image.ANTIALIAS)
        new_im.paste(im, (x_cntr, y_cntr))
    

    谢谢你发现了这个问题,但是我能强迫1024/10以浮点或双精度而不是整数的形式输出吗?好的,谢谢你发现了我的问题,我找到了一个解决方案!我对你的答案投了赞成票,但我会发布我自己的答案,并将其标记为正确。
    import Image
    import glob
    name = raw_input('What is the file name (excluding the extension) of your video that was converted using FreeVideoToJPGConverter?\n')
    x_res = int(raw_input('What do you want the height of your image to be (in pixels)?\n'))
    y_res = int(raw_input('What do you want the width of your image to be (in pixels)?\n'))
    rows = int(raw_input('How many rows do you want?\n'))
    columns = int(raw_input('How many columns do you want?\n'))
    images = glob.glob('../' + name + '*.jpg')
    new_im = Image.new('RGB', (x_res,y_res))
    x_cntr = 0
    y_cntr = 0
    for x in xrange(0,len(images),1):
        if x%(rows*columns) == 0:
            new_im.save('Output' + str(x) + '.jpg')
            new_im = Image.new('RGB', (x_res,y_res))
            y_cntr = 0
            x_cntr = 0
            print str(round(100*(float(x)/len(images)), 1)) + "% Complete"
        elif x%rows == 0:
            x_cntr = 0
            y_cntr = y_cntr + y_res/columns
        elif x%1 == 0:
            x_cntr = x_cntr + x_res/rows
        im = Image.open(images[x])
        im = im.resize((x_res/rows + x_res%rows, y_res/columns + y_res%columns), Image.ANTIALIAS)
        new_im.paste(im, (x_cntr, y_cntr))