使用matplotlib和/或numpy缩小图像调色板的尺寸以创建块状图像 [英] image palette's dimension reduction using matplotlib and/or numpy to create blocky image

查看:104
本文介绍了使用matplotlib和/或numpy缩小图像调色板的尺寸以创建块状图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iPython笔记本,我有一个512x512图像,我想使用块中颜色的最近"插值将其转换"为块状"图像,类似于Photoshop中的马赛克"滤镜,进一步处理一批图片.

I am using iPython notebook, I have a 512x512 image that I want to 'transform' into a 'blocky' image using 'nearest' interpolation of colors in the blocks, similar to the 'mosaic' filter in Photoshop, for some further manipulation of a batch of pictures.

在Photoshop中,加载图像,然后可以将滤镜设置为马赛克",并以像素为单位设置块大小".

In Photoshop, you load the image and you can set the filter to 'Mosaic', and set the 'block size' in pixels.

此处有关于如何使用numpy可以做到这一点,但是由于我不是numpy专家,所以我发现了一种快捷方式",可以使用PIL对我有用.我不知道如何适当地调整它,以避免一些不必要的步骤.

Here there is a partial explanation about how to do it using numpy, but since I am not a numpy expert, I found a 'shortcut' that is sort of working for me using PIL. I don't know how to properly adapt it to avoid some unnecessary steps.

img = Image.open(filename) 
h = img.size[0]
v = img.size[1]

blocksize = 100

rsize = img.resize((h/blocksize,v/blocksize))  # resize the image
rsizeArr = np.asarray(rsize)
lum_img = rsizeArr[:,:,0]

#plt.axes.get_xaxis().set_visible(False)
#plt.axes.get_yaxis().set_visible(False)

imgplot = plt.imshow(rsizeArr)
#imgplot = plt.imsave('test.jpg', rsizeArr)
imgplot.set_interpolation('nearest')

imgplot.axes.get_xaxis().set_visible(False)
imgplot.axes.get_yaxis().set_visible(False)

plt.savefig('testplot1.png', bbox_inches = 'tight')

由于我使用的是iPython笔记本,因此在执行该行后会立即在笔记本中生成内联输出. (我不想显示此特定步骤的内联输出,因为我将在笔记本中处理几张图片.)

Since I'm using iPython notebook, this produces an inline output in the notebook as soon as the line is executed. (I don't want to display the inline output for this particular step, since I'll be processing several pictures in the notebook).

"tesplot1.png"文件的大小为1KB,但是由于某种原因,其尺寸已减小为264x264像素.由于我希望图像的原始尺寸为512x512,因此请执行以下操作:

The 'tesplot1.png' file is 1KB in size, however, for some reason, its dimension get's reduced to 264x264 pixels. Since I want the image in its orginal size of 512x512, I do the following:

img2 = Image.open('testplot1.png')
rsize = img2.resize((512,512))  # resize the image
rsize.save('testplot2.jpg') 

这将生成512x512像素的正确图像.

This generates the right image of 512x512 pixels.

我的问题是: 1)有没有办法产生图像,而不必通过imgplot = plt.imshow(rsizeArr)在线显示?我需要在笔记本中内联pylab,所以我不知道是否可以暂时"禁用它. 2)是否可以通过将"tesplot1.png"保存为正确的大小来忽略调整大小的步骤(img2)? 3)使用说明此处的矢量运算是什么?

My questions are: 1) Is there a way to produce the image w/o having to display it inline via imgplot = plt.imshow(rsizeArr)? I need pylab inline in my notebook, so I dont know if it can be 'temporarily' disabled. 2) Is there a way to ommit the resizing step (img2), by saving 'tesplot1.png' in the right size? 3) What would be the vector operation required to accomplish this using the explanation here?

推荐答案

  1. 请勿内联使用pylab.
  2. 如果您仍要内联使用pylab,请不要.不推荐使用.
  3. 内联使用%matplotlib
  4. 学习matplotlib OO方法,以免在当前活动轴上隐式地做事.

您的绘图问题将消失.

对于块事物,您希望按块将图像作为numpy数组进行处理:

For your block things you want to process your image as a numpy array by block: How can I efficiently process a numpy array in blocks similar to Matlab's blkproc (blockproc) function for example.

这篇关于使用matplotlib和/或numpy缩小图像调色板的尺寸以创建块状图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆