Skip to content

Instantly share code, notes, and snippets.

@Korto19
Last active March 18, 2022 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Korto19/d8474e0284b6de9d78b5b4eab3ce1aa4 to your computer and use it in GitHub Desktop.
Save Korto19/d8474e0284b6de9d78b5b4eab3ce1aa4 to your computer and use it in GitHub Desktop.
A QGIS field calculator expression with two custom expressions to get the size of images stored as blobs in DB or from links
from qgis.core import *
from qgis.gui import *
from PyQt5.QtGui import QImage, QImageReader
@qgsfunction(args='auto', group='Custom', referenced_columns=[])
def get_blob_sizes(img_blob, feature, parent):
"""
Calculate blob image dimension (W x H)
<h2>Example usage:</h2>
<ul>
<li>get_blob_sizes("img_blob") -> '1024 x 512 px'</li>
</ul>
"""
image = QImage().fromData(img_blob)
sizeOfImage = image.size()
img_height = sizeOfImage.height()
img_width = sizeOfImage.width()
dim_blob = str(img_width)+ ' x ' + str(img_height) + ' px '
return dim_blob
@qgsfunction(args='auto', group='Custom', referenced_columns=[])
def get_image_sizes(img_shape, feature, parent):
"""
Calculate blob image size
<h2>Example usage:</h2>
<ul>
<li>get_image_sizes(img_shape) -> '1024 x 512 px'</li>
</ul>
NEED ABSOLUTE IMAGE LINK
"""
reader = QImageReader(img_shape)
sizeOfImage = reader.size()
img_height = sizeOfImage.height()
img_width = sizeOfImage.width()
dim_image = str(img_width)+ ' x ' + str(img_height) + ' px '
return dim_image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment