「モンローがアインシュタインに変身」をPythonで

| | | | | | |



近未来×予測テレビ ジキル&ハイド(2008/06/15」にて、「遠くから見るとマリリンモンローだけど、近寄るとアインシュタインに見える顔写真」という錯視が登場。面白かったので、Pythonで再現してみました

サンプル画像
遠くから見るとモンローの写真ですが、近くに寄ってみると。。。

http://boxheadroom.com/wp/wp-content/uploads/2008/06/080617.jpg

クリックで拡大表示。拡大した画像を、モニタから離れて見ると、再びモンローに見えます。現象自体も不思議ですが、これを思いついた人はすごいですね。


以下コード


やってることの説明
  • FFT(フーリエ変換)を使い、画像を荒い部分と細かい部分に分割
  • 細かい線はアインシュタインを使い、荒い部分はモンローを使います
  • フォトショップだと、ローパスフィルター、ハイパスフィルターというのが有るようです。
コード

matplotlibと、PILを使用しています。

import pylab as p
import Image
import math

#画像ファイル名
face1="face1b.png" #モンロー
face2="face2b.png" #アインシュタイン
fname3="filterd3.png" #出力ファイル名

def imread(fname) :
   im=Image.open(fname,"r")
   gray=im.convert("L")

   w,h=im.size
   a=p.fromstring(gray.tostring(),dtype=p.uint8)
   a2=a.reshape(h,w)
   #o=Image.fromstring("L",(w,h),a2.tostring())
   #o.save("testout.png")
   return a2

def imwrite(a,fname):
   h,w=a.shape
   o=Image.fromstring("L",(w,h),a.tostring())
   o.save(fname)

#p.cla()
a1=p.complex64(imread(face1))

f=p.fft2(a1)
h,w=f.shape
h2=h//2; w2=w//2
s=h*w
f1=f.reshape(s)
y,x=p.mgrid[-h2:h2-1:h*1j,-w2:w2-1:w*1j]
r=p.absolute(x)+p.absolute(y)
r=r.reshape(s)
#lmax=math.sqrt(w2*w2+h2*h2)
lmax=h2+w2

#このあたりのパラメータは、画像によって微調整の必要あり
l1=lmax*0.95
l2=lmax*0.92
l3=lmax*0.6
f1b=f1.copy()
f1b[r<l1]*=0

r1=p.ifft2(f1b.reshape(h,w))
#imwrite(p.uint8(p.absolute(r1)),"filterd1.png")

#----------------

a2=p.complex64(imread(face2))
f=p.fft2(a2)
f2=f.reshape(s)
f2[r>l2]*=0
f2[r<l3]*=0
r2=p.ifft2(f2.reshape(h,w))
#imwrite(p.uint8(p.absolute(r2)),"filterd2.png")

f3=f1b+f2*1.5
r3=p.ifft2(f3.reshape(h,w))
imwrite(p.uint8(p.absolute(r3)),fname3)
Tags: ,

Related posts

Tags: ,

Leave a Reply

Comments for this post will be closed on 15 October 2008.