相同数据保存生成不同图像 – Python
•浏览 1
Same data saved generate different images - Python
我的代码中有两种保存图像数据的方法,一种只是将其值保存为灰度值,另一种用于生成热图图像:
def save_image(self, name):
"""
Save an image data in PNG format
:param name: the name of the file
"""
graphic = Image.new("RGB", (self.width, self.height))
putpixel = graphic.putpixel
for x in range(self.width):
for y in range(self.height):
color = self.data[x][y]
color = int(Utils.translate_range(color, self.range_min, self.range_max, 0, 255))
putpixel((x, y), (color, color, color))
graphic.save(name +".png","PNG")
def generate_heat_map_image(self, name):
"""
Generate a heat map of the image
:param name: the name of the file
"""
#self.normalize_image_data()
plt.figure()
fig = plt.imshow(self.data, extent=[-1, 1, -1, 1])
plt.colorbar(fig)
plt.savefig(name+".png")
plt.close()
class ImageData:
def __init__(self, width, height):
self.width = width
self.height = height
self.data = []
for i in range(width):
self.data.append([0] * height)
color = self.data[x][y]
color = self.data[y][x]
for row in range(self.height):
for col in range(self.width):
color = self.data[row][col]
color = int(Utils.translate_range(color, self.range_min, self.range_max, 0, 255))
putpixel((col, row), (color, color, color))
代表我的数据的类是这样的:
def save_image(self, name):
"""
Save an image data in PNG format
:param name: the name of the file
"""
graphic = Image.new("RGB", (self.width, self.height))
putpixel = graphic.putpixel
for x in range(self.width):
for y in range(self.height):
color = self.data[x][y]
color = int(Utils.translate_range(color, self.range_min, self.range_max, 0, 255))
putpixel((x, y), (color, color, color))
graphic.save(name +".png","PNG")
def generate_heat_map_image(self, name):
"""
Generate a heat map of the image
:param name: the name of the file
"""
#self.normalize_image_data()
plt.figure()
fig = plt.imshow(self.data, extent=[-1, 1, -1, 1])
plt.colorbar(fig)
plt.savefig(name+".png")
plt.close()
class ImageData:
def __init__(self, width, height):
self.width = width
self.height = height
self.data = []
for i in range(width):
self.data.append([0] * height)
color = self.data[x][y]
color = self.data[y][x]
for row in range(self.height):
for col in range(self.width):
color = self.data[row][col]
color = int(Utils.translate_range(color, self.range_min, self.range_max, 0, 255))
putpixel((col, row), (color, color, color))
为两种方法传递相同的数据
ContourMap.save_image("ImagesOutput/VariabilityOfGradients/ContourMap")
ContourMap.generate_heat_map_image("ImagesOutput/VariabilityOfGradients/ContourMapHeatMap")
我得到一张相对于另一张旋转的图像。
方法一:
方法二:
我不明白为什么,但我必须解决这个问题。
任何帮助将不胜感激。
提前致谢。
显然数据是行优先格式,但您正在像列优先格式一样进行迭代,这会将整个数据旋转 -90 度。
快速解决方法是替换这一行:
def save_image(self, name):
"""
Save an image data in PNG format
:param name: the name of the file
"""
graphic = Image.new("RGB", (self.width, self.height))
putpixel = graphic.putpixel
for x in range(self.width):
for y in range(self.height):
color = self.data[x][y]
color = int(Utils.translate_range(color, self.range_min, self.range_max, 0, 255))
putpixel((x, y), (color, color, color))
graphic.save(name +".png","PNG")
def generate_heat_map_image(self, name):
"""
Generate a heat map of the image
:param name: the name of the file
"""
#self.normalize_image_data()
plt.figure()
fig = plt.imshow(self.data, extent=[-1, 1, -1, 1])
plt.colorbar(fig)
plt.savefig(name+".png")
plt.close()
class ImageData:
def __init__(self, width, height):
self.width = width
self.height = height
self.data = []
for i in range(width):
self.data.append([0] * height)
color = self.data[x][y]
color = self.data[y][x]
for row in range(self.height):
for col in range(self.width):
color = self.data[row][col]
color = int(Utils.translate_range(color, self.range_min, self.range_max, 0, 255))
putpixel((col, row), (color, color, color))
一个€|用这个:
def save_image(self, name):
"""
Save an image data in PNG format
:param name: the name of the file
"""
graphic = Image.new("RGB", (self.width, self.height))
putpixel = graphic.putpixel
for x in range(self.width):
for y in range(self.height):
color = self.data[x][y]
color = int(Utils.translate_range(color, self.range_min, self.range_max, 0, 255))
putpixel((x, y), (color, color, color))
graphic.save(name +".png","PNG")
def generate_heat_map_image(self, name):
"""
Generate a heat map of the image
:param name: the name of the file
"""
#self.normalize_image_data()
plt.figure()
fig = plt.imshow(self.data, extent=[-1, 1, -1, 1])
plt.colorbar(fig)
plt.savefig(name+".png")
plt.close()
class ImageData:
def __init__(self, width, height):
self.width = width
self.height = height
self.data = []
for i in range(width):
self.data.append([0] * height)
color = self.data[x][y]
color = self.data[y][x]
for row in range(self.height):
for col in range(self.width):
color = self.data[row][col]
color = int(Utils.translate_range(color, self.range_min, self.range_max, 0, 255))
putpixel((col, row), (color, color, color))
(虽然可能 data 是一个数组,所以你真的应该使用 self.data[y, x] 代替。)
更清晰的解决方法是:
def save_image(self, name):
"""
Save an image data in PNG format
:param name: the name of the file
"""
graphic = Image.new("RGB", (self.width, self.height))
putpixel = graphic.putpixel
for x in range(self.width):
for y in range(self.height):
color = self.data[x][y]
color = int(Utils.translate_range(color, self.range_min, self.range_max, 0, 255))
putpixel((x, y), (color, color, color))
graphic.save(name +".png","PNG")
def generate_heat_map_image(self, name):
"""
Generate a heat map of the image
:param name: the name of the file
"""
#self.normalize_image_data()
plt.figure()
fig = plt.imshow(self.data, extent=[-1, 1, -1, 1])
plt.colorbar(fig)
plt.savefig(name+".png")
plt.close()
class ImageData:
def __init__(self, width, height):
self.width = width
self.height = height
self.data = []
for i in range(width):
self.data.append([0] * height)
color = self.data[x][y]
color = self.data[y][x]
for row in range(self.height):
for col in range(self.width):
color = self.data[row][col]
color = int(Utils.translate_range(color, self.range_min, self.range_max, 0, 255))
putpixel((col, row), (color, color, color))
这可能从 pyplot 文档中并不完全清楚,但是如果您查看 imshow,它会解释它采用形状为 (n, m) 的类似数组的对象并将其显示为 MxN 图像。