View Single Post
Old 09-05-2015, 04:45 AM   #18
fxp33
Addict
fxp33 figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.fxp33 figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.fxp33 figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.fxp33 figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.fxp33 figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.fxp33 figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.fxp33 figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.fxp33 figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.fxp33 figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.fxp33 figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.fxp33 figured out that Keyser Söze was the Kevin Spacey character in less than 20 minutes.
 
Posts: 262
Karma: 110864
Join Date: Mar 2013
Location: Bordeaux, France
Device: Kobo Glo, Aura HD, kindle paperwhite
Last development:

Images need to be resized as thumbnails.
Got an error with the "width" tile exceeding image size, so I just put 200... worked smoothly.


Code:
import csv
import base64
import PIL
from PIL import Image
#Converts the catalog (as csv) exported from Calibre for import with covers into PortoDB (android)
#assume cover paths are in the first column of the csv
#NEED MANUALLY ADDING a COMMA at the beginning of the result file...
#images are resized to thumbnails

ipath = 'C:\Users\Fra\Documents\_permanent\Papier20150903last.csv'
wpath = 'C:\Users\Fra\Documents\_permanent\Papier20150903Coverlast6.csv'
defaultcover = 'C:/PortableApps/default_cover.png'
target = 300    #size of the thumbnail image
thumbnailtemp = 'C:/PortableApps/thumbnail.jpg'

with open(ipath, 'rb') as file1, open(wpath, 'wb') as file2:
    reader = csv.reader(file1, delimiter=',' , quotechar='"', doublequote = True)

    #No quotes around the headers
    #write without looking for path
    headers = next(reader, None)  # returns the headers or `None` if the input is empty
    if headers:
        writer = csv.writer(file2, delimiter=',', quoting=csv.QUOTE_NONE)
        #tried to add the comma but it seems to corrup the file
        #headers.insert(0, "")
        writer.writerow(headers)

    #For all other lines, put double quotes around each cell
    writer = csv.writer(file2, delimiter=',', quoting=csv.QUOTE_ALL)
    #count the book to give them an id
    n = 1

    for i, row in enumerate(reader):
        if row[0] == "": row[0] = defaultcover

        #else:
        image_file = Image.open(row[0])
        originalWidth, originalHeight = image_file.size
        ratio = originalWidth / originalHeight
        if ratio > 1 :
            width = target
            height = int(width / ratio)
        else :
            height = target
            width = int(height * ratio)

        im2 = image_file.resize((200, height), Image.NEAREST) # linear interpolation in a 2x2 environment
        im2.save(thumbnailtemp, 'jpeg')
        with open(thumbnailtemp, "rb") as thumbimage:
            s = base64.b64encode(thumbimage.read())
             #trying to add line feeds at 76 bytes... not working yet
            #'\n'.join(s[pos:pos+76] for pos in xrange(0, len(s), 76))
        row[0] = s
        #insert a book id
        row.insert(0, n)
        writer.writerow(row)
        n += 1
fxp33 is offline   Reply With Quote