Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3.7
- """
- getimg.py
- Gets the current image of the day from NASA and saves this as
- current_background.jpg. The summary / description text is written
- to the image.
- Requires:
- PIL (apt-get install python-imaging or pip install PIL)
- feedparser (apt-get install python-feedparser or pip install feedparser)
- Christian Stefanescu
- http://0chris.com
- Based on the bash script of Jessy Cowan-Sharp - http://jessykate.com
- http://blog.quaternio.net/2009/04/13/nasa-image-of-the-day-as-gnome-background/
- intelli_draw method from:
- http://mail.python.org/pipermail/image-sig/2004-December/003064.html
- Made it compatible with Python 3.7
- """
- from pathlib import Path
- from urllib.request import urlopen
- import shutil
- import os
- from PIL import (
- Image,
- ImageDraw,
- ImageFont,
- )
- import feedparser
- # using configparser to save a initial configuration beside the script
- # to edit the settings without touching the source code..
- HOME = Path.home()
- DOWNLOAD_FOLDER = HOME / 'backgrounds/'
- LINK_PATH = DOWNLOAD_FOLDER / 'current_background.jpg'
- # Edit the font Path. FiraCode is a free font for programmers with ligatures
- FONT_PATH = r'C:\Users\Admin\AppData\Local\Microsoft\Windows\Fonts\FiraCode-Regular.ttf'
- FONT_SIZE = 20
- EMPTY_ROWS = 3
- # Don't change stuff beyond this point
- # Updated the rss-feed url
- FEED_URL = 'https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss'
- font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
- def get_latest_entry():
- """
- Get URL and description of the latest entry in the feed
- """
- feed = feedparser.parse(FEED_URL)
- return (feed.entries[0].enclosures[0].href, feed.entries[0].summary)
- def download_file(url):
- """
- Get the latest NASA image of the day from the feed, returns the name
- of the downloaded file.
- """
- remote_file = urlopen(url)
- local_name = url.split('/')[-1]
- local_path = DOWNLOAD_FOLDER / local_name
- local_path.write_bytes(remote_file.read())
- remote_file.close()
- return local_path
- def intelli_draw(drawer, text, font, containerWidth):
- """
- Figures out how many lines (and at which height in px) are needed to print
- the given text with the given font on an image with the given size.
- Source:
- http://mail.python.org/pipermail/image-sig/2004-December/003064.html
- # Todo
- # This function is too complicated
- # Try to remove the nested loops
- """
- words = text.split()
- lines = []
- lines.append(words)
- finished = False
- line = 0
- while not finished:
- thistext = lines[line]
- newline = []
- innerFinished = False
- while not innerFinished:
- if drawer.textsize(' '.join(thistext), font)[0] > containerWidth:
- newline.insert(0, thistext.pop(-1))
- else:
- innerFinished = True
- if len(newline) > 0:
- lines.append(newline)
- line = line + 1
- else:
- finished = True
- tmp = []
- for i in lines:
- tmp.append(' '.join(i))
- lines = tmp
- (width, height) = drawer.textsize(lines[0], font)
- return (lines, width, height)
- def write_description(img_path, text):
- """
- Write the given text to the given imagefile and overwrite it.
- # choose better variable names
- """
- img = Image.open(img_path)
- (img_width, img_height) = img.size
- draw = ImageDraw.Draw(img)
- lines, tmp, h = intelli_draw(draw, text, font, img_width)
- j = EMPTY_ROWS
- for i in lines:
- draw.text((0, 0 + j * h), i, font=font)
- j = j + 1
- img.save(img_path, 'JPEG')
- if __name__ == '__main__':
- # argparse ?
- if not DOWNLOAD_FOLDER.exists():
- DOWNLOAD_FOLDER.mkdir()
- url, text = get_latest_entry()
- img_path = download_file(url)
- write_description(img_path, text)
- shutil.copy(img_path, LINK_PATH)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement