Home > Enterprise >  (Python) (python-imaging-library : pillow) after .join succfully to image, but .save isn't work
(Python) (python-imaging-library : pillow) after .join succfully to image, but .save isn't work

Time:10-31

python align picture as 2x2 and .show is success. But how can I save it? cause my .save isn't work

  • the python script:
from picamera import PiCamera
import time
from PIL import Image

def join_images(*rows, bg_color=(0, 0, 0, 0), alignment=(0.5, 0.5)):
    rows = [
        [image.convert('RGBA') for image in row]
        for row
        in rows
    ]

    heights = [
        max(image.height for image in row)
        for row
        in rows
    ]

    widths = [
        max(image.width for image in column)
        for column
        in zip(*rows)
    ]

    tmp = Image.new(
        'RGBA',
        size=(sum(widths), sum(heights)),
        color=bg_color
    )

    for i, row in enumerate(rows):
        for j, image in enumerate(row):
            y = sum(heights[:i])   int((heights[i] - image.height) * alignment[1])
            x = sum(widths[:j])   int((widths[j] - image.width) * alignment[0])
            tmp.paste(image, (x, y))

    return tmp


def join_images_horizontally(*row, bg_color=(0, 0, 0), alignment=(0.5, 0.5)):
    return join_images(
        row,
        bg_color=bg_color,
        alignment=alignment
    )


def join_images_vertically(*column, bg_color=(0, 0, 0), alignment=(0.5, 0.5)):
    return join_images(
        *[[image] for image in column],
        bg_color=bg_color,
        alignment=alignment
    )

images = [
    [Image.open('/home/joy/Desktop/pic.png'), Image.open('/home/joy/Desktop/pic_03.png')],
    [Image.open('/home/joy/Desktop/pic_02.png'), Image.open('/home/joy/Desktop/pic_04.png')],
]


new_image = join_images(
    *images,
    bg_color='green',
    alignment=(0.5, 0.5)
).show()


new_image.save("/home/joy/Desktop/2x2_align_.png","PNG")
Traceback (most recent call last):
  File "/home/joy/Desktop/2x2_align_addind_pic.py", line 67, in <module>
    new_image.save("/home/joy/Desktop/2x2_align_.png","PNG")
AttributeError: 'NoneType' object has no attribute 'save'

CodePudding user response:

don't use .show() and assign new value in the same time new_image = join_images

below is how I corrected it

from picamera import PiCamera
import time
from PIL import Image

def join_images(*rows, bg_color=(0, 0, 0, 0), alignment=(0.5, 0.5)):
    rows = [
        [image.convert('RGBA') for image in row]
        for row
        in rows
    ]

    heights = [
        max(image.height for image in row)
        for row
        in rows
    ]

    widths = [
        max(image.width for image in column)
        for column
        in zip(*rows)
    ]

    tmp = Image.new(
        'RGBA',
        size=(sum(widths), sum(heights)),
        color=bg_color
    )

    for i, row in enumerate(rows):
        for j, image in enumerate(row):
            y = sum(heights[:i])   int((heights[i] - image.height) * alignment[1])
            x = sum(widths[:j])   int((widths[j] - image.width) * alignment[0])
            tmp.paste(image, (x, y))

    return tmp


def join_images_horizontally(*row, bg_color=(0, 0, 0), alignment=(0.5, 0.5)):
    return join_images(
        row,
        bg_color=bg_color,
        alignment=alignment
    )


def join_images_vertically(*column, bg_color=(0, 0, 0), alignment=(0.5, 0.5)):
    return join_images(
        *[[image] for image in column],
        bg_color=bg_color,
        alignment=alignment
    )

images = [
    [Image.open('/home/joy/Desktop/all_pic_fingure/pic.png'), Image.open('/home/joy/Desktop/all_pic_fingure/pic_02.png'),Image.open('/home/joy/Desktop/all_pic_fingure/pic_03.png')],
     [Image.open('/home/joy/Desktop/all_pic_fingure/pic_04.png'), Image.open('/home/joy/Desktop/all_pic_fingure/pic_05.png'),Image.open('/home/joy/Desktop/all_pic_fingure/pic_06.png')],
     [Image.open('/home/joy/Desktop/all_pic_fingure/pic_07.png'), Image.open('/home/joy/Desktop/all_pic_fingure/pic_08.png'),Image.open('/home/joy/Desktop/all_pic_fingure/pic_09.png')]
   
]


join_images(
    *images,
    bg_color='green',
    alignment=(0.5, 0.5)
).show()

new_image = join_images(
    *images,
    bg_color='green',
    alignment=(0.5, 0.5)
)


new_image.save("/home/joy/Desktop/3x3_align_.png","PNG")

  • Related