【发布时间】:2021-05-06 18:36:07
【问题描述】:
以下列表是图像文件的行,其中前 3 个数字包含 RGB 值,后 2 个数字是像素的 x 和 y 坐标。这背后的整个想法是减少我需要遍历文件的项目数量,通过将相同的连续像素转换为一个范围,它会大大减小大小(最多 50%),特别是如果图像有一个实体彩色边框。
我想创建一个执行以下操作的算法:
#converts these rows:
#[(63, 72, 204, 1, 3), (63, 72, 204, 2, 3), (63, 72, 204, 3, 3), (234, 57, 223, 4, 3)]
#[(255, 242, 0, 1, 2), (255, 242, 44, 2, 2), (255, 242, 44, 3, 2), (255, 242, 44, 4, 2)]
#[(255, 174, 200, 1, 1), (136, 0, 27, 2, 1), (136, 0, 27, 3, 1), (111, 125, 33, 4, 1)]
#into something like this:
#[(63, 72, 204, 1,3, 3,3), (234, 57, 223, 4, 3)]
#[(255, 242, 0, 1,2, 3,2), (255, 242, 44, 4, 2)]
#[(255, 174, 200, 1, 1), (136, 0, 27, 2,1, 3,1), (111, 125, 33, 4, 1)]
#This is what I have so far:
from PIL import Image
import numpy as np
def pic(name=str):
with Image.open('file_name.png') as png: #opens the image file
width, height = png.size #gets the dimensions
for y in range(height): #iterates through each pixel grabbing RGB and xy position
row = []
for x in range(width):
r,g,b = png.getpixel((x, y))
to_append = (r,g,b,x+1,abs(y-height)) #to flip the y values (unrelated reason)
row.append(tuple((to_append)))
print(row)
【问题讨论】:
标签: python algorithm image numpy