とらりもんHOME  Index  Search  Changes  Login

Python画像処理入門

  1. 画像を表現するクラスをロード

import PIL from PIL import Image import numpy as np import sys, os file = '2017_0617_1505_14976795.JPG'

img = Image.open(file, 'r')

r, g, b = img.split()

rgb_img = Image.merge("RGB", (r,b,g)) rgb_img.save(file[:-4] + "chRGB_rbg.JPG", 'JPEG')

rgb_img = Image.merge("RGB", (g,r,b)) rgb_img.save(file[:-4] + "chRGB_grb.JPG", 'JPEG')

rgb_img = Image.merge("RGB", (g,b,r)) rgb_img.save(file[:-4] + "chRGB_gbr.JPG", 'JPEG')

rgb_img = Image.merge("RGB", (b,r,g)) rgb_img.save(file[:-4] + "chRGB_brg.JPG", 'JPEG')

rgb_img = Image.merge("RGB", (b,g,r)) rgb_img.save(file[:-4] + "chRGB_bgr.JPG", 'JPEG')

  1. 画像を表現するクラスをロード

import PIL from PIL import Image import numpy as np import sys, os file = '2017_0617_1505_14976795.JPG'

img = Image.open(file, 'r') gray_img = img.convert('L') gray_img.save(file[:-4] + "gray.JPG", 'JPEG')

  1. ---↓ for changin whole dir↓ ---
  2. files = os.listdir('pic_all_resize')
  3. os.chdir("./pic_all_resize")
  4. for fil in files:
  5. img = Image.open(fil, 'r')
  6. gray_img = img.convert('L')
  7. gray_img.save(fil[:-4] + "resize_gray.jpg", 'JPEG')

from PIL import Image import sys, os

hoge = Image.open("2017_0617_1505_14976795.JPG").resize((50,50)) hoge.save("resize.jpg")

  1. resize と 解像度を落とすのは違う。
  2. -----↓ 以下はresizeです↓ -----
  3. file = '2017_0617_1505_14976795.JPG'
  4. img = Image.open(file, 'r')
  5. width, height = img.size
  6. width_2 = width/10
  7. height_2 = height/10
  8. img.thumbnail((width_2, height_2))
  9. img.save(file[:-4] + "_LowRes.JPG", 'JPEG')

from PIL import Image import sys, os

hoge = Image.open("2017_0617_1505_14976795.JPG").resize((50,50)) hoge.save("resize.jpg")

  1. resize と 解像度を落とすのは違う。
  2. -----↓ 以下はresizeです↓ -----
  3. file = '2017_0617_1505_14976795.JPG'
  4. img = Image.open(file, 'r')
  5. width, height = img.size
  6. width_2 = width/10
  7. height_2 = height/10
  8. img.thumbnail((width_2, height_2))
  9. img.save(file[:-4] + "_LowRes.JPG", 'JPEG')
  10. coding: utf-8
  11. This is used here in ./pic_all
  12. ------------------------------------------

from PIL import Image

file = '2017_0617_1505_14976795.JPG' img = Image.open(file, 'r') size = min(img.size) width, height = img.size top = 0 bottom = size left = (width - size) / 2 right = left + size box = (left, top, right, bottom) img = img.crop(box) img.thumbnail((100, 100)) img.save(file[:-4] + "_resize.JPG", 'JPEG')

  1. img = Image.open('files[len(files)]', 'r')
  2. import sys, os
  3. sys.path.append(os.pardir)
  4. files = os.listdir('pic_all')
  5. os.chdir("./pic_all")
  6. for fil in files:
  7. img = Image.open(fil, 'r')
  8. resize_img = img.resize((28, 28))#tupleにより指定
  9. resize_img.save(fil[:-4] + "resize.jpg", 'JPEG')#リサイズ後の画像を保存
  10. img = Image.open('files[len(files)]', 'r') #読み込み
  11. coding: utf-8
  12. This is used here in ./pic_all
  13. ------------------------------------------

from PIL import Image

file = '2017_0617_1505_14976795.JPG' img = Image.open(file, 'r') resize_img = img.resize((100, 100)) resize_img.save(file[:-4] + "_resize.JPG", 'JPEG')

  1. img = Image.open('files[len(files)]', 'r')
  2. import sys, os
  3. sys.path.append(os.pardir)
  4. files = os.listdir('pic_all')
  5. os.chdir("./pic_all")
  6. for fil in files:
  7. img = Image.open(fil, 'r')
  8. resize_img = img.resize((28, 28))#tupleにより指定
  9. resize_img.save(fil[:-4] + "resize.jpg", 'JPEG')#リサイズ後の画像を保存
  10. img = Image.open('files[len(files)]', 'r') #読み込み

import numpy as np import PIL from PIL import Image from os import listdir import sys, os

def rm_noise(x, sizex, sizey):

   for s in range(sizey):
       for t in range(sizex):
           #if x[s, t] == 0:
           if imgc[s, t, 0] == 0 and imgc[s, t, 1] == 0 and imgc[s, t, 2] == 0:
               x[s, t, 0]=255
               x[s, t, 1]=255
               x[s, t, 2]=255
   return(x) 

file = '2017_0617_1505_14976795.JPG'

imga = Image.open(file, 'r') for i in range(30, 360, 30):

   imgb = imga.rotate(i)
   sizex = imgb.size[0]
   sizey = imgb.size[1]
   imgc = np.asarray(imgb)
   imgc.flags.writeable = True
   imgd = rm_noise(imgc, sizex ,sizey)
   imge = Image.fromarray(np.uint8(imgd))
   imge.save(file[:-4] + "_rotate" + str(i) + ".JPG", "JPEG")
Last modified:2017/08/22 15:38:43
Keyword(s):
References:[とらりもんHOME]