Skip to content

Commit

Permalink
ruff check
Browse files Browse the repository at this point in the history
  • Loading branch information
FredHappyface committed Mar 30, 2024
1 parent 8972500 commit d4ae6d5
Show file tree
Hide file tree
Showing 22 changed files with 134 additions and 95 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/test-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Python Test and Lint

on:
push:
branches:
- '*'
pull_request:
branches:
- main

jobs:
test:
name: Python Test and Lint
runs-on: ubuntu-latest

strategy:
matrix:
python-version:
- '3.9'
- '3.10'
- '3.11'
- '3.12'

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
- name: Install dependencies
run: poetry install

- name: Run pytest
run: poetry run pytest

- name: Run ruff
run: poetry run ruff check --output-format=github
continue-on-error: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ temp.png
poetry.lock
package-lock.json
requirements_optional.txt
main/input/_*

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
14 changes: 8 additions & 6 deletions imageedit/effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


def roundCorners(image: Image.Image, radius: int | str) -> Image.Image:
"""Round the corners by a number of pixels. May be preferable to use
"""Round the corners by a number of pixels. May be preferable to use.
roundCornersAntiAlias. Use with caution as it modifies the image param.
radius can be one of the following:
Expand Down Expand Up @@ -168,7 +168,7 @@ def convertBlackAndWhite(image: Image.Image, mode: str = "filter-darker"):


def doConvertBlackAndWhiteFilter(image: Image.Image, mode: str):
"""Low level function
"""Low level function.
Convert an image to black and white based on a filter: filter-darker and
lighter respectively make pixels darker than the average black and pixels
Expand All @@ -189,15 +189,17 @@ def doConvertBlackAndWhiteFilter(image: Image.Image, mode: str):
img.thumbnail((1, 1))
averageColour = img.getpixel((0, 0))
# Default tp "filter-lighter"
threshold = lambda pixel: 0 if pixel > averageColour else 255
def threshold(pixel) -> int:
return 0 if pixel > averageColour else 255
if mode == "filter-darker":
threshold = lambda pixel: 0 if pixel < averageColour else 255
def threshold(pixel) -> int:
return 0 if pixel < averageColour else 255
converted = image.convert("L").point(threshold, mode="1")
return converted.convert("RGBA")


def doConvertBlackAndWhiteBGFG(image, mode):
"""Low level function
"""Low level function.
Convert an image to black and white based on the foreground/ background:
background sets the most dominant colour to white and foreground sets the
Expand Down Expand Up @@ -228,7 +230,7 @@ def doConvertBlackAndWhiteBGFG(image, mode):
def addText(image: Image.Image, text: str) -> Image.Image:
"""Add text to an image such that the resultant image is in the form [img]|text.
The text is in fira code and has a maximum length of 16 chars
(text longer than this is truncated with "...")
(text longer than this is truncated with "...").
Args:
----
Expand Down
4 changes: 2 additions & 2 deletions imageedit/imagegrab.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Author FredHappyface 2019-2022
"""Author FredHappyface.
Uses playwright to leverage a headless version of Chromium
"""
Expand All @@ -13,7 +13,7 @@


def grabWebpage(url: str, resolution: tuple[int, int] = (800, 600), evalJs=None):
"""Take a screenshot of a webpage
"""Take a screenshot of a webpage.
Args:
----
Expand Down
2 changes: 1 addition & 1 deletion imageedit/imagetracer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Do a trace of an image on the filesystem using the svgtrace library"""
"""Do a trace of an image on the filesystem using the svgtrace library."""

from __future__ import annotations

Expand Down
19 changes: 6 additions & 13 deletions imageedit/io.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Author FredHappyface 2019-2022.
"""Author FredHappyface.
Lib containing various image editing operations
"""
Expand Down Expand Up @@ -57,10 +57,7 @@ def getPixelDimens(image: Image.Image, dimens: list[int | str]) -> list[int]:
if isinstance(dimension, int):
outDimens.append(dimension)
elif isinstance(dimension, str):
if len(dimens) == 1:
size = min(image.size)
else:
size = image.size[index]
size = min(image.size) if len(dimens) == 1 else image.size[index]
if dimension[-1] == "%":
outDimens.append(int(size * int(dimension[:-1]) / 100))
if dimension[-1] == "x":
Expand Down Expand Up @@ -104,14 +101,10 @@ def openImage(file: str, mode: str | None = None) -> Image.Image:
"""
checkExists(file)
if mode is not None:
image = reduceColours(Image.open(file), mode)
else:
image = Image.open(file)
return image
return reduceColours(Image.open(file), mode) if mode is not None else Image.open(file)


def saveImage(fileName, image):
def saveImage(fileName, image) -> None:
"""Save a single image.
Use full file path or file path relative to /lib. Pass in the image object
Expand Down Expand Up @@ -204,15 +197,15 @@ def combine(
)


def checkExists(file):
def checkExists(file) -> None:
"""Throw an error and abort if the path does not exist."""
if not os.path.exists(file):
print(f"ERROR: {file} does not exist")
sys.exit(1)


def getContrastRatio(image: Image.Image) -> float:
"""Get the contrast ratio of an image"""
"""Get the contrast ratio of an image."""
grayImage = ImageOps.grayscale(image)
stats = ImageStat.Stat(grayImage)
low, high = stats.extrema[0]
Expand Down
8 changes: 4 additions & 4 deletions imageedit/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def cropCentre(image: Image.Image, width: int | str, height: int | str) -> Image

def expand(image: Image.Image, padding: int | str) -> Image.Image:
"""Uncrops the image with a padding. padding can be one of the following:
pixel: int, percent: "val%", scale: "valx"
pixel: int, percent: "val%", scale: "valx".
Args:
----
Expand Down Expand Up @@ -76,7 +76,7 @@ def resize(image: Image.Image, width: int | str, height: int | str) -> Image.Ima
"""Resize an image with desired dimensions. This is most suitable for resizing non
square images where a factor would not be sufficient.
width, height can be one of the following:
pixel: int, percent: "val%", scale: "valx"
pixel: int, percent: "val%", scale: "valx".
Args:
----
Expand All @@ -97,7 +97,7 @@ def resizeSquare(image: Image.Image, size: int | str) -> Image.Image:
"""Resize a square image. Or make a non square image square (will stretch if
input image is non-square)
size can be one of the following:
pixel: int, percent: "val%", scale: "valx"
pixel: int, percent: "val%", scale: "valx".
Args:
----
Expand Down Expand Up @@ -153,7 +153,7 @@ def findAndReplace(
"""

def cmpTup(tupleA, tupleB):
def cmpTup(tupleA, tupleB) -> bool:
for index, _ in enumerate(tupleA):
if (
tupleA[index] > tupleB[index] + threshold
Expand Down
2 changes: 1 addition & 1 deletion main/get_pwa_screenshots.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Author FredHappyface 2019-2022
Author FredHappyface.
Grab some screenshots for my pwas - obviously, you can set these to your own
urls and set your own scripts
Expand Down
10 changes: 5 additions & 5 deletions main/make_phone_screenshots.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Author FredHappyface 2019-2022
"""Author FredHappyface.
Make Android screenshots look nice and create a cover image for google play store
"""
Expand All @@ -18,7 +18,7 @@

if __name__ == "__main__": # pragma: no cover
OUTPUT_DIR = f"{THISDIR}/output/screenshots/"
OVERLAY = io.openImage(f"{THISDIR}/resources/pixel3aScreenshot.png")
OVERLAY = io.openImage(f"{THISDIR}/resources/pixel6Screenshot.png")

# Create cover image/ featureGraphic
coverImages = io.openImagesInDir(f"{THISDIR}/input/*-playstore")
Expand Down Expand Up @@ -48,13 +48,13 @@
Image.new(
"RGBA",
(OVERLAY.width, OVERLAY.height),
"#ef5350", # screenshot.getpixel((50, 100))
"#49a0a0", # screenshot.getpixel((50, 100))
),
),
Layer(
"screenshot",
effects.resize(screenshot, "1x", "1x"),
offsets=(540, 720),
effects.resize(screenshot, 750-80, 1678-190),
offsets=(80, 190),
),
Layer("overlay", OVERLAY),
]
Expand Down
2 changes: 1 addition & 1 deletion main/make_proj_icons.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Author FredHappyface 2019-2022
"""Author FredHappyface.
Make GitHub project icons, does a bit more than round.py. At the moment, I quite
fancy using images in a similar style to those used in the google play store.
Expand Down
4 changes: 2 additions & 2 deletions main/make_pwa_images.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Author FredHappyface 2019-2022
Make Images for PWAs
Author FredHappyface
Make Images for PWAs.
"""

from __future__ import annotations
Expand Down
2 changes: 1 addition & 1 deletion main/make_retro.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Author FredHappyface 2019-2022
"""Author FredHappyface.
Make Images for PWAs
"""
Expand Down
2 changes: 1 addition & 1 deletion main/read_write_layered.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Read and write to an .ora image"""
"""Read and write to an .ora image."""

from __future__ import annotations

Expand Down
Binary file removed main/resources/pixel3aScreenshot.png
Binary file not shown.
Binary file removed main/resources/pixel3aScreenshot.xcf
Binary file not shown.
Binary file added main/resources/pixel6Screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added main/resources/pixel6Screenshot.xcf
Binary file not shown.
9 changes: 3 additions & 6 deletions main/round.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Author FredHappyface 2019-2022
Round the corners of an image
Author FredHappyface
Round the corners of an image.
"""

from __future__ import annotations
Expand Down Expand Up @@ -57,8 +57,5 @@
else:
im = effects.roundCorners(im, int(im.width / 2))

if args.output is not None:
outFileName = args.output
else:
outFileName = args.image
outFileName = args.output if args.output is not None else args.image
io.saveImage(f"{THISDIR}/" + outFileName, im)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ ignore = [
"ISC001",
"N", # pep8 naming
"PLR09", # pylint refactor too many
"T201", # print
"TCH", # type check blocks
"W191" # ignore this to allow tabs
]
Expand Down
Loading

0 comments on commit d4ae6d5

Please sign in to comment.