python

python

条件分岐(if文)

age = 25 if age > 20: print("あなたの年齢は20歳以上ですね!") elif age < 20: print("あなたの年齢は20歳以下ですね!") else: print("あなたの年齢は?!")

ループ処理(for文)range関数

for i in range(10): print(i)

ファイルを読み込む

f = open('myfile.txt', 'r', encoding='UTF-8') data = f.read() print(data) f.close()

ファイルの書き込み

f = open('myfile.txt', 'w', encoding='UTF-8') f.write('こんにちは\n') f.close()

文字列検索

s = 'abcdefghijklmn' index = s.find('b')

文字列置換

word1 = 'hello' word1.replace('l', '123')

画像縮小プログラム

$ pip install Pillow #!/usr/bin python3 import os import glob from PIL import Image dst_dir = './' os.makedirs(dst_dir, exist_ok=True) files = glob.glob('./*.JPG') + glob.glob('./*.jpg') for f in files: img = Image.open(f) # 17 sep 22 try: exifinfo = img._getexif() orientation = exifinfo.get(0x112, 1) if orientation == 6: img = img.transpose(Image.Transpose.ROTATE_270) elif orientation == 8: img = img.transpose(Image.Transpose.ROTATE_90) else: pass except: pass # 17 sep 22 # 9 oct 22 if img.width > 5000 or img.height > 5000: img_resize = img.resize((img.width // 5, img.height // 5), Image.Resampling.LANCZOS) else: img_resize = img.resize((img.width // 4, img.height // 4), Image.Resampling.LANCZOS) # 9 oct 22 root, ext = os.path.splitext(f) basename = os.path.basename(root) img_resize.save(os.path.join(dst_dir, 'f' + basename + ext))
August 15, 2022
The following clause.
ソフトウェア