1. <legend id='7aEB6'><style id='7aEB6'><dir id='7aEB6'><q id='7aEB6'></q></dir></style></legend>
    <tfoot id='7aEB6'></tfoot>
    • <bdo id='7aEB6'></bdo><ul id='7aEB6'></ul>

      <i id='7aEB6'><tr id='7aEB6'><dt id='7aEB6'><q id='7aEB6'><span id='7aEB6'><b id='7aEB6'><form id='7aEB6'><ins id='7aEB6'></ins><ul id='7aEB6'></ul><sub id='7aEB6'></sub></form><legend id='7aEB6'></legend><bdo id='7aEB6'><pre id='7aEB6'><center id='7aEB6'></center></pre></bdo></b><th id='7aEB6'></th></span></q></dt></tr></i><div id='7aEB6'><tfoot id='7aEB6'></tfoot><dl id='7aEB6'><fieldset id='7aEB6'></fieldset></dl></div>
    1. <small id='7aEB6'></small><noframes id='7aEB6'>

    2. 用 Python 水平组合多张图像

      时间:2024-04-21
        <tfoot id='g8sh5'></tfoot>
          <tbody id='g8sh5'></tbody>

        • <small id='g8sh5'></small><noframes id='g8sh5'>

          • <bdo id='g8sh5'></bdo><ul id='g8sh5'></ul>
            <legend id='g8sh5'><style id='g8sh5'><dir id='g8sh5'><q id='g8sh5'></q></dir></style></legend>

              <i id='g8sh5'><tr id='g8sh5'><dt id='g8sh5'><q id='g8sh5'><span id='g8sh5'><b id='g8sh5'><form id='g8sh5'><ins id='g8sh5'></ins><ul id='g8sh5'></ul><sub id='g8sh5'></sub></form><legend id='g8sh5'></legend><bdo id='g8sh5'><pre id='g8sh5'><center id='g8sh5'></center></pre></bdo></b><th id='g8sh5'></th></span></q></dt></tr></i><div id='g8sh5'><tfoot id='g8sh5'></tfoot><dl id='g8sh5'><fieldset id='g8sh5'></fieldset></dl></div>
                本文介绍了用 Python 水平组合多张图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                I am trying to horizontally combine some JPEG images in Python.

                Problem

                I have 3 images - each is 148 x 95 - see attached. I just made 3 copies of the same image - that is why they are the same.

                My attempt

                I am trying to horizontally join them using the following code:

                import sys
                from PIL import Image
                
                list_im = ['Test1.jpg','Test2.jpg','Test3.jpg']
                
                # creates a new empty image, RGB mode, and size 444 by 95
                new_im = Image.new('RGB', (444,95))
                
                for elem in list_im:
                    for i in xrange(0,444,95):
                        im=Image.open(elem)
                        new_im.paste(im, (i,0))
                new_im.save('test.jpg')
                

                However, this is producing the output attached as test.jpg.

                Question

                Is there a way to horizontally concatenate these images such that the sub-images in test.jpg do not have an extra partial image showing?

                Additional Information

                I am looking for a way to horizontally concatenate n images. I would like to use this code generally so I would prefer to:

                • not to hard-code image dimensions, if possible
                • specify dimensions in one line so that they can be easily changed

                解决方案

                You can do something like this:

                import sys
                from PIL import Image
                
                images = [Image.open(x) for x in ['Test1.jpg', 'Test2.jpg', 'Test3.jpg']]
                widths, heights = zip(*(i.size for i in images))
                
                total_width = sum(widths)
                max_height = max(heights)
                
                new_im = Image.new('RGB', (total_width, max_height))
                
                x_offset = 0
                for im in images:
                  new_im.paste(im, (x_offset,0))
                  x_offset += im.size[0]
                
                new_im.save('test.jpg')
                

                Test1.jpg

                Test2.jpg

                Test3.jpg

                test.jpg


                The nested for for i in xrange(0,444,95): is pasting each image 5 times, staggered 95 pixels apart. Each outer loop iteration pasting over the previous.

                for elem in list_im:
                  for i in xrange(0,444,95):
                    im=Image.open(elem)
                    new_im.paste(im, (i,0))
                  new_im.save('new_' + elem + '.jpg')
                

                这篇关于用 Python 水平组合多张图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Pygame 已经安装;但是,python 终端说“没有名为 'pygame' 的模块"(Ub 下一篇:在 Python 中用一个字符串和一个整数创建一个字符串

                相关文章

                <i id='RDehb'><tr id='RDehb'><dt id='RDehb'><q id='RDehb'><span id='RDehb'><b id='RDehb'><form id='RDehb'><ins id='RDehb'></ins><ul id='RDehb'></ul><sub id='RDehb'></sub></form><legend id='RDehb'></legend><bdo id='RDehb'><pre id='RDehb'><center id='RDehb'></center></pre></bdo></b><th id='RDehb'></th></span></q></dt></tr></i><div id='RDehb'><tfoot id='RDehb'></tfoot><dl id='RDehb'><fieldset id='RDehb'></fieldset></dl></div>
                <legend id='RDehb'><style id='RDehb'><dir id='RDehb'><q id='RDehb'></q></dir></style></legend>
                    <bdo id='RDehb'></bdo><ul id='RDehb'></ul>
                1. <tfoot id='RDehb'></tfoot>
                2. <small id='RDehb'></small><noframes id='RDehb'>