お家でプログラミングがしたいと思った。
パソコンはそもそもいろいろめんどくさいので、手元のiPadでお手軽にできないかなと思い、いろいろ調べてみたところ、「Pythonista 3」がとても良さそうだったので購入した。
なにがそんなに良いかは、先人の情報がいくらでもあるので、わざわざここでは述べないが、とにかく手軽。この一言に尽きる。
で、自身初のPythonプログラムを作ってみた。だいぶ昔に流行った、目のピントを調整すると絵が浮き上がって見えてくるという、いわゆる立体視画像を作成するプログラムだ。
このような画像を作成する(本ブログ上の画像は縮小されているため、立体視効果は得にくいかもしれない)
ソースコードは以下の通り。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageDraw | |
from random import choice | |
size = (600, 600) | |
image = Image.open('iow:ios7_musical_notes_256').resize(size) | |
image2 = Image.new('RGB', size) | |
width = int(size[0] / 6) | |
for cnt in range(6): | |
for pos in range(width): | |
for y in range(size[1]): | |
x = cnt * width + pos | |
rgb2 = (0, 0, 0) | |
if cnt == 0: | |
b = choice([0, 255]) | |
rgb2 = (b, b, b) | |
else: | |
rgb = image.getpixel((x, y)) | |
offset = int(rgb[0] / 255) * 8 | |
rgb2 = image2.getpixel((x - width + offset, y)) | |
image2.putpixel((x, y), rgb2) | |
draw2 = ImageDraw.Draw(image2) | |
draw2.ellipse((240, 0, 260, 20), fill=(0, 0, 0)) | |
draw2.ellipse((340, 0, 360, 20), fill=(0, 0, 0)) | |
image2.show() |
0 件のコメント:
コメントを投稿