見出し画像

画像処理('24)#5 画像の変換

 画像の返還は、実施すると画像が動いたのが良く分かり楽しいですね。
時間があればカラーでも試したいですね。
ちょっと遅いかなと思いましたが、インタプリンタですから仕方ないですかね。パソコンも古いし(-_-;)


ベース
明るさの返還
グレースケールへの返還
トーンカーブによる画像の変換
幾何学変換
幾何学変換theta

#プログラム 5-1 明るさの変化
from PIL import Image
import numpy as np

i1=Image.open("IMG_2348.png")
h=i1.height
w=i1.width
image1=np.array(i1)
image2=np.zeros((h,w,3),dtype="uint8")

for y in range(h):
for x in range(w):
for c in range(3):
temp=(int)(image1[y][x][c])+64
if temp<0:
temp=0
if temp>255:
temp=255
image2[y][x][c]=temp

i2=Image.fromarray(image2)
i2.save("IMG_2348_2.png")

#プログラム 5-2 グレースケール画像への変換
from PIL import Image
import numpy as np

i1=Image.open("IMG_2348.png")
h=i1.height
w=i1.width
image1=np.array(i1)
image2=np.zeros((h,w),dtype="uint8")

for y in range(h):
for x in range(w):
temp=0.299image1[y][x][0]
temp+=0.587image1[y][x][1]
temp+=0.114*image1[y][x][2]
image2[y][x]=(int)(temp)

i2=Image.fromarray(image2)
i2.save("IMG_2348_3.png")

#プログラム 5-3 トーンカーブによる画像の返還
from PIL import Image
import numpy as np

i1=Image.open("IMG_2348.png").convert("L")
h=i1.height
w=i1.width
image1=np.array(i1)
image2=np.zeros((h,w),dtype="uint8")

tonecurve=np.zeros(256, dtype="uint8")
for x in range(256):
tonecurve[x]=x
for y in range(h):
for x in range(w):
image2[y][x]=tonecurve[image1[y][x]]

i2=Image.fromarray(image2)
i2.save("IMG_2348_4.png")

#プログラム 5-4 幾何学変換
from PIL import Image
import numpy as np
import math

i1=Image.open("IMG_2348.png").convert("L")
h=i1.height
w=i1.width
image1=np.array(i1)
image2=np.zeros((h,w),dtype="uint8")

a=400
b=200
for v in range(h):
for u in range(w):
x=u-a
y=v-b
if x>=0 and x<w and y>=0 and y<h:
image2[v][u]=image1[y][x]

i2=Image.fromarray(image2)
i2.save("IMG_2348_5.png")

この記事が気に入ったらサポートをしてみませんか?