查看
image_slicer代码后,我可以看到混乱。主要问题是每个
Tile对象都包含图像数据和元数据,例如文件名和最终图像中的位置。但是,当指向的文件被更新时,图像数据不会被更新。
因此,当更新由元数据指向的文件时,也需要更新图块的图像对象。我想最简单的方法是在磁盘上的文件更改时重新打开图块中的图像。这很可能会达到目的:
import cv2import numpy as npfrom matplotlib import pyplot as pltfrom scipy.misc import imsavefrom scipy import ndimagefrom scipy import miscimport scipy.miscimport scipyimport image_slicerfrom image_slicer import joinfrom PIL import Imageimg = 'watch.png'num_tiles = 64tiles = image_slicer.slice(img, num_tiles)for tile in tiles: img = scipy.misc.imread(tile.filename) hist,bins = np.histogram(img.flatten(),256,[0,256]) cdf = hist.cumsum() cdf_normalized = cdf *hist.max()/ cdf.max() plt.plot(cdf_normalized, color = 'g') plt.hist(img.flatten(),256,[0,256], color = 'g') plt.xlim([0,256]) plt.legend(('cdf','histogram'), loc = 'upper left') cdf_m = np.ma.masked_equal(cdf,0) cdf_o = (cdf_m - cdf_m.min())*255/(cdf_m.max()-cdf_m.min()) cdf = np.ma.filled(cdf_o,0).astype('uint8') img3 = cdf[img] cv2.imwrite(tile.filename,img3) tile.image = Image.open(tile.filename)image = join(tiles)image.save('watch-join.png')因此,主要的更改是
tile.image =Image.open(tile.filename)在循环末尾添加。还要注意,我已经通过删除生成文件名的第一个循环来稍微更新了您的代码,相反,第二个循环直接位于磁贴上,因为它们包含了所有所需的信息。



