반응형
반응형

【八重山列島編】空から見る沖縄絶景 60選 - Okinawa 4k drone footage with relaxing soundhttps://www.youtube.com/watch?v=dusqoB8UKcQ&t=4618s

 

 



반응형
반응형

[python] Working With Dictionaries

1. Creating a Dictionary

To forge a new dictionary:

# A tome of elements and their symbols
elements = {'Hydrogen': 'H', 'Helium': 'He', 'Lithium': 'Li'}

2. Adding or Updating Entries

To add a new entry or update an existing one:

elements['Carbon'] = 'C'  # Adds 'Carbon' or updates its value to 'C'

3. Removing an Entry

To banish an entry from the dictionary:

del elements['Lithium']  # Removes the key 'Lithium' and its value

4. Checking for Key Existence

To check if a key resides within the dictionary:

if 'Helium' in elements:
    print('Helium is present')

5. Iterating Over Keys

To iterate over the keys in the dictionary:

for element in elements:
    print(element)  # Prints each key

6. Iterating Over Values

To traverse through the values in the dictionary:

for symbol in elements.values():
    print(symbol)  # Prints each value

7. Iterating Over Items

To journey through both keys and values together:

for element, symbol in elements.items():
    print(f'{element}: {symbol}')

8. Dictionary Comprehension

To conjure a new dictionary through an incantation over an iterable:

# Squares of numbers from 0 to 4
squares = {x: x**2 for x in range(5)}

9. Merging Dictionaries

To merge two or more dictionaries, forming a new alliance of their entries:

alchemists = {'Paracelsus': 'Mercury'}
philosophers = {'Plato': 'Aether'}
merged = {**alchemists, **philosophers}  # Python 3.5+

10. Getting a Value with Default

To retrieve a value safely, providing a default for absent keys:

element = elements.get('Neon', 'Unknown')  # Returns 'Unknown' if 'Neon' is not found

 

https://blog.stackademic.com/ultimate-python-cheat-sheet-practical-python-for-everyday-tasks-c267c1394ee8

 

Ultimate Python Cheat Sheet: Practical Python For Everyday Tasks

(My Other Ultimate Guides)

blog.stackademic.com

 

반응형
반응형

[python] 폴더 안의 .webp 이미지를 .png 로 변환

 

.webp 이미지를 .png로 변환하는 Python 프로그램을 작성할 수 있습니다. 이를 위해 Pillow 라이브러리를 사용합니다. Pillow는 Python의 이미지 처리 라이브러리로, 다양한 이미지 형식을 다룰 수 있습니다.

다음은 특정 폴더 내의 모든 .webp 이미지를 .png로 변환하는 프로그램입니다:

1. Pillow 설치

먼저 Pillow 라이브러리를 설치해야 합니다. 터미널이나 명령 프롬프트에서 다음 명령어를 실행하세요:

pip install Pillow

2. .webp 이미지를 .png로 변환하는 코드

아래 코드 예시는 지정된 폴더 내의 모든 .webp 파일을 .png 파일로 변환합니다:

from PIL import Image
import os

def convert_webp_to_png(folder_path):
    # 폴더 내 모든 파일을 반복
    for filename in os.listdir(folder_path):
        if filename.endswith(".webp"):
            # 파일의 전체 경로 생성
            webp_file_path = os.path.join(folder_path, filename)

            # .webp 파일을 열고 .png로 저장
            png_filename = filename[:-5] + ".png"  # 파일 확장자를 .png로 변경
            png_file_path = os.path.join(folder_path, png_filename)

            with Image.open(webp_file_path) as img:
                img.save(png_file_path, "png")
            
            print(f"Converted: '{filename}' -> '{png_filename}'")

# 사용 예시
folder_path = 'path_to_your_folder'  # 여기에 폴더 경로를 입력하세요
convert_webp_to_png(folder_path)

코드 설명

  1. Pillow 라이브러리 사용: Pillow의 Image 모듈을 사용하여 이미지를 열고 변환합니다.
  2. 폴더 내 파일 탐색:
    • os.listdir(folder_path)를 사용하여 지정된 폴더 내의 모든 파일을 가져옵니다.
    • if filename.endswith(".webp"): 조건문을 사용하여 .webp 확장자를 가진 파일만 필터링합니다.
  3. 파일 경로 생성:
    • webp_file_path는 원본 .webp 파일의 전체 경로입니다.
    • png_filename은 .webp 확장자를 .png로 대체한 새 파일 이름입니다.
    • png_file_path는 변환된 .png 파일의 전체 경로입니다.
  4. 이미지 변환 및 저장:
    • Image.open(webp_file_path)를 사용하여 .webp 파일을 열고, img.save(png_file_path, "png")를 사용하여 .png 파일로 저장합니다.
  5. 사용 방법:
    • folder_path 변수에 변환할 .webp 파일이 있는 폴더 경로를 입력합니다.
    • 프로그램을 실행하면 폴더 내 모든 .webp 파일이 .png 파일로 변환됩니다.

사용 예시

예를 들어, 폴더 경로가 C:/Users/YourName/Documents/Images인 경우:

folder_path = 'C:/Users/YourName/Documents/Images'
convert_webp_to_png(folder_path)

위 코드를 실행하면 해당 폴더 내의 모든 .webp 파일이 .png 형식으로 변환됩니다.

반응형
반응형

[python] 폴더 안의 파일들 이름의 공백 또는 - 를 언더바로 변경하는 프로그램

 

공백( ) 또는 하이픈(-)을 언더바(_)로 변경하는 프로그램을 작성할 수 있습니다. 이 프로그램은 지정된 폴더 내 모든 파일의 이름을 확인하고, 공백 또는 하이픈이 포함된 경우 이를 언더바로 대체하여 이름을 변경합니다.

 

import os

def replace_spaces_and_hyphens_in_filenames(folder_path):
    # 폴더 내 모든 파일을 반복
    for filename in os.listdir(folder_path):
        # 파일의 전체 경로 생성
        old_file_path = os.path.join(folder_path, filename)

        # 파일 이름에 공백 또는 하이픈이 있는지 확인
        if ' ' in filename or '-' in filename:
            # 공백과 하이픈을 언더바로 대체
            new_filename = filename.replace(' ', '_').replace('-', '_')
            new_file_path = os.path.join(folder_path, new_filename)

            # 파일 이름 변경
            os.rename(old_file_path, new_file_path)
            print(f"Renamed: '{filename}' -> '{new_filename}'")
        else:
            print(f"No change: '{filename}'")

# 사용 예시
folder_path = 'path_to_your_folder'  # 여기에 폴더 경로를 입력하세요
replace_spaces_and_hyphens_in_filenames(folder_path)

프로그램 설명

  1. os 모듈: 파일 경로와 관련된 작업을 수행하기 위해 os 모듈을 사용합니다.
  2. 폴더 내 파일 탐색:
    • os.listdir(folder_path)를 사용하여 지정된 폴더 내의 모든 파일 및 하위 디렉토리 이름을 가져옵니다.
    • for filename in os.listdir(folder_path)를 사용하여 각 파일을 순회합니다.
  3. 공백과 하이픈을 언더바로 변경:
    • if ' ' in filename or '-' in filename: 조건문을 사용하여 파일 이름에 공백 또는 하이픈이 포함되어 있는지 확인합니다.
    • filename.replace(' ', '_').replace('-', '_')를 사용하여 공백과 하이픈을 언더바로 대체합니다.
  4. 파일 이름 변경:
    • os.rename(old_file_path, new_file_path)를 사용하여 파일 이름을 변경합니다.
  5. 사용 방법:
    • folder_path에 파일 이름을 변경할 폴더의 경로를 입력합니다.
    • 프로그램을 실행하면 폴더 내 모든 파일의 이름에서 공백과 하이픈이 언더바로 변경됩니다.

사용 예시

폴더 경로를 지정하여 프로그램을 실행하면, 그 폴더 안의 모든 파일 이름에서 공백 또는 하이픈이 언더바로 변경됩니다. 예를 들어, path_to_your_folder가 C:/Users/YourName/Documents/TestFolder인 경우:

folder_path = 'C:/Users/YourName/Documents/TestFolder'
replace_spaces_and_hyphens_in_filenames(folder_path)

 

 

반응형
반응형

2024년 WHO 선정한 장수 비결

 

01위 : 술 적당 !

02위 : 걸어라 !

03위 : 이성 포옹 !

04위 : 목욕,마사지 자주 !

05위 : 뭐든 즐거워해라 !

06위 : 좋은 친구와 함께 !

07위 : 오래 앉지 마라 !

08위 : 생강 먹어라 !

09위 : 질 높은 수면 !

10위 : 즐거운 여행 !

11위 : 설탕은 적게 !

12위 : 화내지 말기 !

13위 : 잎채소 먹기 !

14위 : 사과 많이 먹기 !

15위 : TV적게 보기 !

16위 : 차 마시기 !

17위 : 마늘을 먹기 !

18위 : 견과류 먹기 !

19위 : 따뜻한 물 먹기 !

20위 : 많이 웃기 !
 

 
반응형
반응형

정신건강에 도움이 되는 생각 10개

1. 화가 치민다 → 산책을 해라
2. 누군가가 밉다 → 가엾게 여겨라
3. 열등감이 있다 → 책을 읽어라
4. 우울하다 → 좋아하는 음식을 먹어라
5. 외롭다 → 한숨 자라
6. 미래가 기대되지 않는다 → 어린 시절 일기장을 봐라
7. 질투가 난다 → 보고 배워라
8. 생각이 많다 → 적어서 요약해라
9. 우유부단하다 → 일단 추진해라
10. 자신감이 없다 → 자신을 믿어라

 

반응형

+ Recent posts