Skip to content

Commit

Permalink
pHYs change to keep print size
Browse files Browse the repository at this point in the history
  • Loading branch information
Dnyarri committed Mar 30, 2024
1 parent 18e43e8 commit c68a8f4
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
backup/
build/
temp/
testcases/
__pycache__/
Expand Down
9 changes: 4 additions & 5 deletions README.RU.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
- Scale2xGUI.py - использует масштабирование AdvMAME2x, оснащена простым GUI для открытия и сохранения файлов PNG.
- Scale2xCLI.py - тот же алгоритм AdvMAME2x, что и выше, работает из командной строки:
*python Scale2xCLI.py input.png output.png*.
- batchScale2x.py - оптовое масштабирование всех PNG файлов в выбранной директории и поддиректориях с помощью AdvMAME2x. Масштабированные картинки сохраняются в виде копий с добавленным расширением ".2x.png".
- batchScale2x.py - оптовое масштабирование всех PNG файлов в выбранной директории и поддиректориях с помощью AdvMAME2x. *Внимание:* Исходные файлы замещаются обработанными, если хотите сделать копию - делайте это заранее.

- Scale3xGUI.py - использует масштабирование AdvMAME3x, оснащена простым GUI для открытия и сохранения файлов PNG.
- Scale3xCLI.py - тот же алгоритм AdvMAME3x, что и выше, работает из командной строки:
*python Scale3xCLI.py input.png output.png*.
- batchScale2x.py - оптовое масштабирование всех PNG файлов в выбранной директории и поддиректориях с помощью AdvMAME3x. Масштабированные картинки сохраняются в виде копий с добавленным расширением ".3x.png".

*Внимание:* при использовании batch-программ на одной директории несколько раз подряд вы начинаете в прогрессии накапливать копии копий копий в разном масштабе. С каждым проходом программы они размножаются, и только один человек может это остановить.
- batchScale3x.py - оптовое масштабирование всех PNG файлов в выбранной директории и поддиректориях с помощью AdvMAME3x. *Внимание:* Исходные файлы замещаются обработанными, если хотите сделать копию - делайте это заранее.

![Example of Scale3x run twice](https://dnyarri.github.io/imgscalenx/x3x3.png)

Expand All @@ -38,4 +37,4 @@

[github Dnyarri](https://github.com/Dnyarri)

[gitflic Dnyarri](https://gitflic.ru/user/dnyarri)
[gitflic Dnyarri](https://gitflic.ru/user/dnyarri)
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ Apparently useful for scaling up grey text scans with low resolution before OCR,
Currently **Scale2x** (aka **AdvMAME2x**) and **Scale3x** (aka **AdvMAME3x**) are implemented.

- Scale2xGUI.py - uses AdvMAME2x scaling, equipped with simple GUI for opening and saving PNG files.
- Scale2xCLI.py - same AdvMAME2x scaling as above, runs with command line:
- Scale2xGUI.py - uses Scale2x scaling, equipped with simple GUI for opening and saving PNG files.
- Scale2xCLI.py - same Scale2x scaling as above, runs with command line:
*python Scale2xCLI.py input.png output.png*
- batchScale2x.py - batch rescaling of all PNG files within chosen directory, recursively, using AdvMAME2x scaling. Rescaled images are saved as copies with ".2x.png" extension added.
- batchScale2x.py - batch rescaling of all PNG files within chosen directory, recursively, using Scale2x scaling. Source images are replaced, no backup - no mercy.

- Scale3xGUI.py - uses AdvMAME3x scaling, equipped with simple GUI for opening and saving PNG files.
- Scale3xCLI.py - same AdvMAME3x scaling as above, runs with command line:
- Scale3xGUI.py - uses Scale3x scaling, equipped with simple GUI for opening and saving PNG files.
- Scale3xCLI.py - same Scale3x scaling as above, runs with command line:
*python Scale3xCLI.py input.png output.png*
- batchScale3x.py - batch rescaling of all PNG files within chosen directory, recursively, using AdvMAME3x scaling. Rescaled images are saved as copies with ".3x.png" extension added.

Take notice that, by running batchScaleNx programs on the same folder several times, you progressively populate this folder with copies of copies of copies of PNGs with different rescaling. Soon they multiply, and only one man can stop it.
- batchScale3x.py - batch rescaling of all PNG files within chosen directory, recursively, using Scale3x scaling. Source images are replaced, no backup - no mercy.

## Sample of Scale3x output (twice)

Expand All @@ -41,4 +39,4 @@ Related links:

[github Dnyarri](https://github.com/Dnyarri)

[gitflic Dnyarri](https://gitflic.ru/user/dnyarri)
[gitflic Dnyarri](https://gitflic.ru/user/dnyarri)
21 changes: 19 additions & 2 deletions Scale2xCLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
01.002 Changed from self-contained to modular, IncSrc and IncScaleNx modules created
01.003 Ultimate modular evil, moving everything possible to IncSrc.py and IncScaleNx.py
2024.02.24 Cleanup, minimizing import, versioning changed to YYYY.MM.DD
2024.03.30 pHYs chunk editing to keep image print size constant
'''

__author__ = "Ilya Razmanov"
__copyright__ = "(c) 2024 Ilya Razmanov"
__credits__ = "Ilya Razmanov"
__license__ = "unlicense"
__version__ = "2024.02.24"
__version__ = "2024.03.30"
__maintainer__ = "Ilya Razmanov"
__email__ = "[email protected]"
__status__ = "Production"
Expand Down Expand Up @@ -55,13 +56,29 @@
# Reshaping 2x scaled 3D list into 1D list for PyPNG .write_array method
ResultImageAsList = IncSrc.Img3Dto1D(EPXImage, doubleX, doubleY, Z)

# --------------------------------------------------------------
# Fixing resolution to match original print size.
# If no pHYs found in original, 96 ppi is assumed as original value.
if 'physical' in info:
res = info['physical'] # Reading resolution as tuple
x_pixels_per_unit = res[0]
y_pixels_per_unit = res[1]
unit_is_meter = res[2]
else:
x_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
y_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
unit_is_meter = True
x_pixels_per_unit = 2*x_pixels_per_unit # Double resolution to keep print size
y_pixels_per_unit = 2*y_pixels_per_unit # Double resolution to keep print size
# Resolution changed
# --------------------------------------------------------------

# --------------------------------------------------------------
# Open export file

resultPNG = open(Dvo, mode='wb')
# Writing export file
writer = png.Writer(doubleX, doubleY, greyscale = info['greyscale'], alpha = info['alpha'], bitdepth = info['bitdepth'])
writer = png.Writer(doubleX, doubleY, greyscale = info['greyscale'], alpha = info['alpha'], bitdepth = info['bitdepth'], physical = [x_pixels_per_unit, y_pixels_per_unit, unit_is_meter])
writer.write_array(resultPNG, ResultImageAsList)
resultPNG.close()

Expand Down
22 changes: 20 additions & 2 deletions Scale2xGUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
01.003 Ultimate modular evil, moving everything possible to IncSrc.py and IncScaleNx.py
01.004 Progress indication added, showing processing stage
2024.02.24 Cleanup, GUI tweaks, versioning changed to YYYY.MM.DD
2024.03.30 pHYs chunk editing to keep image print size constant
'''

__author__ = "Ilya Razmanov"
__copyright__ = "(c) 2024 Ilya Razmanov"
__credits__ = "Ilya Razmanov"
__license__ = "unlicense"
__version__ = "2024.02.24"
__version__ = "2024.03.30"
__maintainer__ = "Ilya Razmanov"
__email__ = "[email protected]"
__status__ = "Production"
Expand Down Expand Up @@ -117,10 +118,27 @@
# Export file opened
# --------------------------------------------------------------

# --------------------------------------------------------------
# Fixing resolution to match original print size.
# If no pHYs found in original, 96 ppi is assumed as original value.
if 'physical' in info:
res = info['physical'] # Reading resolution as tuple
x_pixels_per_unit = res[0]
y_pixels_per_unit = res[1]
unit_is_meter = res[2]
else:
x_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
y_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
unit_is_meter = True
x_pixels_per_unit = 2*x_pixels_per_unit # Double resolution to keep print size
y_pixels_per_unit = 2*y_pixels_per_unit # Double resolution to keep print size
# Resolution changed
# --------------------------------------------------------------

# --------------------------------------------------------------
# Writing export file

writer = png.Writer(doubleX, doubleY, greyscale = info['greyscale'], alpha = info['alpha'], bitdepth = info['bitdepth'])
writer = png.Writer(doubleX, doubleY, greyscale = info['greyscale'], alpha = info['alpha'], bitdepth = info['bitdepth'], physical = [x_pixels_per_unit, y_pixels_per_unit, unit_is_meter])
writer.write_array(resultPNG, ResultImageAsList)
resultPNG.close()

Expand Down
22 changes: 20 additions & 2 deletions Scale3xCLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
01.002 Changed from self-contained to modular, IncSrc and IncScaleNx modules created
01.003 Ultimate modular evil, moving everything possible to IncSrc.py and IncScaleNx.py
2024.02.24 Cleanup, minimizing import, versioning changed to YYYY.MM.DD
2024.03.30 pHYs chunk editing to keep image print size constant
'''

__author__ = "Ilya Razmanov"
__copyright__ = "(c) 2024 Ilya Razmanov"
__credits__ = "Ilya Razmanov"
__license__ = "unlicense"
__version__ = "2024.02.24"
__version__ = "2024.03.30"
__maintainer__ = "Ilya Razmanov"
__email__ = "[email protected]"
__status__ = "Production"
Expand Down Expand Up @@ -56,11 +57,28 @@
# Reshaping 3x scaled 3D list into 1D list for PyPNG .write_array method
ResultImageAsList = IncSrc.Img3Dto1D(EPXImage, tripleX, tripleY, Z)

# --------------------------------------------------------------
# Fixing resolution to match original print size.
# If no pHYs found in original, 96 ppi is assumed as original value.
if 'physical' in info:
res = info['physical'] # Reading resolution as tuple
x_pixels_per_unit = res[0]
y_pixels_per_unit = res[1]
unit_is_meter = res[2]
else:
x_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
y_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
unit_is_meter = True
x_pixels_per_unit = 3*x_pixels_per_unit # Triple resolution to keep print size
y_pixels_per_unit = 3*y_pixels_per_unit # Triple resolution to keep print size
# Resolution changed
# --------------------------------------------------------------

# --------------------------------------------------------------
# Open export file
resultPNG = open(Dvo, mode='wb')
# Writing export file
writer = png.Writer(tripleX, tripleY, greyscale = info['greyscale'], alpha = info['alpha'], bitdepth = info['bitdepth'])
writer = png.Writer(tripleX, tripleY, greyscale = info['greyscale'], alpha = info['alpha'], bitdepth = info['bitdepth'], physical = [x_pixels_per_unit, y_pixels_per_unit, unit_is_meter])
writer.write_array(resultPNG, ResultImageAsList)
resultPNG.close()
# Export file closed
Expand Down
22 changes: 20 additions & 2 deletions Scale3xGUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
01.003 Ultimate modular evil, moving everything possible to IncSrc.py and IncScaleNx.py
01.004 Progress indication added, showing processing stage
2024.02.24 Cleanup, GUI tweaks, versioning changed to YYYY.MM.DD
2024.03.30 pHYs chunk editing to keep image print size constant
'''

__author__ = "Ilya Razmanov"
__copyright__ = "(c) 2024 Ilya Razmanov"
__credits__ = "Ilya Razmanov"
__license__ = "unlicense"
__version__ = "2024.02.24"
__version__ = "2024.03.30"
__maintainer__ = "Ilya Razmanov"
__email__ = "[email protected]"
__status__ = "Production"
Expand Down Expand Up @@ -117,10 +118,27 @@
# Export file opened
# --------------------------------------------------------------

# --------------------------------------------------------------
# Fixing resolution to match original print size.
# If no pHYs found in original, 96 ppi is assumed as original value.
if 'physical' in info:
res = info['physical'] # Reading resolution as tuple
x_pixels_per_unit = res[0]
y_pixels_per_unit = res[1]
unit_is_meter = res[2]
else:
x_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
y_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
unit_is_meter = True
x_pixels_per_unit = 3*x_pixels_per_unit # Triple resolution to keep print size
y_pixels_per_unit = 3*y_pixels_per_unit # Triple resolution to keep print size
# Resolution changed
# --------------------------------------------------------------

# --------------------------------------------------------------
# Writing export file

writer = png.Writer(tripleX, tripleY, greyscale = info['greyscale'], alpha = info['alpha'], bitdepth = info['bitdepth'])
writer = png.Writer(tripleX, tripleY, greyscale = info['greyscale'], alpha = info['alpha'], bitdepth = info['bitdepth'], physical = [x_pixels_per_unit, y_pixels_per_unit, unit_is_meter])
writer.write_array(resultPNG, ResultImageAsList)
resultPNG.close()

Expand Down
24 changes: 21 additions & 3 deletions batchScale2x.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
01.000 Initial working release
01.001 Progress indication added, showing name of file being processed
2024.02.24 Cleanup, GUI tweaks, versioning changed to YYYY.MM.DD
2024.03.30 pHYs chunk editing to keep image print size constant
'''

__author__ = "Ilya Razmanov"
__copyright__ = "(c) 2024 Ilya Razmanov"
__credits__ = "Ilya Razmanov"
__license__ = "unlicense"
__version__ = "2024.02.24"
__version__ = "2024.03.30"
__maintainer__ = "Ilya Razmanov"
__email__ = "[email protected]"
__status__ = "Production"
Expand Down Expand Up @@ -64,7 +65,7 @@
for runningfilename in glob(sourcedir + "/**/*.png", recursive=True): # select all PNG files in all subfolders

oldfile = runningfilename
newfile = oldfile + '.2x.png' # If you wish originals to be replaced, set newfile = oldfile
newfile = oldfile # Previous version used backup newfile = oldfile + '.2x.png'

zanyato.config(text = oldfile) # Updating label, showing processed file name
sortir.update()
Expand All @@ -85,9 +86,26 @@
# Reshaping 2x scaled 3D list into 1D list for PyPNG .write_array method
ResultImageAsList = IncSrc.Img3Dto1D(EPXImage, newX, newY, Z)

# --------------------------------------------------------------
# Fixing resolution to match original print size.
# If no pHYs found in original, 96 ppi is assumed as original value.
if 'physical' in info:
res = info['physical'] # Reading resolution as tuple
x_pixels_per_unit = res[0]
y_pixels_per_unit = res[1]
unit_is_meter = res[2]
else:
x_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
y_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
unit_is_meter = True
x_pixels_per_unit = 2*x_pixels_per_unit # Double resolution to keep print size
y_pixels_per_unit = 2*y_pixels_per_unit # Double resolution to keep print size
# Resolution changed
# --------------------------------------------------------------

# Writing new image
resultPNG = open(newfile, mode='wb')
writer = png.Writer(newX, newY, greyscale = info['greyscale'], alpha = info['alpha'], bitdepth = info['bitdepth'])
writer = png.Writer(newX, newY, greyscale = info['greyscale'], alpha = info['alpha'], bitdepth = info['bitdepth'], physical = [x_pixels_per_unit, y_pixels_per_unit, unit_is_meter])
writer.write_array(resultPNG, ResultImageAsList)
resultPNG.close()

Expand Down
24 changes: 21 additions & 3 deletions batchScale3x.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
01.000 Initial working release
01.001 Progress indication added, showing name of file being processed
2024.02.24 Cleanup, GUI tweaks, versioning changed to YYYY.MM.DD
2024.03.30 pHYs chunk editing to keep image print size constant
'''

__author__ = "Ilya Razmanov"
__copyright__ = "(c) 2024 Ilya Razmanov"
__credits__ = "Ilya Razmanov"
__license__ = "unlicense"
__version__ = "2024.02.24"
__version__ = "2024.03.30"
__maintainer__ = "Ilya Razmanov"
__email__ = "[email protected]"
__status__ = "Production"
Expand Down Expand Up @@ -64,7 +65,7 @@
for runningfilename in glob(sourcedir + "/**/*.png", recursive=True): # select all PNG files in all subfolders

oldfile = runningfilename
newfile = oldfile + '.3x.png' # If you wish originals to be replaced, set newfile = oldfile
newfile = oldfile # Previous version used backup newfile = oldfile + '.3x.png'

zanyato.config(text = oldfile) # Updating label, showing processed file name
sortir.update()
Expand All @@ -85,9 +86,26 @@
# Reshaping 3x scaled 3D list into 1D list for PyPNG .write_array method
ResultImageAsList = IncSrc.Img3Dto1D(EPXImage, newX, newY, Z)

# --------------------------------------------------------------
# Fixing resolution to match original print size.
# If no pHYs found in original, 96 ppi is assumed as original value.
if 'physical' in info:
res = info['physical'] # Reading resolution as tuple
x_pixels_per_unit = res[0]
y_pixels_per_unit = res[1]
unit_is_meter = res[2]
else:
x_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
y_pixels_per_unit = 3780 # 3780 px/meter = 96 px/inch, 2834 px/meter = 72 px/inch
unit_is_meter = True
x_pixels_per_unit = 3*x_pixels_per_unit # Triple resolution to keep print size
y_pixels_per_unit = 3*y_pixels_per_unit # Triple resolution to keep print size
# Resolution changed
# --------------------------------------------------------------

# Writing new image
resultPNG = open(newfile, mode='wb')
writer = png.Writer(newX, newY, greyscale = info['greyscale'], alpha = info['alpha'], bitdepth = info['bitdepth'])
writer = png.Writer(newX, newY, greyscale = info['greyscale'], alpha = info['alpha'], bitdepth = info['bitdepth'], physical = [x_pixels_per_unit, y_pixels_per_unit, unit_is_meter])
writer.write_array(resultPNG, ResultImageAsList)
resultPNG.close()

Expand Down

0 comments on commit c68a8f4

Please sign in to comment.