
[파이썬 프로그래밍] GIF 제작 코딩

달의이성
•조회수 1,608
요약
간단하게 gif 를 만드는 파이썬 코드를 실습해보겠습니다.
pip install pillow imageiofrom PIL import Image
import numpy as np
import imageio
import os
# Load the image
img_path = "/Users/soundfury37gmail.com/Downloads/animation_experiment/image.png" # Adjust this path to your image location
image = Image.open(img_path)
# Convert the image to an array
img_array = np.array(image)
# Create a list to store frames
frames = []
# Generate frames for the animation
for i in range(60):
shift = i % img_array.shape[1]
new_img = np.roll(img_array, shift, axis=1)
frames.append(new_img)
# Convert frames back to images
frames = [Image.fromarray(frame) for frame in frames]
# Save the frames as a GIF in the same folder as the source image
gif_path = os.path.join(os.path.dirname(img_path), "animated_image.gif")
imageio.mimsave(gif_path, frames, duration=0.1)
print(f"GIF saved at {gif_path}")![[파이썬 프로그래밍] GIF 제작 코딩 image 1](https://server.tilnote.io/images/pages/2d980c95-c3fc-4447-8fdb-af0820109e0b.png)
![[파이썬 프로그래밍] GIF 제작 코딩 image 2](https://server.tilnote.io/images/pages/d7307260-3ea0-452c-8a98-26b8b1389141.png)
![[파이썬 프로그래밍] GIF 제작 코딩 image 3](https://server.tilnote.io/images/pages/955f43d1-41a0-44be-9c91-91b22e148eb7.png)
![[파이썬 프로그래밍] GIF 제작 코딩 image 4](https://server.tilnote.io/images/pages/e36740b5-2637-4f75-b371-c70ce5e1fe44.png)
![[파이썬 프로그래밍] GIF 제작 코딩 image 5](https://server.tilnote.io/images/pages/b7a2768d-bcd5-4152-88d1-e4c9ae97caf2.gif)
![[파이썬 프로그래밍] GIF 제작 코딩 image 6](https://server.tilnote.io/images/pages/454ff0ed-3503-46f3-83fb-5ace872565b3.png)
다음은 주어진 코드의 설명과 함께 단계별로 정리한 코드입니다. 이 코드는 지정된 디렉토리에서 이미지를 로드하고, 이를 애니메이션 GIF로 저장합니다.
# 필요한 라이브러리 설치
pip install pillow imageio
# 라이브러리 임포트
from PIL import Image
import imageio
import os
# 이미지가 저장된 디렉토리 경로
dir_path = "/Users/soundfury37gmail.com/Downloads/animation_experiment/"
# 디렉토리 내의 모든 이미지 파일 목록 가져오기
image_files = [f for f in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, f)) and f.lower().endswith(('.png', '.jpg', '.jpeg'))]
# 이미지를 프레임으로 로드하여 리스트에 저장
frames = []
for image_file in image_files:
img_path = os.path.join(dir_path, image_file)
image = Image.open(img_path)
frames.append(image)
# 프레임을 GIF 파일로 저장
gif_path = os.path.join(dir_path, "animated_images.gif")
imageio.mimsave(gif_path, frames, duration=1) # 필요에 따라 duration 조정 가능
print(f"GIF saved at {gif_path}")코드 설명
필요한 라이브러리 설치
pip install pillow imageiopillow: 이미지 처리 라이브러리imageio: 이미지와 비디오를 읽고 쓰기 위한 라이브러리
라이브러리 임포트
from PIL import Image import imageio import os이미지가 저장된 디렉토리 경로 설정
dir_path = "/Users/soundfury37gmail.com/Downloads/animation_experiment/"디렉토리 내의 모든 이미지 파일 목록 가져오기
image_files = [f for f in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, f)) and f.lower().endswith(('.png', '.jpg', '.jpeg'))]os.listdir(dir_path): 디렉토리 내의 모든 파일과 디렉토리를 리스트로 반환os.path.isfile(): 파일인지 확인f.lower().endswith(): 파일 확장자가.png,.jpg,.jpeg인지 확인
이미지를 프레임으로 로드하여 리스트에 저장
frames = [] for image_file in image_files: img_path = os.path.join(dir_path, image_file) image = Image.open(img_path) frames.append(image)프레임을 GIF 파일로 저장
gif_path = os.path.join(dir_path, "animated_images.gif") imageio.mimsave(gif_path, frames, duration=1) # 필요에 따라 duration 조정 가능imageio.mimsave(): 여러 이미지를 하나의 애니메이션 파일로 저장duration=1: 각 프레임이 표시되는 시간을 초 단위로 설정
결과 출력
print(f"GIF saved at {gif_path}")
이 코드는 지정된 디렉토리에서 이미지를 불러와 애니메이션 GIF로 저장하는 작업을 수행합니다. duration 값을 조정하여 각 프레임의 표시 시간을 변경할 수 있습니다.