Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8260355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:04:50+00:00 2026-06-08T03:04:50+00:00

I wrote a little program which downloads the latest daily tide graph from NOAA

  • 0

I wrote a little program which downloads the latest daily tide graph from NOAA, adds a text table to the image containing tidal information, and then sets this image as my desktop wallpaper.

from bs4 import BeautifulSoup
import Image, ImageDraw, ImageFont
from urllib import urlretrieve
import urllib2
import time
import ctypes
import sys
import os
import datetime
import traceback


def Fetch_tideGraph():
    # Fetch the Tides Graph .gif
    #
    # Fetch the Station Home Page
    try:
        url = 'http://tidesandcurrents.noaa.gov/noaatidepredictions/viewDailyPredictions.jsp?Stationid=8637689'
        page = urllib2.urlopen(url)
        soup = BeautifulSoup(page.read())

        # Find the Url to the tides graph
        ImgElement = str(soup.find_all('input', { "alt" : "Daily Tide Prediction graphical plot" }))
        soup = BeautifulSoup(ImgElement)
        tag = soup.input
        src = str(tag['src'])
        imgUrl = 'http://tidesandcurrents.noaa.gov/noaatidepredictions' + src.lstrip('.')
        print imgUrl
    except Exception, e:
        print "Failed to Load Webpage"
        traceback.print_exc()
        raw_input('Press Enter to exit...')
        sys.exit()

    # Download the tide graph
    try:
        print "Downloading gif....."
        urlretrieve(imgUrl, "C:\\Users\\Jack\\Documents\\Py Projects\\tides.gif")
        # Allow time for image to save:
        time.sleep(5)
        print "Gif Downloaded."
    except:
        print "Failed to Download new GIF"
        raw_input('Press Enter to exit...')
        sys.exit()

    # Convert gif to jpg
    try:
        print "Converting GIF to JPG...."
        Image.open("C:\\Users\\Jack\\Documents\\Py Projects\\tides.gif").convert('RGB').save("C:\\Users\\Jack\\Documents\\Py Projects\\tides.jpg")
        print "Image Converted"
    except Exception, e:
        print "Conversion FAIL:", sys.exc_info()[0]
        traceback.print_exc()
        pass

def update_wallpaper():
    # Change the Wallpaper
    imgPath = 'C:\\Users\\Jack\\Documents\\Py Projects\\tides.jpg'
    SPI_SETDESKWALLPAPER = 20
    try:
        print "Updating WallPaper..."
        ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, imgPath, 0)
        print "Wallpaper Updated"
    except:
        print "Wallpaper update FAIL"
        raw_input('Press Enter to exit...')

def todays_tide():
    # Print Table of Todays Tides
    # Open Tide Tables and Image File:
    try:
        info = open('C:\\Users\\Jack\\Documents\\Py Projects\\AnnualTides.txt', 'r')
        img = Image.open('C:\\Users\\Jack\\Documents\\Py Projects\\tides.jpg')
    except IOError:
        print "Tide table look-up failed."
        traceback.print_exc()
    # Load Font:
    f = ImageFont.load_default()
    draw = ImageDraw.Draw(img)
    # Lookup todays tides by matching date to table:
    now = datetime.datetime.now()
    date = now.strftime("%Y/%m/%d")
    tomorrow = now.strftime("%Y/%m/") + str(now.day+1)
    # Draw on image:
    y = 30
    head = '   Date    Day Time      Ft cm High/Low'
    draw.text((150, 20), head, (0,0,0), font=f)
    for line in info.readlines():
        if date in line or tomorrow in line:
            line = line.replace('\t', ' ')
            draw.text((150, y), line, (0,0,0), font=f)
            y += 10
    info.close()
    img.save("C:\\Users\\Jack\\Documents\\Py Projects\\tides.jpg")




##########################################

def main():
    try:
        Fetch_tideGraph()
        todays_tide()
        update_wallpaper()
        raw_input('Press Enter to exit...')
    except:
        print "Error in main()", sys.exc_info()[0]
        raw_input('Press Enter to exit...')

###########################################

if __name__ == "__main__":
    main()

The code, despite being rather ugly, seems to work well enough save for one little bug that I can not seem to squash. Most of the time when I run the program everything goes smooth, but every couple of runs I get the following output:

>>> 
http://tidesandcurrents.noaa.gov/noaatidepredictions/serveimage?filename=images/8637689/21072012/855/8637689_2012-07-22.gif
Downloading gif.....
Gif Downloaded.
Converting GIF to JPG....
Conversion FAIL: <type 'exceptions.IOError'>
Traceback (most recent call last):
  File "C:\Users\Jack\Documents\Py Projects\Tides.py", line 54, in Fetch_tideGraph
    Image.open("C:\\Users\\Jack\\Documents\\Py Projects\\tides.gif").convert('RGB').save("C:\\Users\\Jack\\Documents\\Py Projects\\tides.jpg")
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1980, in open
    raise IOError("cannot identify image file")
IOError: cannot identify image file
Updating WallPaper...
Wallpaper Updated
Press Enter to exit...
>>> 

Help me to understand and remedy this Error. Why does it only occur part of the time?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:04:57+00:00Added an answer on June 8, 2026 at 3:04 am

    The IOError suggests your gif file is not complete, and thus not recognizable as an Image to PIL. The most likely issue is that your download is taking longer to complete than the 5 seconds you’re allowing in the try: block to download the gif file.

    You might try a couple other url-fetching approaches to make sure you get a complete file before continuing. This one has a progress bar, but also try here and here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a little bit confused about simple program which I wrote, can You please
I wrote a little program, which is working like a ping command(i use ICMPSendEcho2),
I just wrote a little program which will be executed as a post-build step
I wrote a little program which prints help information if argument is not passed.
I just wrote a little program which searches for contours in a color thresholded
I wrote a little program using fork to create new processes which use pipe
I want to write a little program which is able to work with Google
i wrote a little test programm which queries the status servlet of a tomcat
I wrote a little program that use activemq embedded broker. Program run on one
I wrote a little program that solves 49151 sudoku's within an hour for an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.