3

I'm using tikz to draw a square and tcolorbox skins to fill the square with an image. The problem is when I try to rotate the image.

\documentclass[margin=5cm]{standalone}
\usepackage{tikz}
\usepackage[skins]{tcolorbox}
\begin{document}
\begin{tikzpicture}
    \path (0,0) node[rectangle,draw=none,minimum size=2cm, fill overzoom image=cat.png] {1};
    \path (2,0) node[rectangle,draw=none,minimum size=2cm, fill overzoom image*={angle=30}{cat.png}] {2};
    \draw[draw=none,rotate=30,fill overzoom image={cat.png}] (3,0) rectangle (4.5,-1.5) ;
    \draw (4,1) node{3};
    \draw[draw=none,rotate=30,fill shrink image={cat.png}] (4.5,0) rectangle (6.5,-2) ;
    \draw (5.5,2) node{4};
\end{tikzpicture}
\end{document}

Which gives me: enter image description here

The edges of [2] and [4] aren't smooth, whereas [3] crops the picture. What I want is to rotate 1.

  • I would not worry too much about the aliased edge, because it appears to be related to the rendering of your viewer. When zooming on the edge in a pdf viewer, the aliased pixelation will shrink too. That said, note that [2] will not work well for non-square images, because you are only rotating the image, but not the square that is clipping your image. Note furthermore that you can also rotate a node: e.g. you could add rotate=30 to [1] and replace overzoom for shrink to replicate the result of [4] (but it won't solve the aliasing "issue"). – JJM Driessen Jun 14 at 12:35
  • @JJMDriessen I want to print and frame the finished document. Also, I'm cropping the images into squares before I use them. – Hipponax43 Jun 14 at 12:56
  • In that case, did you make a test print and was the aliasing visible? – JJM Driessen Jun 14 at 13:15
  • @JJMDriessen Yes – Hipponax43 Jun 14 at 13:28
2

When rotating a (bitmap/pixelized) picture, its edges may appear aliased, depending on the viewer, whereas vector-based edges (such as those from tikz) appear anti-aliased.

The picture's edges appear anti-aliased if they are cropped or bounded by vector-based edges. In your solutions (2) and (4), the image is not fully cropped or bounded, so (parts) of its edges appear aliased, as shown below (rectangle edges drawn with draw=red):

original with red edges

Having the node edge bound (coincide with) the image can be achieved using path picture and includegraphics options, which provide more controls than fill shrink image and fill overzoom image.

In particular, for the square image we can do something like:

\documentclass{article}
\usepackage{tikz}
\begin{document}

% \placesquareimage{imagefile}{size}{xcoordinate,ycoordinate}{rotation}
\newcommand{\placesquareimage}[4]{
    \node[rotate=#4,rectangle,minimum size=#2,draw=none,path picture={
        \node[transform shape] at (path picture bounding box.center){
            \includegraphics[height=#2]{#1}
        };
    }] at (#3) {};
}

\begin{tikzpicture}
% aliasing not visible
\placesquareimage{example-grid-100x100pt.jpg}{2cm}{0,0}{30};
\placesquareimage{example-grid-100x100pt.jpg}{1.5cm}{1.5,0.5}{-30};
% aliasing visible, by making the rectange slightly larger (only change: minimum size=2cm --> 2.1cm)
\node[rotate=30,rectangle,minimum size=2.1cm,draw=none,path picture={
    \node[transform shape] at (path picture bounding box.center){
        \includegraphics[height=2cm]{example-grid-100x100pt.jpg}
    };
}] at (4,0) {};
\end{tikzpicture}

\end{document}

On the right side a rectangle that is intentionally larger to illustrate the difference. solution

  • Thanks for that. I had guessed that a possible solution would be to bound the image but I couldn't work out how to do it. – Hipponax43 Jun 17 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.