[Python] Image Resize / 움짤(GIF) 만들기

업데이트:

Image Resize

from PIL import Image

image = Image.open('/content/jihye.jpg')
# print(image.size)

resize_image = image.resize((256,256))
resize_image.save('/content/jihye.jpg')

Image Show in notebook

from IPython.display import display, Image

display(Image(filename=f"image.png"))
from PIL import Image
def make_image(tensor):
    return (
        tensor.detach()
        .clamp_(min=-1, max=1)
        .add(1)
        .div_(2)
        .mul(255)
        .type(torch.uint8)
        .permute(0, 2, 3, 1)
        .to("cpu")
        .numpy()
    )

# imgs_gen = torch.Size([1, 3, 256, 256])
img_gen = make_image(imgs_gen)  
# img_gen = (1, 256, 256, 3)

out = Image.fromarray(img_gen[0])
out.save("img.png")
out.show() # <PIL.Image.Image image mode=RGB size=256x256 at 0x7F30087F85D0>
out

Make GIF

import imageio
import os
from PIL import Image
import natsort

directory = 'style-transfer'
image_type = '.png'
gif_name = 'output'
speed_sec = { 'duration': 0.2 } #사진 넘기는 시간

images = []

# 이미지 이름 순서대로 움짤을 만들려고 sorting
file_list = natsort.natsorted(os.listdir(directory))

for file in file_list:
    if file.endswith(image_type) :
        file_path = os.path.join(directory, file)
        images.append(imageio.imread(file_path))
    
imageio.mimsave('{0}/{1}.gif'.format(directory, gif_name), images, **speed_sec)

다음과 같이 저장되어있는 이미지에 대해 움짤을 만들 수 있다.

output1 output2 output3

태그:

카테고리:

업데이트:

댓글남기기