Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JPEG instead of PNG as intermediate format #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified screenshots/Zotero-OCR-Preferences.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/chrome/content/preferences.xul
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<label value="Maximum number of pages for which an individual HTML attachment is created:"/>
<textbox id="pref-zoteroocr-max-html-pages" preference="pref-zoteroocr-max-html-pages" width="20"/>
</hbox>
<checkbox preference="pref-zoteroocr-output-png" label="Save the intermediate PNGs as well in the folder"/>
<checkbox preference="pref-zoteroocr-output-png" label="Save the intermediate images as well in the folder"/>
<checkbox preference="pref-zoteroocr-output-as-copy-attachment" label="Import the resulting PDF as a copy instead of as a file link"/>
</groupbox>
</prefpane>
Expand Down
21 changes: 20 additions & 1 deletion src/chrome/content/zoteroocr.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,26 @@ Zotero.OCR = new function() {
let ocrbase = Zotero.Prefs.get("zoteroocr.overwritePDF") ? base : base + '.ocr';
// TODO filter out PDFs which have already a text layer

// build the pdftoppm arguments based on hidden preferences:
// => will produce a PDF output with reasonable size and image quality
// File format: JPEG by default instead of PNG
// JPEG quaility 70/100 (pdftoppm default is 75)
// JPEG Hufmann tables optimization: yes (pdftoppm default is no)
// Use progressive JPEF: yes (pdftoppm default is no)
let imageFormat = Zotero.Prefs.get("zoteroocr.imageFormat");
let pdftoppmCmdArgs;
if (imageFormat == "jpg" || imageFormat == "jpeg") {
imageFormat = "jpg";
let jpegQuality = Zotero.Prefs.get("zoteroocr.jpegQuality");
let jpegProgressive = Zotero.Prefs.get("zoteroocr.jpegProgressive");
let jpegOptimization = Zotero.Prefs.get("zoteroocr.jpegOptimization");
pdftoppmCmdArgs = ['-jpeg', '-jpegopt', 'quality='+jpegQuality+',progressive='+jpegProgressive+',optimize='+jpegOptimization, '-r', Zotero.Prefs.get("zoteroocr.outputDPI"), pdf, dir + '/page'];

} else {
imageFormat = "png";
pdftoppmCmdArgs = ['-png', '-r', Zotero.Prefs.get("zoteroocr.outputDPI"), pdf, dir + '/page'];
}

// extract images from PDF
let imageList = OS.Path.join(dir, 'image-list.txt');
if (!(yield OS.File.exists(imageList))) {
Expand All @@ -141,7 +161,6 @@ Zotero.OCR = new function() {
Zotero.debug("Running " + pdfinfo + ' ' + pdfinfoCmdArgs.join(' '));
yield Zotero.Utilities.Internal.exec(pdfinfo, pdfinfoCmdArgs);

let pdftoppmCmdArgs = ['-png', '-r', Zotero.Prefs.get("zoteroocr.outputDPI"), pdf, dir + '/page'];
Zotero.debug("Running " + pdftoppm + ' ' + pdftoppmCmdArgs.join(' '));
yield Zotero.Utilities.Internal.exec(pdftoppm, pdftoppmCmdArgs);
}
Expand Down
6 changes: 6 additions & 0 deletions src/defaults/preferences/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ pref("extensions.zotero.zoteroocr.maximumPagesAsHtml", "5");
pref("extensions.zotero.zoteroocr.outputDPI", "300");
pref("extensions.zotero.zoteroocr.PSMMode", "3");
pref("extensions.zotero.zoteroocr.outputAsCopyAttachment", true);
// Hidden pdftoppm preferences
pref("extensions.zotero.zoteroocr.imageFormat", "jpg");
pref("extensions.zotero.zoteroocr.jpegQuality", "70");
pref("extensions.zotero.zoteroocr.jpegProgressive", "y");
pref("extensions.zotero.zoteroocr.jpegOptimization", "y");

6 changes: 6 additions & 0 deletions src/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ pref("extensions.zotero.zoteroocr.maximumPagesAsHtml", "5");
pref("extensions.zotero.zoteroocr.outputDPI", "300");
pref("extensions.zotero.zoteroocr.PSMMode", "3");
pref("extensions.zotero.zoteroocr.outputAsCopyAttachment", true);
// Hidden pdftoppm preferences
pref("extensions.zotero.zoteroocr.imageFormat", "jpg");
pref("extensions.zotero.zoteroocr.jpegQuality", "70");
pref("extensions.zotero.zoteroocr.jpegProgressive", "y");
pref("extensions.zotero.zoteroocr.jpegOptimization", "y");

2 changes: 1 addition & 1 deletion src/prefs.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<label value="Maximum number of pages for which an individual HTML attachment is created:"/>
<html:input type="text" id="pref-zoteroocr-max-html-pages" preference="extensions.zotero.zoteroocr.maximumPagesAsHtml" width="20"/>
</hbox>
<checkbox preference="extensions.zotero.zoteroocr.outputPNG" label="Save the intermediate PNGs as well in the folder"/>
<checkbox preference="extensions.zotero.zoteroocr.outputPNG" label="Save the intermediate images as well in the folder"/>
<checkbox preference="extensions.zotero.zoteroocr.outputAsCopyAttachment" label="Import the resulting PDF as a copy instead of as a file link"/>
</groupbox>
</vbox>
31 changes: 28 additions & 3 deletions src/zotero-ocr.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,30 @@ ZoteroOCR = {
let ocrbase = Zotero.Prefs.get("zoteroocr.overwritePDF") ? base : base + '.ocr';
// TODO filter out PDFs which have already a text layer

// build the pdftoppm arguments based on hidden preferences:
// => will produce a PDF output with reasonable size and image quality
// File format: JPEG by default instead of PNG
// JPEG quaility 70/100 (pdftoppm default is 75)
// JPEG Hufmann tables optimization: yes (pdftoppm default is no)
// Use progressive JPEF: yes (pdftoppm default is no)
let imageFormat = Zotero.Prefs.get("zoteroocr.imageFormat");
let pdftoppmCmdArgs;
if (imageFormat == "jpg" || imageFormat == "jpeg") {
imageFormat = "jpg";
let jpegQuality = Zotero.Prefs.get("zoteroocr.jpegQuality");
let jpegProgressive = Zotero.Prefs.get("zoteroocr.jpegProgressive");
let jpegOptimization = Zotero.Prefs.get("zoteroocr.jpegOptimization");
pdftoppmCmdArgs = ['-jpeg', '-jpegopt', 'quality='+jpegQuality+',progressive='+jpegProgressive+',optimize='+jpegOptimization, '-r', Zotero.Prefs.get("zoteroocr.outputDPI"), pdf, dir + '/page'];

} else {
imageFormat = "png";
pdftoppmCmdArgs = ['-png', '-r', Zotero.Prefs.get("zoteroocr.outputDPI"), pdf, dir + '/page'];
}

// extract images from PDF
let imageList = PathUtils.join(dir, 'image-list.txt');
if (!(await IOUtils.exists(imageList))) {
try {
let pdftoppmCmdArgs = ['-png', '-r', Zotero.Prefs.get("zoteroocr.outputDPI"), pdf, dir + '/page'];
Zotero.debug("Running " + pdftoppm + ' ' + pdftoppmCmdArgs.join(' '));
await Zotero.Utilities.Internal.exec(pdftoppm, pdftoppmCmdArgs);
}
Expand All @@ -189,8 +208,14 @@ ZoteroOCR = {
(entries) => {
for (const entry of entries) {
Zotero.debug('IOutils.getChildren() ran', entry);
if (entry.match(/-\d+\.png$/)) {
imageListArray.push(entry);
if (imageFormat == "jpg") {
if (entry.match(/-\d+\.jpg$/)) {
imageListArray.push(entry);
}
} else {
if (entry.match(/-\d+\.png$/)) {
imageListArray.push(entry);
}
}
}
Zotero.debug('Files are now:')
Expand Down
Loading