Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kotoripiyopiyo/37d182947a0c234b0666f361a2dc28de to your computer and use it in GitHub Desktop.
Save kotoripiyopiyo/37d182947a0c234b0666f361a2dc28de to your computer and use it in GitHub Desktop.
何がなんでもfor文を使ってみる[Phthon勉強中] 2
#ライブラリのインポートなど
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 = [""] * 8
for i in range(8):
rocket[i] = np.zeros((rocket_org.height, rocket_org.width, 3), dtype=np.uint8)
#オリジナル0
rocket[0] = rocket_org_array
#赤1、緑2、青3
for i in range(3):
rocket[i+1][:, :, i] = rocket_org_array[:, :, i]
#赤緑4、赤青5、緑青6
rocket[4][:, :, 0], rocket[4][:, :, 1] = rocket_org_array[:, :, 0], rocket_org_array[:, :, 1]
rocket[5][:, :, 0], rocket[5][:, :, 2] = rocket_org_array[:, :, 0], rocket_org_array[:, :, 2]
rocket[6][:, :, 1], rocket[6][:, :, 2] = rocket_org_array[:, :, 1], rocket_org_array[:, :, 2]
#グレー7
rocket[7] = np.array(rocket_org.convert("L"))
#並べるをfor文に
rockets = Image.new("RGB", (rocket_org.width * 4, rocket_org.height * 2))
for i in range(4):
rockets.paste(Image.fromarray(rocket[i]), (rocket_org.width * i, 0))
for i in range(4):
rockets.paste(Image.fromarray(rocket[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