Img

Creating and working with images

The mash.img library allows you to generate and manipulate image files.

mash.img wraps PHP ImageMagick. You can read more about the package here.


Reading image files

read

mash.img.read(File: file, number: ?x, number: ?y) -> ImagickWrapper

Read an image file.

Parameters

file

A MaSH File object containing an image.

x (optional)

The x resolution of the image

y (optional)

The y resolution of the image

Natural

set file to mash.file(138, "path/to/test.png")

set image to mash.img.read(file)

printline image

Standard

file = mash.file(138, "path/to/test.png")

image = mash.img.read(file)

printline(image)

Output

ImagickWrapper {
    "mimetype": "image/png",
    "format": "PNG (Portable Network Graphics)",
    "units": "PixelsPerCentimeter",
    "colorSpace": "sRGB",
    "type": "TrueColorAlpha",
    "compression": "Zip",
    "fileSize": "117213B",
    "geometry": {
        "width": 2202,
        "height": 1702
    },
    "resolution": {
        "x": 28.34,
        "y": 28.34
    },
    "signature": "bfd0fe5b90a4d00a839171329db87d4e2414974b904feec31f48dac4845f242f"
}

Drawing on an image

draw

mash.img.draw() -> ImagickDraw

Create an ImageMagick draw object

Natural

# Some text to draw on an image
set text to "Hello from mash"

set folder to mash.file(138, "path/to/images")

set file to folder.get("test.png")

set image to mash.img.read(file)

# Create a draw object
set draw to mash.img.draw()

# Set the properties
draw.setFillColor("black");
draw.setFont("Helvetica");
draw.setFontSize(50);
draw.setGravity(mash.img.gravity_center)

# Annotate the image
image.annotateImage(draw, 0, 0, 0, text)

# Save the new image
folder.write("annotated.png", image)

Standard

# Some text to draw on an image
text = "Hello from mash"

folder = mash.file(138, "path/to/images")

file = folder.get("test.png")

image = mash.img.read(file)

# Create a draw object
draw = mash.img.draw()

# Set the properties
draw.setFillColor("black");
draw.setFont("Helvetica");
draw.setFontSize(50);
draw.setGravity(mash.img.gravity_center)

# Annotate the image
image.annotateImage(draw, 0, 0, 0, text)

# Save the new image
folder.write("annotated.png", image)

Reading Exif Data

readExif

mash.img.readExif(File: file) -> Dictionary

Read the EXIF data from an image file.

Parameters

file

A MaSH File object containing an image.

Natural

set file to mash.file(138, "path/to/test.jpg")

set exif to mash.img.readExif(file)

printline exif

Standard

file = mash.file(138, "path/to/test.jpg")

exif = mash.img.readExif(file)

printline(exif)

Output

Dictionary {
    "FileDateTime": 0,
    "FileSize": 128037,
    "FileType": 2,
    "MimeType": "image/jpeg",
    "SectionsFound": "ANY_TAG, IFD0, THUMBNAIL, EXIF, INTEROP, MAKERNOTE",
    "COMPUTED": {
        "html": "width=\"640\" height=\"480\"",
        "Height": 480,
        "Width": 640,
        "IsColor": 1,
        "ByteOrderMotorola": 0,
        "CCDWidth": "5mm",
        "ApertureFNumber": "f/4.0",
        "FocusDistance": "3.75m",
        "UserComment": null,
        "UserCommentEncoding": "UNDEFINED",
        "Thumbnail.FileType": 2,
        "Thumbnail.MimeType": "image/jpeg"
    },
    "Make": "Canon",
    "Model": "Canon DIGITAL IXUS",
    "Orientation": 1,
    "XResolution": "180/1",
    "YResolution": "180/1",
    "ResolutionUnit": 2,
    "DateTime": "2001:06:09 15:17:32",
    "YCbCrPositioning": 1,
    "Exif_IFD_Pointer": 184,
    "THUMBNAIL": {
        "Compression": 6,
        "XResolution": "180/1",
        "YResolution": "180/1",
        "ResolutionUnit": 2,
        "JPEGInterchangeFormat": 1524,
        "JPEGInterchangeFormatLength": 5342
    },
    "ExposureTime": "1/350",
    "FNumber": "40/10",
    "ExifVersion": "0210",
    "DateTimeOriginal": "2001:06:09 15:17:32",
    "DateTimeDigitized": "2001:06:09 15:17:32",
    "ComponentsConfiguration": [
        1,
        2,
        3,
        0
    ],
    "CompressedBitsPerPixel": "3/1",
    "ShutterSpeedValue": "553859/65536",
    "ApertureValue": "262144/65536",
    "ExposureBiasValue": "0/3",
    "MaxApertureValue": "194698/65536",
    "SubjectDistance": "3750/1000",
    "MeteringMode": 2,
    "Flash": 0,
    "FocalLength": "346/32",
    "MakerNote": "\n",
    "UserComment": "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000",
    "FlashPixVersion": "0100",
    "ColorSpace": 1,
    "ExifImageWidth": 640,
    "ExifImageLength": 480,
    "InteroperabilityOffset": 1088,
    "FocalPlaneXResolution": "640000/206",
    "FocalPlaneYResolution": "480000/155",
    "FocalPlaneResolutionUnit": 2,
    "SensingMethod": 2,
    "FileSource": 3,
    "InterOperabilityIndex": "R98",
    "InterOperabilityVersion": "0100",
    "RelatedImageWidth": 640,
    "RelatedImageHeight": 480,
    "ModeArray": [
        38,
        2,
        0,
        3,
        1,
        0,
        0,
        1,
        0,
        1,
        2,
        1,
        0,
        0,
        0,
        0,
        0,
        0,
        1
    ],
    "UndefinedTag:0x0002": [
        2,
        346,
        211,
        158
    ],
    "UndefinedTag:0x0003": [
        0,
        0,
        0,
        0
    ],
    "ImageInfo": [
        30,
        0,
        140,
        258,
        128,
        270,
        0,
        0,
        0,
        1,
        4,
        0,
        0,
        0,
        12290
    ],
    "UndefinedTag:0x0000": [
        0,
        0,
        0,
        0,
        0,
        0
    ],
    "ImageType": "IMG:JPEG file",
    "FirmwareVersion": "Firmware Version 1.0",
    "ImageNumber": 1010163,
    "OwnerName": "Tom Rowan and Sarah Clifton",
    "UndefinedTag:0x0010": 100925440
}

← All articles