pytroll / tutorial-satpy-half-day

Jupyter Notebook-based tutorial providing an overview of Satpy's features lasting about 4 hours
Other
55 stars 20 forks source link

Satpy font error #6

Closed gersmit closed 3 years ago

gersmit commented 3 years ago

Dear @djhoese , I thought I was there, had the hard part but unfortunately not. You already close the other issue so i hope it is no problem to ask you how to write text on a easy way into the image. As i say, i works fine but..... I have a python routine that put text into an image. But i think with satpy is that also possible. I have this and is working good until i set "write_text : False" to True, then ik get different errors

import aggdraw from aggdraw import Draw, Brush, Pen, Font

font = aggdraw.Font('blue','/home/pi/eumetview/arial.ttf', size = 12)

                 # Font takes only 4 arguments

local_scn.save_dataset('overview', data_img_warmte + 'test.png', overlay={'coast_dir': '/home/pi/eumetview/GSHHS_DATA_ROOT/', "overlays": { "coasts": { "outline": (255, 255, 0), "width": 1.5, "level": 1, "resolution": "f"}, 'grid': { 'major_lonlat': (10, 10), 'write_text': False, 'outline': (224, 224, 224), 'width': 0.5, "lakes": { "outline": (255, 0, 0), "width": 1.5, "level": 1, "resolution": "f"}, 'text': {'txt': 'start_time_txt', 'align': {'top_bottom': 'bottom', 'left_right': 'right'}, 'font':'/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf', 'outline': (224, 224, 224), 'font_size': 12, 'height': 30, 'bg': 'black', 'bg_opacity': 255, 'line': 'white'}, "borders": { "outline": (0, 0, 0), "width": 1.5, "level": 3, "resolution": "f"}}}})

Traceback (most recent call last): File "/home/pi/eumetview/maak_sneeuw_kaart.py", line 275, in "resolution": "f"}}}}) File "/home/pi/.local/lib/python3.7/site-packages/satpy/scene.py", line 1314, in save_dataset compute=compute, save_kwargs) File "/home/pi/.local/lib/python3.7/site-packages/satpy/writers/init.py", line 829, in save_dataset return self.save_image(img, filename=filename, compute=compute, fill_value=fill_value, kwargs) File "/home/pi/.local/lib/python3.7/site-packages/satpy/writers/simple_image.py", line 65, in save_image return img.save(filename, compute=compute, kwargs) File "/home/pi/.local/lib/python3.7/site-packages/trollimage/xrimage.py", line 423, in save compute=compute, format_kwargs) File "/home/pi/.local/lib/python3.7/site-packages/trollimage/xrimage.py", line 617, in pil_save return delay.compute() File "/home/pi/.local/lib/python3.7/site-packages/dask/base.py", line 167, in compute (result,) = compute(self, traverse=False, kwargs) File "/home/pi/.local/lib/python3.7/site-packages/dask/base.py", line 452, in compute results = schedule(dsk, keys, kwargs) File "/home/pi/.local/lib/python3.7/site-packages/dask/threaded.py", line 84, in get kwargs File "/home/pi/.local/lib/python3.7/site-packages/dask/local.py", line 486, in get_async raise_exception(exc, tb) File "/home/pi/.local/lib/python3.7/site-packages/dask/local.py", line 316, in reraise raise exc File "/home/pi/.local/lib/python3.7/site-packages/dask/local.py", line 222, in execute_task result = _execute_task(task, data) File "/home/pi/.local/lib/python3.7/site-packages/dask/core.py", line 121, in _execute_task return func((_execute_task(a, cache) for a in args)) File "/home/pi/.local/lib/python3.7/site-packages/trollimage/xrimage.py", line 643, in _delayed_apply_pil new_img = fun(pil_image, image_metadata, fun_args, fun_kwargs) File "/home/pi/.local/lib/python3.7/site-packages/satpy/writers/init.py", line 183, in _burnoverlay cw.add_overlay_from_dict(overlays, area, background=img) File "/home/pi/.local/lib/python3.7/site-packages/pycoast/cw_base.py", line 891, in add_overlay_from_dict lat_placement=lat_placement) File "/home/pi/.local/lib/python3.7/site-packages/pycoast/cw_agg.py", line 364, in add_grid lat_placement=lat_placement) File "/home/pi/.local/lib/python3.7/site-packages/pycoast/cw_base.py", line 383, in _add_grid txt, font, **kwargs) File "/home/pi/.local/lib/python3.7/site-packages/pycoast/cw_base.py", line 123, in _draw_grid_labels font = self._get_font(kwargs.get('outline', 'black'), font, 12) File "/home/pi/.local/lib/python3.7/site-packages/pycoast/cw_agg.py", line 648, in _get_font return aggdraw.Font(outline, font_file, size=font_size) TypeError: Font() argument 2 must be str, not None

When i use font=aggdraw.Font('blue', 'home/pi/eumetview/arial.ttf, size=12) Error == Traceback (most recent call last): File "/home/pi/eumetview/maak_sneeuw_kaart.py", line 242, in font = aggdraw.Font('blue','/home/pi/eumetview/arial.ttf', size = 12) OSError: cannot load font (no text renderer)

Where am I making the mistake, thanks for helping me

djhoese commented 3 years ago

Try removing font_size and instead create a font object like this:

import aggdraw
font =  aggdraw.Font('black', '/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf', size=12)

and do:

...
'font': font,
...

In your parameters.

gersmit commented 3 years ago

I no what you mean but when python runs the row: font = aggdraw.Font('black', '/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf', size=12) I immediately get the error File "/home/pi/eumetview/maak_sneeuw_kaart.py", line 242, in font = aggdraw.Font('blue','/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf', size=12) OSError: cannot load font (no text renderer)

Before it start the routine overlay= I removed aggdraw as someone say (install it again) and try other solutions.

djhoese commented 3 years ago

Does that font actually exist on your system? Meaning does the file /usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf exist?

gersmit commented 3 years ago

dejavu

djhoese commented 3 years ago

How did you install aggdraw? With pip? You could try making sure that your system has the "freetype" library (include the dev version) installed. Maybe that's the issue (based on other issues we've had with this in the past with aggdraw).

I'm currently rereading: https://github.com/ejeschke/ginga/issues/664

gersmit commented 3 years ago

Yes, i read sommething about that. I read on StackOverflow that freetype give problem with aggdraw? Do you have instruction for me how to install freetype, And do i have to use this also? -> https://github.com/pytroll/aggdraw

djhoese commented 3 years ago

I believe freetype is used to read the .ttf fonts. If it isn't installed then aggdraw can't use it. You already have aggdraw installed as it is a requirement of pycoast. How old was the stackoverflow question? Pytroll (me) has revived the project and made some big improvements for finding freetype.

Installing freetype can either be done by install freetype-py (the python package) or using your system's package manager? It is different for each operating system.

gersmit commented 3 years ago

Pip3 list show this: pycoast 1.4.0, freetype-py 2.2.0, aggdraw 1.3.12 Only pytroll is not in the list, is that the problem?

I think i am going to remove and make an new install from the modules.

djhoese commented 3 years ago

"pytroll" is not a package that gets installed. "pytroll" is the community and "suite" of tools. You may be thinking of "satpy"?

Uninstalling/reinstalling aggdraw and freetype-py may be useful. I'm sorry, but I'm not sure how to help beyond this as this seems to be an issue specific to your system right now.

gersmit commented 3 years ago

@djhoese, thanks, Problem and i wil use other RPI to test. Also i try wit pil and pillow but get still aggdraw error. Can you tell me what error message under means, perhaps we can find solution there:

80, in add_points font = self._get_font(outline, font_file, font_size) File "/home/pi/.local/lib/python3.7/site-packages/pycoast/cw_agg.py", line 648, in _get_font return aggdraw.Font(outline, font_file, size=font_size) TypeError: Font() argument 2 must be str, not FreeTypeFont

djhoese commented 3 years ago

Also i try wit pil and pillow but get still aggdraw error.

What do you mean by this? How did you change your code?

The error message you are receiving is because pycoast mixing up its fonts and passing a Pillow-style FreeTypeFont object, but the AGGContourWriter wants a string (the path to a font).

Also, I forgot you were on a raspberry pi. You may want to install freetype using your system's package manager rather than from freetype-py. If you are on a debian/ubuntu-style system you may be able to do something like "sudo apt-get install libfreetype6" and then reinstall aggdraw.

gersmit commented 3 years ago

It remains a problem and after trying everything and searching with Google I find out that it is not only my problem. I read several issues with your name that it is a problem for many. I thought I found something: https://github.com/pytroll/pycoast/issues/43, but these solutions also lead to nothing. The main thing is Aggdraw which gives the problems. return aggdraw.Font(outline, font_file, size=font_size) TypeError: Font() argument 2 must be str, not None

Last question: no workaround has been developed that solves the problem. Incidentally, the border - coast and other lines work perfectly, only the control font crashes.

djhoese commented 3 years ago

@gersmit This error you pasted:

TypeError: Font() argument 2 must be str, not None

Is fixed by passing the aggdraw Font object as I described originally. The error you got after that about OSError: cannot load font (no text renderer) is a different issue and something to do with your installation as far as I can tell. Do you have another machine you can test on that isn't a rpi?

gersmit commented 3 years ago

Yes, i wil try on other RPI but i better going to see what Windows do. 👍

gersmit commented 3 years ago

@djhoese,

I think solved problem. Find aggdraw special for python3. I think no more problems for the users with the same problem. aggdraw

djhoese commented 3 years ago

Glad you got it working. I'm not sure I understand what you did to fix it, but still glad it works.

I'm going to close this issue now. Feel free to file bug reports on the specific packages you are using (satpy, pycoast, etc) if you run into future issues. That way the people who work on those packages will see the issues rather than just me.

gersmit commented 3 years ago

https://debian.pkgs.org/10/debian-main-arm64/python3-aggdraw_1.3.9+ds-2_arm64.deb.html as solution

gersmit commented 3 years ago

@djhoese hi, I have a question about making parts of a scene image transparent. Is it possible to add transparency if, for example, the given temperature is higher or lower than, say, 240 Kelvin? I then want to keep the pixels that are 240 degrees and remove the rest, make them transparent.

Thanks in advance for an answer

djhoese commented 3 years ago

@gersmit In the future please create a new separate issue for any questions you have. You should also make your questions on the Satpy repository (https://github.com/pytroll/satpy) if they aren't specific to something in this tutorial. That way other Satpy developers can see and answer your question.

As for your question, you'll probably want to look at using the MaskingCompositor: https://satpy.readthedocs.io/en/stable/api/satpy.composites.html?highlight=MaskingCompositor#satpy.composites.MaskingCompositor

gersmit commented 3 years ago

Sorry about this, I'll keep an eye out for it and thanks for the answer