python随机排列图片_更改图片中的随机像素,python
我想编写一个函数,在这张图片的天空中创建一个随机数(在m和n之间,包括m和n)(
http://tinypic.com/r/34il9hu/6).我希望星星应该由一个白色像素或一个4个相邻白色像素的正方形组成.我也不想在树枝,月亮或鸟上放置一个“星”(1像素)虽然
我怎么能在python中这样做?有人可以帮忙吗?谢谢!
到目前为止我有这个:
到目前为止,我已经开始并且已经出来了,我不知道它是否是正确的,或者即使我在正确的轨道上:
def randomStars(small, large):
import random
file = pickAFile()
pic = makePicture(myPic)
#x = random.randrange(getWidth(pic))
#y = random.randrange(getHeight(pic))
for pixel in pic.getAllPixels():
if random.random() < 0.25:
pixel.red = random.randint(256)
pixel.green = random.randint(256)
pixel.blue = random.randint(256)
show(pic)
我不知道我在做什么:(
最佳答案 这似乎是尝试
superpixels的一个很好的例子,由
skimage实现.你可以更容易地解决你的问题.
import urllib
import random
import io
import matplotlib.pyplot as plt
import skimage.segmentation
import pandas
# Read the image
f = io.BytesIO(urllib.urlopen('http://oi46.tinypic.com/34il9hu.jpg').read())
img = plt.imread(f, format='jpg')
# Prefer to keep pixels together based on location
# But not too much, so we still get some branches.
superpixel = skimage.segmentation.slic(img, n_segments=200, ratio=20)
plt.imshow(superpixel%7, cmap='Set2')
现在我们有超像素,我们可以通过每个超像素来做分类更容易一些.你可以在这里使用一些花哨的分类,但是这个例子很简单,蓝天,让我们手工完成.
# Create a data frame with the relative blueish of every super pixel
# Convert image to hsv
hsv = matplotlib.colors.rgb_to_hsv(img.astype('float32')/255)
# Define blueish as the percentage of pixels in the blueish range of the hue space
df =pandas.DataFrame({'superpixel':superpixel.ravel(),
'blue':((hsv[:,:,0] > 0.4) & (hsv[:,:,0]<0.8)).astype('float32').ravel(),
'value':hsv[:,:,2].ravel()})
grouped = df.groupby('superpixel').mean()
# Lookup the superpixels with the least blue
blue = grouped.sort('blue', ascending=True).head(100)
# Lookup the darkest pixels
light = grouped.sort('value', ascending=True).head(50)
# If superpixels are too dark or too blue, get rid of them
mask = (np.in1d(superpixel, light.index ).reshape(superpixel.shape) |
np.in1d(superpixel, blue.index ).reshape(superpixel.shape))
# Now we can put the stars on the blueish, not too darkish areas
def randomstar(img, mask):
"""random located star"""
x,y = random.randint(1,img.shape[0]-1), random.randint(1,img.shape[1]-1)
if not mask[x-1:x+1, y-1:y+1].any():
# color not so random
img[x,y,:] = 255
img[x-1,y,:] = 255
img[x+1,y,:] = 255
img[x,y-1,:] = 255
img[x,y+1,:] = 255
for i in range(100):
randomstar(img, mask)
plt.imshow(img)
python随机排列图片_更改图片中的随机像素,python相关推荐
- Python灰帽子_黑客与逆向工程师的Python编程之道
收藏自用 链接:Python灰帽子_黑客与逆向工程师的Python编程之道
- python爬图片_网络爬虫经验:反爬和反反爬
我想很多人入门python是图片爬虫,就是HTTP请求,保存一下图片,用python实现非常快.网上很多爬虫的教程就讲到这里,实际上很单一,看了跟没看没什么区别,都是找一下网页的规律,然后Beauti ...
- python画圣诞老人图片_来得瑟一下!用Python做一个缩放自如的圣诞老人
原标题:来得瑟一下!用Python做一个缩放自如的圣诞老人 圣诞节又要到了,虽说我们中国人不提倡过西方的节日, 但是商家们还是很喜欢的,估计有对象的男孩纸女孩纸们也很喜欢吧. 今天的主题是为大家展示如 ...
- 调python返回图片_三个好习惯,帮你写好Python里的异常处理
如果你用 Python 编程,那么你就无法避开异常,因为异常在这门语言里无处不在.打个比方,当你在脚本执行时按 ctrl+c 退出,解释器就会产生一个 KeyboardInterrupt 异常.而 K ...
- python 重复图片_删除重复文件或图片(去重)的python代码
通过python爬虫或其他方式保存的图片文件通常包含一些重复的图片或文件, 通过下面的python代码可以将重复的文件删除以达到去重的目的.其中,文件目录结构如下图: # /usr/bin/env p ...
- python浮雕图片_用Python来画浮雕画
浮雕艺术在世界各地都可以见到,中国古代在唐朝以来就有许多浮雕效果的东西,很多的大型纪念性建筑都有这种作为装饰,常见的有花窗,龙柱等. 简单的来说,浮雕就是把所要呈现的图像突起于石头表面,根据凹凸的程度 ...
- python新手程序_推荐:一个适合于Python新手的入门练手项目
随着人工智能的兴起,国内掀起了一股Python学习热潮,入门级编程语言,大多选择Python,有经验的程序员,也开始学习Python,正所谓是人生苦短,我用Python 有个Python入门练手项目, ...
- python 面试问题_值得阅读的30个Python面试问题
python 面试问题 Interview questions are quite tricky to predict. In most cases, even peoples with great ...
- python核心理念_《三天搞定Python基础概念之第一天》中文版
前言: 首先,非常感谢Jiang老师将其分享出来!本课件非常经典! 经过笔者亲测,竟然确实只要三天,便可管中窥豹洞见Python及主要库的应用.实属难得诚意之作! 其次,只是鉴于Jiang老师提供的原 ...
最新文章
- Java 面试宝典全公开!
- matlba 正交基
- 基于【IPv6】静态路由和默认路由的配置
- code ro rw zi 编译的一个ARM的程序,编译结果中的一句话
- 在 Oracle 和 PHP 中使用 LOB
- python获取字典的值_python取出字典中的所有值的两种方法
- android viewgroup点击变色,Android ViewGroup事件分发
- Django项目部署在Centos7
- 上位机服务器通讯协议,家庭监护终端与手机、上位机及服务器通讯协议22.doc
- Silverlight之我见——数据批示(2)
- LeetCode 242 Valid Anagram
- matlab实现模拟退火算法
- 移动端关于手机横屏时样式修改
- oracle 10g固定执行计划,oracle 10g执行计划 (转帖)
- mfc匹配关键字颜色显示_如何在多台显示器上匹配颜色
- 赫尔维兹_勒奇超越函数(matlab自编函数)
- ch341a i2c 安卓_CH341-I2C-labview-all-vision CH341A的I2C接口Labview all vision - 下载 - 搜珍网...
- C# serialport串口接收数据异常,出现很多3F的解决方法
- 启明欣欣STM32开发板闪烁LED实验
- 数据库连接池种类、C3P0数据库连接池、德鲁伊数据库连接池