我怎么能在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 urllibimport randomimport ioimport matplotlib.pyplot as pltimport skimage.segmentationimport pandas# Read the imagef = 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 spacedf =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 blueblue = grouped.sort('blue',ascending=True).head(100)# Lookup the darkest pixelslight = grouped.sort('value',ascending=True).head(50)# If superpixels are too dark or too blue,get rID of themmask = (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 areasdef 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,:] = 255 img[x+1,:] = 255 img[x,y-1,y+1,:] = 255for i in range(100): randomstar(img,mask)plt.imshow(img)总结
以上是内存溢出为你收集整理的更改图片中的随机像素,python全部内容,希望文章能够帮你解决更改图片中的随机像素,python所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)