【发布时间】:2018-10-23 14:26:12
【问题描述】:
我作为 Python 初学者正在学习图像处理。我的目标是将我的图像分割成一个 nxn 网格,其中每个正方形分别是原始图像的平均颜色(灰度图像)。我成功地分割了图像,改变了它的像素数据并保存了新的图像。我现在的问题是将图像重新拼接在一起。我知道连接功能指向原始图像,我曾希望通过保存瓷砖我可以解决这个问题。
这是我第一次在 stackoverflow 上发帖(而且我对 python 非常陌生),如果我不清楚或格式错误,请道歉。
# Import packages
import numpy as np
from numpy import matlib
import PIL
import image_slicer
import math
import glob
from image_slicer import join
from PIL import Image
### Use PIL to import image
##img = Image.open("einstein.jpg")
# Display original image
# img.show()
##new_img = img.resize((256,256))
##new_img.save('einstein-256x256','png')
### new_img.show()
#Slice image into four pieces
tiles = image_slicer.slice("einstein.jpg", 16)
# Use glob to open every .png file with for loop
for filename in glob.glob("*.png"):
img=Image.open(filename)
pixels = img.load() # create the pixel map
pixelMap = img.load() #create the pixel map
#convert to array
arr = np.asarray(img)
#find mean
pixelMean = arr.mean(0).mean(0)[0]
# Convert mean to integer
IntMean = math.floor(pixelMean)
print(IntMean)
##pixel = pixelMap[0,0] #get the first pixel's value
##print(pixel)
# Loop for going through every pixel in image and converting it
for i in range(img.size[0]): # for every col:
for j in range(img.size[1]): # For every row
pixels[i,j] = (IntMean,IntMean,IntMean) # set the colour accordingly
# Save new monotone images
img.save(filename)
# Join new images into one
image = join(tiles)
# Save new image
image.save("einsteinJoined.jpg")
image.show()
【问题讨论】:
标签: image numpy python-3.6