![]() |
#16 |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 262
Karma: 110864
Join Date: Mar 2013
Location: Bordeaux, France
Device: Kobo Glo, Aura HD, kindle paperwhite
|
Well, actually, no luck on the import, but the script here works:
- takes a Calibre catalog as input - the covers must be in the first column - outputs a css file with the covers as base64 BLOB Code:
import csv import base64 #assume cover paths are in the first column of the csv with open('C:\Users\Fra\Documents\_permanent\Papier20150901.csv', 'rb') as file1, open('C:\Users\Fra\Documents\_permanent\Papier20150901covers.csv', '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) writer.writerow(headers) #For all other lines, put double quotes around each cell writer = csv.writer(file2, delimiter=',', quoting=csv.QUOTE_ALL) for i, row in enumerate(reader): if row[0] == "": writer.writerow(row) else: with open(row[0], "rb") as image_file: row[0] = base64.b64encode(image_file.read()) writer.writerow(row) François |
![]() |
![]() |
![]() |
#17 |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 262
Karma: 110864
Join Date: Mar 2013
Location: Bordeaux, France
Device: Kobo Glo, Aura HD, kindle paperwhite
|
Second version of the script... if anyone needs that for another tool.
Code:
import csv import base64 #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... ipath = 'C:\Users\Fra\Documents\_permanent\Papier20150903last.csv' wpath = 'C:\Users\Fra\Documents\_permanent\Papier20150903Coverlast5.csv' #requires no space in the path... pity it would be simpler with the normal Calibre\ressource\image path defaultcover = "C:\PortableApps\default_cover.png" 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: with open(row[0], "rb") as image_file: s = base64.b64encode(image_file.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 François |
![]() |
![]() |
Advert | |
|
![]() |
#18 |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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 |
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Export catalog to Excel | myki | Library Management | 17 | 03-19-2015 01:19 PM |
Calibre - Export/Import Settings and Catalog | joseph.voros | Library Management | 4 | 06-13-2014 11:19 PM |
Select books based on info from Conversion > Export Catalog | halfcore | Library Management | 2 | 10-22-2013 06:15 PM |
Changing cover for catalog | Terisa de morgan | Library Management | 2 | 03-11-2011 02:57 AM |
Export Catalog *and* the stored cover? | Hitch | Library Management | 2 | 02-24-2011 10:50 PM |