Skip to content

Instantly share code, notes, and snippets.

@Aloxaf
Last active Oct 28, 2021
Embed
What would you like to do?
BOOKWALKER 图片提取
import json
from PIL import Image
coords = json.load(open('coords.json'))
for name, coord in coords.items():
print(f'\r重排列 {name} ……', end='')
try:
img = Image.open(name)
new = Image.new("RGB", img.size)
for pos in coord:
part = img.crop((pos[0], pos[1], pos[0] + pos[2], pos[1] + pos[3]))
new.paste(part, (pos[4], pos[5], pos[4] + pos[6], pos[5] + pos[7]))
new.save(name) #, quality=100, subsampling=0)
except Exception as e:
print(e)
// ==UserScript==
// @name bookwalker 图片提取
// @namespace Aloaxf
// @version 0.1
// @description 提取 bookwaler 中的图片
// @author Aloxaf
// @updateURL https://gist.githubusercontent.com/Aloxaf/016f4ed6f9fc823549754cf8bb566416/raw/ca6e27c979ca556f98147862824ec27c4b330f00/userscript.js
// @match https://pcreader.bookwalker.com.tw/*
// @match https://viewer.bookwalker.jp/*
// @icon https://www.google.com/s2/favicons?domain=bookwalker.com.tw
// @require https://cdn.jsdelivr.net/gh/eligrey/FileSaver.js@2.0.4/dist/FileSaver.min.js
// @require https://cdn.jsdelivr.net/gh/Stuk/jszip@3.5.0/dist/jszip.min.js
// @require https://cdn.jsdelivr.net/gh/stuk/jszip-utils@0.1.0/dist/jszip-utils.min.js
// @grant unsafeWindow
// ==/UserScript==
function addToZip(name, url) {
let content = new Promise((resolve, reject) => {
JSZipUtils.getBinaryContent(url, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
imagepack.file(name, content, {binary: true});
}
function drawImageHook(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) {
if (image.src && image.src.indexOf('.jpeg') != -1) {
let name = image.src.match(/(?<=\/)[^/]+(?=\.xhtml)/)[0] + ".jpeg";
let count = Object.keys(coords).length + 1;
if (!coords[name]) {
console.log(`[${count}] downloading ${name}`);
addToZip(name, image.src);
coords[name] = new Set();
if (autoscroll) {
NFBR.a6G.Initializer.B0U.menu.a6l.moveToPage(count + 4);
}
}
coords[name].add([sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight]);
}
return CanvasRenderingContext2D.prototype._drawImage.apply(this, arguments);
}
function setToJson(key, value) {
if (typeof value === 'object' && value instanceof Set) {
return [...value];
}
return value;
}
let imagepack = new JSZip();
let coords = {};
let autoscroll = false;
CanvasRenderingContext2D.prototype._drawImage = CanvasRenderingContext2D.prototype.drawImage;
CanvasRenderingContext2D.prototype.drawImage = drawImageHook;
unsafeWindow.saveAs = saveAs;
unsafeWindow.autoScroll = () => {
autoscroll = true;
};
unsafeWindow.saveAsZip = () => {
imagepack.file('coords.json', JSON.stringify(coords, setToJson));
imagepack
.generateAsync({type: "blob"})
.then(blob => {
saveAs(blob, "imagepack.zip");
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment