Skip to content

Update fetch() for portrait oriented images #60

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

Merged
merged 5 commits into from
Jan 22, 2020
Merged

Conversation

cogliano
Copy link
Contributor

@cogliano cogliano commented Jan 6, 2020

This code was added to allow multiple fetch() commands to work correctly when there is a mix of portrait and landscape oriented images to display on the PyPortal. An additional parameter, image_dim_json_path, was added to init() to point to the JSON section that contains the original width and height of the image. If this parameter (a width and height tuple) is not present, the fetch should work as before with no portrait mode considerations.

Here is an example of the feature being used. I commented out some of the lines so the code could run without external file dependencies. Specific landscape and portrait itemids can be uncommented for testing, or use the randomint() function.

More info on the api used in the example is here: https://openaccess-api.clevelandart.org/

import time
import board
import random
from adafruit_pyportal import PyPortal
from adafruit_display_shapes.circle import Circle

WIDTH = board.DISPLAY.width
HEIGHT = board.DISPLAY.height

apiurl = "https://openaccess-api.clevelandart.org/api/artworks?cc0=1&has_image=1&indent=2&limit=1&skip="

pyportal = PyPortal(
                    # default_bg=background_file,
                    image_json_path=["data",0,"images","web","url"],
                    image_dim_json_path=(["data",0,"images","web","width"],["data",0,"images","web","height"]),
                    image_resize=(WIDTH, HEIGHT - 15),
                    image_position=(0, 0),
                    #text_font="/fonts/OpenSans-9.bdf",
                    #json_path=["data",0,"title"],
                    #text_position=(4,HEIGHT - 9),
                    #text_color =0xFFFFFF
                    )

circle = Circle(312, 233, 5, fill=0)
pyportal.splash.append(circle)
loopcount = 0
errorcount = 0
while True:
    response = None
    try:
        circle.fill = 0xFF0000
        itemid = random.randint(1, 30000)
        # itemid = 20 # portrait mode example
        # itemid = 21 # landscape mode example
        print("retrieving url:", apiurl +str(itemid))
        response = pyportal.fetch(apiurl + str(itemid))
        circle.fill = 0
        print("Response is", response)
        loopcount = loopcount + 1

    except (RuntimeError, KeyError) as e:
        print("An error occured, retrying! -", e)
        print("loop counter:", loopcount)
        assert errorcount < 20, "Too many errors, stopping"
        errorcount = errorcount + 1
        continue

    errorcount = 0
    stamp = time.monotonic()
    # wait 5 minutes before getting again
    while (time.monotonic() - stamp) < (5*60):
        # or, if they touch the screen, fetch immediately!
        if pyportal.touchscreen.touch_point:
            break

@cogliano cogliano marked this pull request as ready for review January 6, 2020 20:02
@kattni kattni requested a review from a team January 8, 2020 19:28
Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to edit the given test code a bit to wait for a minute between retries. Sometimes it would succeed to send the image to adafruit io server, but fail to fully download it. The io server rate limit stopped the next several retries from succeeding.

The change looks good to me. With the 4.1.2 adafruit_pyportal.mpy file the portrait image from the sample code gets cropped in to the center of the original painting:
412mpy

With the edited pyportal library, but without the new parameter the image looks the same, so there is no change for anyone that doesn't explicitly use the new parameter.

With the edited library and the new parameter included the image gets scaled to fit within the height of the pyportal screen:
edited_lib_with_params

I also tested the landscape image from the example code, the results were consistent between the 4.12 mpy library, the edited library with the new parameter, and the edited library without the new parameter.

I don't have much experience with pylint, so I am unsure about the effect of that change.

@kattni
Copy link
Contributor

kattni commented Jan 22, 2020

Thanks for the update, @cogliano! Thank you for testing this, @FoamyGuy!

@kattni kattni merged commit d14e53a into adafruit:master Jan 22, 2020
@cogliano
Copy link
Contributor Author

Thank you @kattni and @FoamyGuy. This feature will be included in an upcoming learning guide I am working on.

@kattni
Copy link
Contributor

kattni commented Jan 22, 2020

@cogliano Thanks for letting me know. I did a release so your update will be included in the version in the library bundle. It will go in when the bundle builds next, likely today.

adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jan 23, 2020
Updating https://github.com/adafruit/Adafruit_CircuitPython_AM2320 to 1.1.4 from 1.1.3:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_AM2320#9 from Ferroin/master
  > Merge pull request adafruit/Adafruit_CircuitPython_AM2320#11 from adafruit/dherrada-patch-1
  > Update README.rst
  > Merge pull request adafruit/Adafruit_CircuitPython_AM2320#10 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_DS1307 to 2.0.2 from 2.0.1:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_DS1307#15 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_DS1307#14 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_DS3231 to 2.2.2 from 2.2.1:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_DS3231#21 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_FocalTouch to 1.1.7 from 1.1.6:
  > update pylint examples directive

Updating https://github.com/adafruit/Adafruit_CircuitPython_FRAM to 1.2.5 from 1.2.4:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_FRAM#14 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_FRAM#11 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_FXAS21002C to 2.0.2 from 2.0.1:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_FXAS21002C#12 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_FXAS21002C#11 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_FXOS8700 to 2.0.2 from 2.0.1:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_FXOS8700#15 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_FXOS8700#14 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_HTU21D to 0.9.1 from 0.9.0:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_HTU21D#7 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_HTU21D#6 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_INA260 to 1.1.4 from 1.1.3:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_INA260#8 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_INA260#7 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_IS31FL3731 to 2.5.3 from 2.5.2:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_IS31FL3731#21 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_IS31FL3731#20 from makermelissa/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_LSM9DS0 to 2.1.3 from 2.1.2:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_LSM9DS0#16 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_LSM9DS0#15 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_LSM9DS1 to 2.0.6 from 2.0.5:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_LSM9DS1#18 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_MatrixKeypad to 1.1.5 from 1.1.4:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_MatrixKeypad#8 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_MatrixKeypad#7 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_MAX7219 to 1.2.3 from 1.2.2:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_MAX7219#23 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_MCP4725 to 1.2.1 from 1.2.0:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_MCP4725#11 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_MCP4725#10 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_MCP9808 to 3.2.4 from 3.2.3:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_MCP9808#20 from signalmeister/signalmeister-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_MCP9808#22 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_MCP9808#21 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_MLX90393 to 1.3.2 from 1.3.1:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_MLX90393#13 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_MLX90393#12 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_MMA8451 to 1.2.5 from 1.2.4:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_MMA8451#8 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_MMA8451#7 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_MPL3115A2 to 1.1.6 from 1.1.5:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_MPL3115A2#10 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_MPL3115A2#9 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_PCD8544 to 1.1.2 from 1.1.1:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_PCD8544#9 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_PCF8523 to 1.3.1 from 1.3.0:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_PCF8523#14 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_PCF8523#13 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_Pixie to 1.1.6 from 1.1.5:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_Pixie#14 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_Pixie#13 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_PyPortal to 3.1.6 from 3.1.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_PyPortal#60 from cogliano/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_RA8875 to 3.0.1 from 3.0.0:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_RA8875#17 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_RFM69 to 1.3.1 from 1.3.0:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_RFM69#21 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_SD to 3.2.6 from 3.2.5:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_SD#30 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_SI4713 to 1.1.5 from 1.1.4:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_SI4713#11 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_SI4713#10 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_SI5351 to 1.1.5 from 1.1.4:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_SI5351#10 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_SI5351#9 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_SI7021 to 3.1.5 from 3.1.4:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_SI7021#15 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_SI7021#14 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_TCA9548A to 0.2.2 from 0.2.1:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_TCA9548A#14 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_TCA9548A#13 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_TCS34725 to 3.2.4 from 3.2.3:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_TCS34725#25 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_TLC5947 to 1.2.4 from 1.2.3:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_TLC5947#18 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_TLC5947#17 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_TMP007 to 2.0.1 from 2.0.0:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_TMP007#7 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_TMP007#6 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_TSL2561 to 3.2.3 from 3.2.2:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_TSL2561#28 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_TSL2561#27 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_VC0706 to 4.0.4 from 4.0.3:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_VC0706#11 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_VC0706#10 from kattni/update-example-pi
  > Merge pull request adafruit/Adafruit_CircuitPython_VC0706#9 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_VL53L0X to 3.1.7 from 3.1.6:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_VL53L0X#13 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_VL6180X to 1.1.5 from 1.1.4:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_VL6180X#10 from adafruit/dherrada-patch-1
  > Merge pull request adafruit/Adafruit_CircuitPython_VL6180X#9 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_OneWire to 1.1.4 from 1.1.3:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_OneWire#17 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_RTTTL to 2.3.2 from 2.3.1:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_RTTTL#18 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_Waveform to 1.2.6 from 1.2.5:
  > update pylint examples directive
  > Merge pull request adafruit/Adafruit_CircuitPython_Waveform#15 from adafruit/dherrada-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Added the following libraries: Adafruit_CircuitPython_BLE_Apple_Media, Adafruit_CircuitPython_NTP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants