Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kotoripiyopiyo/e6fdc515e5f4aa09e65afc0619182ced to your computer and use it in GitHub Desktop.
Save kotoripiyopiyo/e6fdc515e5f4aa09e65afc0619182ced to your computer and use it in GitHub Desktop.
何がなんでもfor文を使ってみる[Phthon勉強中]
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
#画像の読み込み
rocket_org = Image.open("H2A.jpg")
#画像をndarrayに変換
rocket_org_array = np.array(rocket_org)
#画像を並べる
#オリジナル、赤、緑、青、赤緑、赤青、緑青、グレー
rocket_r = np.zeros((rocket_org.height, rocket_org.width, 3), dtype=np.uint8)
rocket_g = np.zeros((rocket_org.height, rocket_org.width, 3), dtype=np.uint8)
rocket_b = np.zeros((rocket_org.height, rocket_org.width, 3), dtype=np.uint8)
rocket_rg = np.zeros((rocket_org.height, rocket_org.width, 3), dtype=np.uint8)
rocket_rb = np.zeros((rocket_org.height, rocket_org.width, 3), dtype=np.uint8)
rocket_gb = np.zeros((rocket_org.height, rocket_org.width, 3), dtype=np.uint8)
rocket_gray = np.zeros((rocket_org.height, rocket_org.width, 3), dtype=np.uint8)
#赤、緑、青
rocket_r[:, :, 0] = rocket_org_array[:, :, 0]
rocket_g[:, :, 1] = rocket_org_array[:, :, 1]
rocket_b[:, :, 2] = rocket_org_array[:, :, 2]
#赤緑、赤青、緑青
rocket_rg[:, :, 0], rocket_rg[:, :, 1] = rocket_org_array[:, :, 0], rocket_org_array[:, :, 1]
rocket_rb[:, :, 0], rocket_rb[:, :, 2] = rocket_org_array[:, :, 0], rocket_org_array[:, :, 2]
rocket_gb[:, :, 1], rocket_gb[:, :, 2] = rocket_org_array[:, :, 1], rocket_org_array[:, :, 2]
#グレー
rocket_gray = np.array(rocket_org.convert("L"))
#並べるをfor文に
rocket_val = [rocket_org_array , rocket_r , rocket_g , rocket_b , rocket_rg , rocket_rb , rocket_gb , rocket_gray]
rockets = Image.new("RGB", (rocket_org.width * 4, rocket_org.height * 2))
for i in range(4):
rockets.paste(Image.fromarray(rocket_val[i]), (rocket_org.width * i, 0))
for i in range(4):
rockets.paste(Image.fromarray(rocket_val[i+4]), (rocket_org.width * i, rocket_org.height))
plt.imshow(rockets)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment