![]() |
#1 |
Connoisseur
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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 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 |
![]() |
![]() |
![]() |
#2 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,188
Karma: 27110894
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.
|
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Connoisseur
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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?
|
![]() |
![]() |
![]() |
#4 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,188
Karma: 27110894
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
See utils/magick/draw.py
|
![]() |
![]() |
![]() |
#5 |
Connoisseur
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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 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. |
![]() |
![]() |
Advert | |
|
![]() |
Tags |
cannot identify image, pil, png |
|
![]() |
||||
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 | 0 | 10-03-2007 01:06 AM |