python Python中的图像Glitching实用程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Python中的图像Glitching实用程序相关的知识,希望对你有一定的参考价值。

import random
import urllib
import Image
from BeautifulSoup import BeautifulStoneSoup
 
def find_an_image(keyword):
    response = urllib.urlopen('http://api.flickr.com/services/feeds/photos_public.gne?tags=' + keyword + '&lang=en-us&format=rss_200')
    soup = BeautifulStoneSoup(response)
 
    image_list = []
 
    for image in soup.findAll('media:content'):
        image_url = dict(image.attrs)['url']
        image_list.append(image_url)
 
    return random.choice(image_list)
   
def download_an_image(image_url):
    filename = image_url.split('/')[-1]
    urllib.urlretrieve(image_url, filename)
   
    return filename
 
def get_random_start_and_end_points_in_file(file_data):
    start_point = random.randint(2500, len(file_data))
    end_point = start_point + random.randint(0, len(file_data) - start_point)
 
    return start_point, end_point
 
def splice_a_chunk_in_a_file(file_data):
    start_point, end_point = get_random_start_and_end_points_in_file(file_data)
    section = file_data[start_point:end_point]
    repeated = ''
 
    for i in range(1, random.randint(1,5)):
        repeated += section
 
    new_start_point, new_end_point = get_random_start_and_end_points_in_file(file_data)
    file_data = file_data[:new_start_point] + repeated + file_data[new_end_point:]
    return file_data
   
def glitch_an_image(local_image):
    file_handler = open(local_image, 'r')
    file_data = file_handler.read()
    file_handler.close()
 
    for i in range(1, random.randint(1,5)):
        file_data = splice_a_chunk_in_a_file(file_data)
 
    file_handler = open(local_image, 'w')
    file_handler.write(file_data)
    file_handler.close
 
    return local_image
 
if __name__ == '__main__':
    image_url = find_an_image('art')
    local_image = download_an_image(image_url)
    image_glitch_file = glitch_an_image(local_image)
 
    print image_glitch_file

以上是关于python Python中的图像Glitching实用程序的主要内容,如果未能解决你的问题,请参考以下文章

Python-OpenCV中的图像模糊

python - 如何在python中使用MTCNN从文件夹中的图像中提取人脸?

Python,调整文件夹中的图像大小[重复]

检测二维图像中的标记[Python/OpenCV]

[OpenCV-Python] OpenCV 中的图像处理 部分 IV (二)

使用python 3.6中的matplotlib.image在python中打开.jpg图像