Python scipy.misc 模块,fromimage() 实例源码
我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用scipy.misc.fromimage()。
def original_color_transform(content, generated, mask=None, hist_match=0, mode='ycbcr'):
generated = fromimage(toimage(generated, mode='RGB'), mode=mode) # Convert to ycbcr color space
if mask is None:
if hist_match == 1:
for channel in range(3):
generated[:, :, channel] = match_histograms(generated[:, channel], content[:, channel])
else:
generated[:, 1:] = content[:, 1:]
else:
width, height, channels = generated.shape
for i in range(width):
for j in range(height):
if mask[i, j] == 1:
if hist_match == 1:
for channel in range(3):
generated[i, j, channel] = match_histograms(generated[i, content[i, channel])
else:
generated[i, 1:] = content[i, 1:]
generated = fromimage(toimage(generated, mode=mode), mode='RGB') # Convert to RGB color space
return generated
# util function to load masks
def original_color_transform(content, mask=None):
generated = fromimage(toimage(generated, mode='ycbcr') # Convert to ycbcr color space
if mask is None:
generated[:, 1:] # Generated CbCr = Content CbCr
else:
width, j] == 1:
generated[i, mode='ycbcr'), mode='RGB') # Convert to RGB color space
return generated
def original_color_transform(content, mode='RGB') # Convert to RGB color space
return generated
def original_color_transform(content, mode='RGB') # Convert to RGB color space
return generated
def original_color_transform(content, mode='RGB') # Convert to RGB color space
return generated
def find_dominant_colors(image):
"""Cluster the colors of the image in CLUSTER_NUMBER of clusters. Returns
an array of dominant colors reverse sorted by cluster size.
"""
array = img_as_float(fromimage(image))
# Reshape from MxNx4 to Mx4 array
array = array.reshape(scipy.product(array.shape[:2]), array.shape[2])
# Remove transparent pixels if any (channel 4 is alpha)
if array.shape[-1] > 3:
array = array[array[:, 3] == 1]
# Finding centroids (centroids are colors)
centroids, _ = kmeans(array, CLUSTER_NUMBER)
# Allocate pixel to a centroid cluster
observations, _ = vq(array, centroids)
# Calculate the number of pixels in a cluster
histogram, _ = scipy.histogram(observations, len(centroids))
# Sort centroids by number of pixels in their cluster
sorted_centroids = sorted(zip(centroids, histogram),
key=lambda x: x[1],
reverse=True)
sorted_colors = tuple((couple[0] for couple in sorted_centroids))
return sorted_colors
def original_color_transform(content, mode='RGB') # Convert to RGB color space
return generated
def draw_label(label, img, n_class, label_titles, bg_label=0):
"""Convert label to rgb with label titles.
@param label_title: label title for each labels.
@type label_title: dict
"""
from PIL import Image
from scipy.misc import fromimage
from skimage.color import label2rgb
from skimage.transform import resize
colors = labelcolormap(n_class)
label_viz = label2rgb(label, colors=colors[1:], bg_label=bg_label)
# label 0 color: (0,0) -> (0,255)
label_viz[label == 0] = 0
# plot label titles on image using matplotlib
plt.subplots_adjust(left=0, right=1, top=1, bottom=0,
wspace=0, hspace=0)
plt.margins(0, 0)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.axis('off')
# plot image
plt.imshow(label_viz)
# plot legend
plt_handlers = []
plt_titles = []
for label_value in np.unique(label):
if label_value not in label_titles:
continue
fc = colors[label_value]
p = plt.Rectangle((0, 0), 1, fc=fc)
plt_handlers.append(p)
plt_titles.append(label_titles[label_value])
plt.legend(plt_handlers, plt_titles, loc='lower right', framealpha=0.5)
# convert plotted figure to np.ndarray
f = StringIO.StringIO()
plt.savefig(f, bBox_inches='tight', pad_inches=0)
result_img_pil = Image.open(f)
result_img = fromimage(result_img_pil, mode='RGB')
result_img = resize(result_img, img.shape, preserve_range=True)
result_img = result_img.astype(img.dtype)
return result_img
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。