Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Software > Calibre > Recipes

Notices

Reply
 
Thread Tools Search this Thread
Old 09-25-2012, 02:45 PM   #1
RobFreundlich
Connoisseur
RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.
 
Posts: 74
Karma: 10000010
Join Date: Jan 2012
Device: Android Tablet with Calibre Companion and Moon+ Reader Pro
Certain PNG files fail to load

Certain PNG files in one of my feeds are failing to load, with the following error:

Code:
Fetching http://c.o0bg.com/rw/Boston/2011-2020/2012/09/25/Comics/aj_c120925.png
Traceback (most recent call last):
  File "site-packages\calibre\web\fetch\simple.py", line 388, in process_images
  File "site-packages\PIL\Image.py", line 1982, in open
IOError: cannot identify image file
Unlike the last time I posted something like this, where it was my own mistake, this seems to be a problem in the image files themselves. The image file at http://c.o0bg.com/rw/Boston/2011-202...aj_c120925.png seems to have something weird about it - I can open it in Chrome and Firefox (or, for that matter, GIMP), but IE 8 gives me a "red X broken image" picture for it.

I've attached a copy here as well. This simple script fails with the same error message as calibre (not surprising, given that it uses PIL):

Code:
from PIL import Image

im = Image.open("aj_c120925.png")
print "Format:", im.format, ", Mode:", im.mode, ", Size:", im.size
Can anyone offer any advice on this?
Attached Thumbnails
Click image for larger version

Name:	aj_c120925.png
Views:	281
Size:	230.7 KB
ID:	92863  
RobFreundlich is offline   Reply With Quote
Old 09-26-2012, 01:07 AM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,826
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
There isn't anything simple you can do about it, but if you are willing to code a little you can impleent image_url_processor in your recipe. There you can download the image and try processing ti with imagemagick (which calibre bundles). Maybe imagemagick can "fix" whatever is wrong with the image and then return the url of the fixed image so that PIL will not choke.
kovidgoyal is offline   Reply With Quote
Advert
Old 09-26-2012, 09:47 AM   #3
RobFreundlich
Connoisseur
RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.
 
Posts: 74
Karma: 10000010
Join Date: Jan 2012
Device: Android Tablet with Calibre Companion and Moon+ Reader Pro
Thanks - I'll give that a try. One more question: I looked in the pylib.zip that came with Calibre, and don't see imagemagick. Where is it bundled, and what's the correct import statement?
RobFreundlich is offline   Reply With Quote
Old 09-26-2012, 10:20 AM   #4
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,826
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
See utils/magick/draw.py
kovidgoyal is offline   Reply With Quote
Old 09-27-2012, 08:58 AM   #5
RobFreundlich
Connoisseur
RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.RobFreundlich ought to be getting tired of karma fortunes by now.
 
Posts: 74
Karma: 10000010
Join Date: Jan 2012
Device: Android Tablet with Calibre Companion and Moon+ Reader Pro
Solved it

Thanks to your tips, I've solved this problem. Here's my solution:

Code:
    valid_filename_chars = "-_.%s%s" % (string.ascii_letters, string.digits)

    def image_url_processor(self, baseurl, url):
        self.log("===================\nbaseurl: ", baseurl, "\nurl: ", url)
        # This is a hack because some of the URLs just have a leading
        # // instead of http://
        if url.startswith("//"):
            url = "http:" + url

        url = self.get_image(url)

        self.log("url out: ", url, "\n===================")

        return url

    def get_image(self, url):
        # Another hack - sometimes the URLs just have a leading /,
        # in which case I stick on "http://" and the correct domain
        if url.startswith("/"):
          url = self.make_url(url)

        # Get the image bytes
        br = BasicNewsRecipe.get_browser()
        response = br.open(url)
        data = response.get_data()

        # write it to a local file whose name is based on the URL
        filename = ''.join(c for c in url if c in self.valid_filename_chars)
        self.log("filename=%s" % filename)

        f = open(filename, "wb")
        f.write(data)
        f.close()

        # Try to read it with PIL, which is what the containing app will do
        try:
            im = PIL.Image.open(filename)
        except:
            # If it failed, read it with ImageMagick and write it to a new file,
            # changing the URL to point to the new file
            self.log("Could not open ", filename, " from ", url)
            self.log("Trying to open and re-save with ImageMagick")
            image = calibre.utils.magick.Image()
            image.read(filename)
            image.save("new_" + filename)
            url = os.getcwd() + "/new_" + filename
            url = "file:///" + url.replace("\\", "/")
            self.log("Succeeded.  Using local file")

        return url
Luckily, ImageMagick manages to load the file successfully AND heal it when saving it back out, so I didn't have to look into what exactly was wrong with these files. For curiosity's sake, I did do a comparison of the old and new, and there are differences, but since I don't know squat about the PNG format (and don't have the time or energy to learn), I don't know exactly what they mean.

If I had the time, I'd grab the calibre source, find where it's doing the image load, put something like this in, and submit it as a bug fix, but unfortunately, I don't. If anyone else out there wants to do it, go ahead - I happily release this code (particularly the try...except bit that solves the problem) into the public domain.
RobFreundlich is offline   Reply With Quote
Advert
Reply

Tags
cannot identify image, pil, png

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fetch news does not download *.png files ? remaille Recipes 1 08-16-2011 04:59 PM
PRS-600 Cannot load BBeB files? JoeH Sony Reader 19 01-04-2010 03:14 PM
where to get autorun.xml, icon.png, key.png, lut.bin for PRS-505 obender Sony Reader Dev Corner 2 01-25-2009 03:20 PM
What files take longest to load/index? ProDigit Sony Reader 4 10-24-2008 02:52 PM
Combining multiple .gif or .png files into a specific PDF page. Raventhon PDF 0 10-03-2007 01:06 AM


All times are GMT -4. The time now is 11:32 PM.


MobileRead.com is a privately owned, operated and funded community.