05-14-2024, 02:31 PM | #2056 |
Connoisseur
Posts: 69
Karma: 9464
Join Date: Jan 2021
Device: Likebook Mars
|
What is the best way to run k2pdfopt on an Android phone? I'm assuming via Linux? Has anyone successfully been able to get it to work and would have step-by-step instructions?
|
05-16-2024, 06:48 AM | #2057 |
Junior Member
Posts: 4
Karma: 10
Join Date: May 2021
Device: None
|
Good question above. Hoping to get some help here.
|
05-16-2024, 12:42 PM | #2058 |
Fuzzball, the purple cat
Posts: 1,283
Karma: 11087488
Join Date: Jun 2011
Location: California
Device: iPad
|
I don't have a test platform for compiling or running on an Android phone. I might be able to cross-compile, but I'd still need to test it. Is there a virtual Android phone simulator that will run on either a PC or a Mac Mini?
|
07-26-2024, 06:22 AM | #2059 | |
Junior Member
Posts: 9
Karma: 55638
Join Date: Jul 2024
Device: Sony PRS-T3
|
Quote:
You should install Termux and install packages k2pdfopt and ghostscript. Termux may not be available on google play and should be downloaded and installed manually. |
|
07-26-2024, 07:01 AM | #2060 | |
the rook, bossing Never.
Posts: 12,354
Karma: 92073397
Join Date: Jun 2017
Location: Ireland
Device: All 4 Kinds: epub eink, Kindle, android eink, NxtPaper11
|
Quote:
All a free download from Google. It was very like doing a Java app, except Google is a pain and keeps changing how you do I/O to SD card or storage. Really Windows for Workgroups 3.11 was better (apart from lack of Explorer desktop, as long as Win32s, 32 bit TCP/IP and 32 bit Disk etc all added), stupid changes since Android 3 and poor UI. Material Design is idiotic. But then I think Win10/Win11/Chrome/Mozilla UIs are all rubbish compared to mac OS9, Win9x, NT4.0/2K/XP UIs. |
|
08-05-2024, 05:41 PM | #2061 |
Junior Member
Posts: 4
Karma: 55640
Join Date: Aug 2024
Device: Kindle Paperwhite
|
Help?
Is this where I can ask for help with getting the formatting down? A lot of this is way over my head. And can I post screenshots here to show what I mean?
|
08-05-2024, 06:03 PM | #2062 | |
Grand Sorcerer
Posts: 5,527
Karma: 100606001
Join Date: Apr 2011
Device: pb360
|
Quote:
You can post screenshots here, but if they are larger than 600x600 please attach them rather than embed them. (Use the "Manage Attachments" tool.) Also, please don't post deep links to images because that frequently causes problems. |
|
08-06-2024, 03:05 AM | #2063 |
Member
Posts: 11
Karma: 10
Join Date: Apr 2022
Device: none
|
I wrote myself a dirty piece of code to rotate every CJK character by 90 degrees, which is designed to ignore Latin script if a word is wide enough. Not an effective implementation but with many magic numbers, though.
Code:
#! /usr/bin/env python3 # -*- coding: utf-8 -*- import sys import cv2 import numpy def rotate_image(image, angle): (h, w) = image.shape[: 2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, angle, 1.0) corrected = cv2.warpAffine(image, M, (w, h), flags = cv2.INTER_CUBIC, borderMode = cv2.BORDER_REPLICATE) return corrected def determine_score(arr): histogram = numpy.sum(arr, axis = 2, dtype = float) score = numpy.sum((histogram[..., 1 :] - histogram[..., : -1]) ** 2, axis = 1, dtype = float) return score def correct_skew(monoImg, delta = 0.1, limit = 5): angles = numpy.arange(-limit, limit + delta, delta) img_stack = numpy.stack([rotate_image(monoImg, angle) for angle in angles], axis = 0) scores = determine_score(img_stack) best_angle = angles[numpy.argmax(scores)] return best_angle orgImage = cv2.imread(sys.argv[1]) myImage = numpy.ascontiguousarray(numpy.rot90(orgImage)) (my_h, my_w, _) = myImage.shape grayImg = cv2.cvtColor(myImage, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(grayImg, (0,0), sigmaX=33, sigmaY=33) divide = cv2.divide(grayImg, blur, scale=255) _, myImage = cv2.threshold(divide,127,255,cv2.THRESH_BINARY) myImage = divide at_bs = 5 monoImg = cv2.ximgproc.niBlackThreshold(grayImg, 255, cv2.THRESH_BINARY, at_bs, -0.3, binarizationMethod=cv2.ximgproc.BINARIZATION_NICK) monoImg = cv2.adaptiveThreshold(monoImg, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) monoImg = cv2.bitwise_not(monoImg) angle = correct_skew(monoImg) print(angle) if not -0.1 < angle < 0.1: monoImg = rotate_image(monoImg, angle) myImage = rotate_image(myImage, angle) erosion_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 1)) erosion = cv2.erode(monoImg, erosion_kernel, iterations = 1) dilation_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 5)) dilation = cv2.dilate(erosion, dilation_kernel, iterations = 1) contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) dilation_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 1)) dilation = cv2.dilate(monoImg, dilation_kernel, iterations = 1) newImage = numpy.full(myImage.shape, 255, dtype=numpy.uint8) for cnt in contours: x, y, w, h = cv2.boundingRect(cnt) ROI = dilation[y:y+h, x:x+w] # 1 1 0 1 1 # original 1 right pad # 1 1 1 0 1 # original 2 right pad ROI1 = numpy.c_[ROI, numpy.zeros(len(ROI),dtype=numpy.uint8)] # 1 1 0 1 1 # right shift 1 # 1 1 1 0 1 # right shift 2 ROI2 = numpy.c_[numpy.zeros(len(ROI),dtype=numpy.uint8), ROI] # 0 1 0 0 1 0 # AND 1 # 0 1 1 0 0 0 # AND 2 gap = cv2.bitwise_and(ROI1, ROI2) # gap between pixel #0 1 1 0 1 0 # reduced reducedArray = cv2.reduce(gap, 0, cv2.REDUCE_MAX, dtype=cv2.CV_8U) rcnt, length, i, left, started = [], len(reducedArray[0]), 0, 0, False while i < length: if reducedArray[0][i] >0: if not started: left = i-1 started = True else: if started: rcnt.append((x+left, y, i-left, h)) # absolute coordination started = False i=i+1 for rc in rcnt: _x, _y, _w, _h = rc if _h//_w>=2 or _w//_h>=3: continue xhigh = _x + _w//2 + _h//2 yhigh = _y + _h//2 + _w//2 xlow = xhigh - _h ylow = yhigh - _w if xlow <0: xlow = 0 xhigh = _h if xhigh > my_w: xhigh = my_w xlow = xhigh - _h if ylow <0: ylow = 0 yhigh = _w if yhigh > my_h: yhigh = my_h ylow = yhigh - _w if xlow >=0 and xhigh<=my_w and ylow >=0 and yhigh<=my_h: glyph = numpy.ascontiguousarray(numpy.rot90(myImage[_y:_y+_h, _x:_x+_w], -1)) cv2.rectangle(myImage, (_x, _y), (_x+_w, _y+_h), (255, 255, 255), -1) newImage[ylow:yhigh, xlow:xhigh] = cv2.min(newImage[ylow:yhigh, xlow:xhigh], glyph) cv2.imwrite(sys.argv[1]+".rotate.png", cv2.min(newImage,myImage)) |
08-06-2024, 01:55 PM | #2064 |
Fuzzball, the purple cat
Posts: 1,283
Karma: 11087488
Join Date: Jun 2011
Location: California
Device: iPad
|
A little context here would be useful. What is your script intended to be used on? A PDF file? Does it work on any PDF?
|
08-06-2024, 01:57 PM | #2065 | |
Fuzzball, the purple cat
Posts: 1,283
Karma: 11087488
Join Date: Jun 2011
Location: California
Device: iPad
|
Quote:
|
|
08-07-2024, 01:06 AM | #2066 | ||
Member
Posts: 11
Karma: 10
Join Date: Apr 2022
Device: none
|
Quote:
Quote:
It binarizes the image, rotates it by 90 degrees counter-clockwise, aligns it horizontally, and filters out noise pixels. Next steps are: Reverse the color. Every white pixel will color adjacent pixels to make sure every radical of a single, square character is connected to one another. Chop the result to disconnected pieces. Rotate 90 degrees clockwise every rectangular piece (but in the original image) that is square enough. My solution works on single characters. It would be better to process text by line. Last edited by pdurrant; 08-11-2024 at 05:18 AM. |
||
08-07-2024, 07:29 PM | #2067 | |
Junior Member
Posts: 4
Karma: 55640
Join Date: Aug 2024
Device: Kindle Paperwhite
|
Quote:
So the attached image is where I'm at currently and what the problem is. It's always something about maybe the headers/footers getting jumbled, or footnotes? I tried the -1 col thing but that didn't work. Is it the margins? |
|
08-09-2024, 03:34 PM | #2068 |
Fuzzball, the purple cat
Posts: 1,283
Karma: 11087488
Join Date: Jun 2011
Location: California
Device: iPad
|
It's very hard to tell what is happening without the source PDF document. Can you post that document, or maybe a couple of pages of it? Or send to me privately? It looks like it has some extra text that is screwing up the re-flow (the "GE Public..." text rotated 90 degrees in your preview image).
|
08-09-2024, 03:54 PM | #2069 | |
Junior Member
Posts: 4
Karma: 55640
Join Date: Aug 2024
Device: Kindle Paperwhite
|
Quote:
Attached the PDF, it's an academic paper. That's what I use this program for and it sometimes gets jumbled if they're in columns too. I appreciate your help! [Removed academic paper that was marked with a recent Copyright notice] Last edited by pdurrant; 08-11-2024 at 05:14 AM. |
|
08-10-2024, 10:51 PM | #2070 | |
Fuzzball, the purple cat
Posts: 1,283
Karma: 11087488
Join Date: Jun 2011
Location: California
Device: iPad
|
Quote:
Conversion #1 See k2pdfopt_window.png and cropregion_window.png attachments. Here I tried to stay as close to your options as possible 1. I selected a crop region to ignore the edges of the page. 2. I selected "Erase horizontal lines" to ignore the divider lines on some of the pages. 3. I de-selected auto-crop. That's really meant to be for poorly scanned books that have photocopying artifacts at the edges of the pages. It doesn't work very well. 4. I set max columns to 1 just so k2pdfopt would not look for multiple columns 5. I put -fc- in the additional options box to suppress a warning message. It's also important that "Autostraighten" is selected. I set the font size to 18 points. I attached sample output to output_sample_18ptfont.pdf. The conversion was also pretty slow--200 s on my PC. See below for how I remedied that. Conversion #2 (k2pdfopt_window_faster.png and output_sample_faster.pdf) Here I made some changes to make the conversion faster and the output file smaller. Rather than setting the output font size, which is slow for a number of reasons (part of it that k2pdfopt has to analyze the file to determine the font size), I just tell k2pdfopt that your kindle window size is much smaller than it actually is. It seems strange, but this has the same effect, and k2pdfopt processes everything at a lower dpi, so it goes much faster and makes a smaller output file. Also, I set the input dpi to 300 dpi since that is the native resolution of the source file. Normally it defaults to twice the output dpi. Finally, I added "-rt 0" so k2pdfopt does not waste time verifying the orientation of the source file. This brought the conversion time down from 200 s to 45 s on my PC. The output file size is ~11 MB. Conversion #3 (k2pdfopt_window_fastest_and_smallest.png and output_sample_fastest_and_smallest.pdf) I added -bpc 1 to the additional options since the source document is just a monochrome (1-bit) document. This gets the output down to ~7 MB from an original size of ~20 MB in conversion #1. Last edited by willus; 08-11-2024 at 10:00 AM. |
|
Tags |
ebook apps, k5 tools, kindle tools, kindle touch, tools |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Viewing PDFs with another font | Font | PocketBook | 4 | 11-12-2010 09:27 AM |
Viewing Textbook PDFs... | NJReader | enTourage Archive | 4 | 08-17-2010 06:17 PM |
PRS-600 Restart bug while viewing PDFs? | conundrum | Sony Reader | 2 | 03-04-2010 09:46 PM |
More on viewing pdfs | dso371 | Bookeen | 8 | 03-11-2008 08:15 PM |
Viewing Untagged PDFs on Palm T|X | Eroica | Reading and Management | 3 | 12-10-2007 02:44 PM |