AI, Analytics & Data Science: Towards Analytics Specialist

AI, Analytics & Data Science: Towards Analytics Specialist

Share this post

AI, Analytics & Data Science: Towards Analytics Specialist
AI, Analytics & Data Science: Towards Analytics Specialist
Basic Image Manipulations in R: Resizing (Scaling), Rotating, and Cropping

Basic Image Manipulations in R: Resizing (Scaling), Rotating, and Cropping

Dr Nilimesh Halder's avatar
Dr Nilimesh Halder
Aug 23, 2025
∙ Paid
1

Share this post

AI, Analytics & Data Science: Towards Analytics Specialist
AI, Analytics & Data Science: Towards Analytics Specialist
Basic Image Manipulations in R: Resizing (Scaling), Rotating, and Cropping
1
Share

This article provides a complete guide to resizing, rotating, and cropping images in R, enabling you to develop efficient preprocessing workflows for computer vision, machine learning, and data visualisation tasks.

Article Outline:

  1. Introduction

    • Importance of fundamental image manipulations in data analysis, machine learning, and visualisation tasks.

    • Overview of resizing, rotating, and cropping as building blocks for preprocessing.

  2. Setting Up the R Environment

    • Installing and loading required packages (magick, imager, and ggplot2 for visualisation).

    • Brief explanation of how images are represented in R (raster objects, arrays, or magick objects).

  3. Creating or Loading an Image

    • Generating a synthetic example image using R graphics functions for reproducibility.

    • Alternative methods: loading a file from disk with magick::image_read.

  4. Resizing (Scaling) Images in R

    • Using magick::image_scale and imager::resize functions.

    • Preserving aspect ratio when scaling.

    • Examples of upscaling and downscaling.

  5. Rotating Images in R

    • Rotating images by fixed and arbitrary angles.

    • Handling background fill colour during rotations.

    • Visualising the differences between 90°, 180°, and arbitrary rotations.

  6. Cropping Images in R

    • Defining regions of interest (ROI) with pixel coordinates.

    • Using magick::image_crop and imager::crop.bbox.

    • Practical considerations such as centering and bounding-box cropping.

  7. End-to-End Example Workflow

    • Step-by-step pipeline: create/load → resize → rotate → crop → save and display results.

    • Combining transformations into a reproducible workflow.

  8. Common Issues and Troubleshooting

    • Dealing with distorted aspect ratios.

    • Pixel coordinate systems and off-by-one indexing.

    • Performance considerations with large images.

  9. Conclusion and Next Steps

    • Recap of resizing, rotating, and cropping with R.

    • Directions for advanced manipulations such as color transformations and filtering.

Introduction

Image manipulation is a cornerstone of modern data analysis, computer vision, and machine learning workflows. Even the simplest operations such as resizing, rotating, and cropping play critical roles in preprocessing raw image data into consistent, structured forms suitable for analysis or predictive modeling. In R, a language primarily known for statistical computing and data visualisation, the ability to process and manipulate images has become increasingly important. Libraries such as magick and imager extend R’s utility beyond numbers and charts, enabling practitioners to directly interact with image data.

This article explores the fundamentals of resizing (scaling), rotating, and cropping images in R. We will construct a simulated image dataset, demonstrate transformations, and provide an end-to-end reproducible workflow. Along the way, we will discuss conceptual aspects, practical challenges, and best practices.


AI, Analytics & Data Science: Towards Analytics Specialist is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.


Keep reading with a 7-day free trial

Subscribe to AI, Analytics & Data Science: Towards Analytics Specialist to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 Nilimesh Halder
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share