如何使用Python将图像上传到Tumblr?

时间:2016-03-28 15:30:17

标签: python image-uploading tumblr visual-glitch

所以我要做的是,将图像从我的电脑上传到Tumblr,然后再次下载,用它做一些小故障并再次上传。目标是,重复该过程约100次。我希望它始终下载我上传的最新图片,以便结果看起来像时间轴。

到目前为止,我所拥有的代码仅包括毛刺部分和图像链接,如果您运行代码,该图像将被下载并出现故障。

import random
import urllib

from BeautifulSoup import BeautifulStoneSoup


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(2600, 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(3,3)):
        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(2,3)):
        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 = "https://upload.wikimedia.org/wikipedia/en/thumb/f/f5/Bid_logo.svg/1264px-Bid_logo.svg.png"
    local_image = download_an_image(image_url)
    image_glitch_file = glitch_an_image(local_image)

    print image_glitch_file

谢谢!

0 个答案:

没有答案