ã¯ããã«
ä»åã¯pythonã«ããç»åå¦çã®ã話ã§ãã
æ®æ®µä»äºã§ã¯Rubyã¡ã¤ã³ãªã®ã§ãããæè¿è¶£å³ã§pythonãåå¼·ãã¯ãããç»åãå å·¥ããã®ãæå¤ã¨ç°¡åã ã¨åãã£ãã®ã§ãç°¡åãªç»åå¦çã«ã¤ãã¦æ¸ãã¦ã¿ã¾ããã pythonã«ã¤ãã¦ã¯ã¾ã ã¾ã åå¼·ä¸ãªã®ã§ãpythonçã«ã¯ããã¯æ¸ããªããããã ã¨å¦çãé ããã³ã¼ããæ±ããªã©ãªã©ããã¾ããããã³ã¡ã³ãã§æãã¦ããã ããã¨å¹¸ãã§ãã
ã¾ãã以ä¸ã®ã³ã¼ãã§ã¯pythonã®ã©ã¤ãã©ãªãnumpy, pillowã使ç¨ãã¦ãã¾ãã èè ã®ç°å¢ã§ã¯windows10ä¸ã§Anaconda3ã使ç¨ãã¦ããããå¥éã¤ã³ã¹ãã¼ã«ã¯ä¸è¦ã§ããããå®è¡ããéã¯å¿ è¦ã«å¿ãã¦ã¤ã³ã¹ãã¼ã«ããé¡ããã¾ãã
ç»åå¦çã®æ¦è¦
ç»åã®å å·¥ã¯åç´ã«åãã¯ã»ã«ã®è²ãç»åã®ç«¯ãã端ã¾ã§é çªã«å¤æ´ãããã¨ã§å®ç¾ãã¦ãã¾ãã
ããã§ãã¾ãã¯ç»åãã¡ã¤ã«ãèªã¿è¾¼ã¿ãæä½ããããããã«å¤§ããã ç»åã®é«ã à ç»åã®å¹ à 3 ã®ï¼æ¬¡å é åã«å¤æãã¾ãã
ãã®ããã«ãããã¨ã§ãä¾ãã°ç»åã®å·¦ãã100çªç®ãä¸ãã200çªç®ã®ãã¯ã»ã«ã®è²ã¯ã
img_pixels[100][200]ã¨ãã¦åå¾ã§ããããã«ãªãã¾ããåå¾ããå¤ã¯è¦ç´ æ°ãï¼ã®é
å[r,g,b]
ã«ãªãã¾ãã
ã¾ãæåã«æä½éã®pillow(PIL)ã®ä½¿ãæ¹ã¨ç»åå å·¥ã§ç¨ããå ±éã®å¦çã説æãã¾ãã
ç»åã®èªã¿è¾¼ã¿ãæ°è¦ä½æ
è¡é ã§å¿ è¦ãªã©ã¤ãã©ãªãã¤ã³ãã¼ããã¾ãã
ããããããããã«ä½¿ç¨ããç»åã¯pyãã¡ã¤ã«ã¨åããã©ã«ãã«ç½®ãã¦ããã¾ãã
from PIL import Image import numpy as np # å ã¨ãªãç»åã®èªã¿è¾¼ã¿ img = Image.open('original.jpg') #ãªãªã¸ãã«ç»åã®å¹ ã¨é«ããåå¾ width, height = img.size # ãªãªã¸ãã«ç»åã¨åããµã¤ãºã®Imageãªãã¸ã§ã¯ããä½æãã img2 = Image.new('RGB', (width, height))
ç»åãã¡ã¤ã«ã®é åå
èªã¿è¾¼ãã ãªãªã¸ãã«ç»åãé åã«å¤æãã¾ãã
img_pixels = [] for y in range(height): for x in range(width): # getpixel((x,y))ã§å·¦ããxçªç®,ä¸ããyçªç®ã®ãã¯ã»ã«ã®è²ãåå¾ããimg_pixelsã«è¿½å ãã img_pixels.append(img.getpixel((x,y))) # ãã¨ã§è¨ç®ããããããã«numpyã®arrayã«å¤æãã¦ãã img_pixels = np.array(img_pixels)
ã¡ãªã¿ã«ã以ä¸ã®ããã«ä¸è¡ã§æ¸ããã¨ãã§ãã¾ãã
img_pixels = np.array([[img.getpixel((i,j)) for j in range(width)] for i in range(height)])
åãã¯ã»ã«ã®å¤ã®åå¾
ä¸è¨ã§ãæ¸ãã¾ããããåãã¯ã»ã«ã®è²ã®åå¾ã¯ä»¥ä¸ã®ããã«ãã¾ã
img_pixels[100][200] # => array([255,255,255])
ãã¯ã»ã«ã¸ã®å¤ã®ã»ãã
å å·¥å¾ã®ç»åãªãã¸ã§ã¯ãimg2ã®å·¦ãã100çªç®ãä¸ãã200çªç®ã®ãã¯ã»ã«ã¸è²ãã»ããããã«ã¯ä»¥ä¸ã®ããã« putpixelã¡ã½ããã使ãã¾ãã以ä¸ã®ä¾ã§ã¯éè²ã«ã»ãããã¦ãã¾ãã
赤è²ã«ã»ããããã«ã¯å¾ã3ã¤ã®å¼æ°ã255,0,0ã¨ããã°ããã§ãã
img2.putpixel((100, 200), (0, 0, 255))
表示ã¨ä¿å
ç·¨éããç»åã¤ã³ã¹ã¿ã³ã¹ã表示ããã«ã¯showã¡ã½ããã使ãã¾ãã
img2.show()
ä¿åããå ´åã¯saveã¡ã½ããã使ç¨ãã¾ãã
img2.save('edited_img.jpg')
以ä¸ã®ã¡ã½ããã使ç¨ãã¦å®éã«ç»åãå å·¥ãã¦ã¿ã¾ãã
ç»åå å·¥ã«ä½¿ç¨ãããªãªã¸ãã«ç»åã¯ä»¥ä¸ã®ãã®ã使ãã¾ãã
ããªã¼ç»åãµã¤ãã®ã±ããããã( https://www.pakutaso.com/ )ã®ç»åã使ããã¦ããã ãã¾ããã
ã¼ããå å·¥
ç»åå ¨ä½ãå°ããããã ãããªãã¼ããå å·¥ãå®è£ ãã¾ãã
ãã£ã«ã¿ã¼ãµã¤ãºåç»åãå°ãããªã£ã¦ãã¾ãã®ãé£ç¹ã§ãã
å¨ãã®è²ã¨ã®å¹³åã®è²ãå å·¥å¾ç»åã«æ¸ãè¾¼ãã§ãããã¨ã§ã«ãã¾ãããããªç»åãä½æãã¾ãã
æ¬å½ã¯èµ·ç¹ããã¿ã¦ä¸ä¸å·¦å³ã«ããè²ã¨ã®å¹³åãåãããã£ãã®ã§ãããç°¡åã®ããå³ä¸æ¹åã«ããè²ã¨ã®å¹³åãã¨ã£ã¦ãã¾ãã
from PIL import Image import numpy as np img = Image.open('original.jpg') width, height = img.size filter_size = 20 img2 = Image.new('RGB', (width - filter_size, height - filter_size)) img_pixels = np.array([[img.getpixel((x,y)) for x in range(width)] for y in range(height)]) for y in range(height - filter_size): for x in range(width - filter_size): # ä½ç½®(x,y)ãèµ·ç¹ã«ç¸¦æ¨ªãã£ã«ã¿ã¼ãµã¤ãºã®å°ããç»åããªãªã¸ãã«ç»åããåãåã partial_img = img_pixels[y:y + filter_size, x:x + filter_size] # å°ããç»åã®åãã¯ã»ã«ã®å¤ãä¸åã«ä¸¦ã¹ã color_array = partial_img.reshape(filter_size ** 2, 3) # åR,G,Bããããã®å¹³åãæ±ãã¦å å·¥å¾ç»åã®ä½ç½®(x,y)ã®ãã¯ã»ã«ã®å¤ã«ã»ãããã mean_r, mean_g, mean_b = color_array.mean(axis = 0) img2.putpixel((x,y), (int(mean_r), int(mean_g), int(mean_b))) img2.show() img2.save('bokashi.jpg')
以ä¸ãåºåãããç»åã§ãã
ã¢ã¶ã¤ã¯
ç»åå ¨ä½ã«ã¢ã¶ã¤ã¯ãããã£ããããªç»åãçæãã¾ãã
縦横ãã£ã«ã¿ã¼ãµã¤ãºåã®é¨åç»åã®ä¸ã§ãä¸çªæ¿ãè²ã§é¨åç»åã¨åã大ããã®(åè²ã®)ç»åãä½æãã¾ãã
ä½æããåè²ã®ç»åãå å·¥å¾ç»åã«æ¬¡ã ã¨ã¯ãè¾¼ãã§ããã¢ã¶ã¤ã¯ç»åãä½æãã¾ãã
from PIL import Image import numpy as np img = Image.open('original.jpg') width, height = img.size filter_size = 10 img2 = Image.new('RGB', (width, height)) img_pixels = np.array([[img.getpixel((x,y)) for x in range(width)] for y in range(height)]) # def draw_partial_img(img2, start_x, start_y, partial_size_x, partial_size_y, pixel_color): for y in range(start_y, start_y + partial_size_y): for x in range(start_x, start_x + partial_size_x): img2.putpixel((x, y), pixel_color) for y in range(0, height, filter_size): for x in range(0, width, filter_size): # ã¼ããå å·¥åæ§ã«ç»åã®ä¸é¨åãåãåºã partial_img = img_pixels[y:y + filter_size, x:x + filter_size] # è²ã®é åã«ãªãããã«å¤æãã color_array = partial_img.reshape(partial_img.shape[0] * partial_img.shape[1], 3) # åãã¯ã»ã«ãã¨ã®r + g + bãæ大å¤ãåãç©ã®çªå·ãåå¾ãã # ããããã«åãåºããç»åã®ä¸ã§ä¸çªæ¿ãè²ã®çªå· max_index = np.argmax(color_array.sum(axis=1)) max_r, max_g, max_b = color_array[max_index] # (x,y)ãèµ·ç¹ã«ç¸¦æ¨ªãã£ã«ã¿ã¼ãµã¤ãºã§åè²(ä¸è¨ã®è²)ã®ç»åãimg2ã¸ã»ãããã draw_partial_img(img2, x, y, partial_img.shape[1], partial_img.shape[0], (max_r, max_g, max_b)) img2.show() img2.save('mozaiku.jpg')
以ä¸ãåºåçµæã«ãªãã¾ãã
ï¼éã«ã¼ããããã®ã§æ°æã¡æªãã§ããããããããã®å¤§ããã®ç»åãªãæ°ç§ã§è¡¨ç¤ºããã¾ããã
è²å転
ãã¬ã®ãããªè²ãå転ããç»åãçæãã¾ãã
from PIL import Image import numpy as np img = Image.open('original.jpg') width, height = img.size img2 = Image.new('RGB', (width, height)) img_pixels = np.array([[img.getpixel((x,y)) for x in range(width)] for y in range(height)]) # è²ãå転ãã reverse_color_pixels = 255 - img_pixels for y in range(height): for x in range(width): # å転ããè²ã®ç»åãä½æãã r,g,b = reverse_color_pixels[y][x] img2.putpixel((x,y), (r,g,b)) img2.show() img2.save('hanten.jpg')
以ä¸ãçæãããç»åã§ãã
å ã®ç»åã綺éºã ããããæãããã§ã¯ããã¾ãããå転ãã¦ã綺éºã§ããã
ã¾ã¨ã
å®éã¯pillowã«è²å転ãããã¡ã½ãããæºåããã¦ãããããã®ã§ãããä»åã¯pythonç·´ç¿ã®ããnumpyã®æãåããªããç»åå å·¥ãå®è£ ãã¦ã¿ã¾ããã
è³ãã¬æç« ã§ã¯ããã¾ããããæå¾ã¾ã§ãèªã¿ããã ããããã¨ããããã¾ããã
ããããããããããã¨ãããã®ã§ã¾ã追è¨ããããããã¾ããã
追è¨
ãã®è¨äºã®ç¶ç·¨ãæ¸ãã¦ãã¾ããç»åå¦çã«èå³ãæã£ã¦ããã ããæ¹ã¯ããé²ã¿ãã ããã
- pythonによる画像処理入門part2 k平均法による減色処理 - ブンバボーンな毎日
- pythonによる画像処理入門part3 塗り絵を生成してみる - ブンバボーンな毎日
- python画像処理入門4 ミニチュア風画像をつくる - ブンバボーンな毎日(ãããã)
- python画像処理入門5 アンシャープマスキング - ブンバボーンな毎日
èªè ç»é²ãã¦ããã ããã¨ã¯ãã¿ã«ãªãã¾ãããããããé¡ããã¾ã