bzamecnik / halftone

Halftoning in Python.
MIT License
4 stars 0 forks source link

Errors during run #1

Open Rel-Oiv opened 2 years ago

Rel-Oiv commented 2 years ago

Hello,

I ran the code that is present in the githup description and it throws an error:

Traceback (most recent call last):
  File "H:\2.Stuff\Projects\Script to execute image combination\steicPythonScript\venv\testHalftone.py", line 6, in <module>
    halftoned = ht.halftone(img, ht.euclid_dot(spacing=8, angle=30))
  File "H:\2.Stuff\Projects\Script to execute image combination\steicPythonScript\venv\lib\site-packages\halftone\__init__.py", line 13, in halftone
    halftoned = img > evaluate_2d_func(img.shape, spot_fn)
  File "H:\2.Stuff\Projects\Script to execute image combination\steicPythonScript\venv\lib\site-packages\halftone\__init__.py", line 85, in evaluate_2d_func
    w, h = img_shape
ValueError: too many values to unpack (expected 2)

(debugger shows that img_shape tuplet has 3 items)

I added a variable in the function def evaluate_2d_func(img_shape, fn): after h variable in w, h = img_shape, to have 3 variables out of the tuplet, so it wouldn't throw an error.

But know throws a different error:

Traceback (most recent call last):
  File "H:\2.Stuff\Projects\Script to execute image combination\steicPythonScript\venv\testHalftone.py", line 6, in <module>
    halftoned = ht.halftone(img, ht.euclid_dot(spacing=8, angle=30))
  File "H:\2.Stuff\Projects\Script to execute image combination\steicPythonScript\venv\lib\site-packages\halftone\__init__.py", line 13, in halftone
    halftoned = img > evaluate_2d_func(img.shape, spot_fn)
ValueError: operands could not be broadcast together with shapes (148,148,4) (148,148) 
Rel-Oiv commented 2 years ago

I found out what caused the errors. I was using an image with RGBA color mode . To not give the error, the image color mode must be "gray". I used the function convert( 'L' ) to transform the image to "gray" ('L' is 8-bit pixels, black and white, as per pillow documentation), form pillow library (this halftone library already uses it). Bear in mind, function convert( 'L' ) transform the alpha (transparency) into black color.

For who is interested, here is the code of the code from readme file with the addition of convert( 'L' ):

import halftone as ht
import PIL.Image

inputImage = 'ht-test.png'  # Path to the image that will be halftoned
outputImage = 'bar.png'     # Where the image will be exported as a file

img = PIL.Image.open(inputImage)
img = img.convert('L')
halftoned = ht.halftone(img, ht.euclid_dot(spacing=8, angle=30))

halftoned.save(outputImage)