View Single Post
Old 09-04-2020, 12:34 AM   #12
playful
Mammal
playful has become one with the cosmosplayful has become one with the cosmosplayful has become one with the cosmosplayful has become one with the cosmosplayful has become one with the cosmosplayful has become one with the cosmosplayful has become one with the cosmosplayful has become one with the cosmosplayful has become one with the cosmosplayful has become one with the cosmosplayful has become one with the cosmos
 
playful's Avatar
 
Posts: 126
Karma: 21380
Join Date: Oct 2010
Location: Right Here
Device: Onyx Note Pro, Kindle DXG
Quote:
Originally Posted by davidfor View Post
For the record, the calibre editor can retrieve external resources such as images from a URL. Beyond a couple of quick tests, I haven't used it.
Haha, I think you missed this. : )

Quote:
Originally Posted by playful View Post
With Calibre I found one workaround:
- open the Epub generated by Sigil
- Edit
- Tools / External Links / Download external resources
- Save a copy
Anyhow… Realized that the dpi needs to be changed in order for the images to display properly on my Onyx. When I download the pictures via Calibre as above, the images keep their original nominal dpi (meaning, according to metadata) of 300 and look like postage stamps.

In order to avoid having a second step, I ended up downloading the images in the Python script and setting the dpi using exiftool at the same time. Had to mess around with the options a bit. (There's an `exif` package for Python but as of writing the `x_resolution` and `y_resolution` does not properly set the dpi for Windows.)

In case it can help anyone on the same track, here's my download function (just fix the path to exiftool on the `comm =` line).

Usage:
url → "http://example.com/my.jpg"
folder → "path/to/output/folder"
target → "file_name.jpg"
[optional] dpi → 72

Code:
import requests
import shutil
import subprocess

from requests.exceptions import Timeout

def download_image(url, folder, target_name, dpi=False, timeout=20):
    """
    Usage:
    url → "http://example.com/my.jpg"
    folder → "path/to/output/folder"
    target → "file_name.jpg"
    [optional] dpi → 72
    """

    try:
        resp = requests.get(url, timeout=timeout, stream=True)
        if resp.status_code != 200:
            return False
        path = f"{folder}{target_name}"
        with open(path, 'wb') as f:
            resp.raw.decode_content = True
            shutil.copyfileobj(resp.raw, f)

        # Try to change the dpi
        if dpi and type(dpi) is int:
            # https://exiftool.org/exiftool_pod.html#OPTIONS
            comm = ["PATH/TO/exiftool.exe", '-charset', 'filename='
                           , '-q', '-overwrite_original_in_place'
                           , f'-Xresolution={dpi}', f'-Yresolution={dpi}'
                           , path]
            subprocess.run(comm)
        return True

    except:  # bare exception: the caller just needs the False value
        return False

Last edited by playful; 09-04-2020 at 05:53 PM. Reason: Add Timeout import
playful is offline   Reply With Quote