Layer images and crop a sprite with stars for review badge

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
lightframes
Posts: 1
Joined: 2016-02-17T01:59:17-07:00
Authentication code: 1151

Layer images and crop a sprite with stars for review badge

Post by lightframes »

Hi everybody,
i'm trying to figure out how to solve this problem but after several hours I couldn't get it.
I've ImageMagick 6.7.3-6 2011-11-18 Q16


I have 2 images, one used for background (http://www.esploro.it/images_reviews_te ... hopbox.png)
and one sprite with all stars votes (http://www.esploro.it/images_reviews_te ... ar-def.png) that i want to put over the background image. Before putting the stars over the background i want to crop the image on the right star sequences depending on the the votes the shop have.

Actually I'm using this command

Code: Select all

convert tn-sg-shopbox.png -page +67+174 sprite-star-def.png -background transparent -layers flatten  box-complete-test.png
and this is the result

http://www.esploro.it/images_reviews_te ... e-test.png

Obviously there should be only one row of stars with the votes corresponding to the toal votes of the shop.

My problem is hot to crop the sprite stars before marging with the background.

Thank you in advance
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Layer images and crop a sprite with stars for review badge

Post by Bonzo »

Why complicate things with one image; how much space would eleven images take and it makes things a lot easier?

Your code is going to be more complicated as you will need to supply the image location to the -crop command for each of the eleven possible positions.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Layer images and crop a sprite with stars for review badge

Post by snibgo »

I would do the basic command like this. Windows BAT syntax.

Code: Select all

convert ^
  tn-sg-shopbox.png ^
  sprite-star-def.png ^
  -composite ^
  out.png
However, you don't want the entire stars image, but just one row. There are 11 rows of stars. The image has 275 rows of pixels, so 25 pixel rows per star row. Suppose you want the third row of stars from the top. You want a crop of the entire width, height 25 pixels, starting at x=0 and y=2*25 = 50.

Code: Select all

convert ^
  sprite-star-def.png ^
  -crop x25+0+50 +repage ^
  onerow.png
We can combine this into one command.

I don't know where you want the row of stars. I have put it centrally left-right, 80 pixels down from the top.

Code: Select all

convert ^
  tn-sg-shopbox.png ^
  ( sprite-star-def.png -crop x25+0+50 +repage ) ^
  -gravity North ^
  -geometry +0+80 ^
  -composite ^
  out.png
As Bonzo says, instead of doing the crop as needed for each image, you could make eleven images from crops, and then choose the correct one when you need it.
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Layer images and crop a sprite with stars for review badge

Post by GeeMack »

lightframes wrote:My problem is hot to crop the sprite stars before marging with the background.
There are some good suggestions already posted. Here's another idea, again using Windows BAT syntax...

Code: Select all

set VOTES=6

convert ^
   sprite-star-def.png ^
   -crop 1x11@ ^
   +repage ^
   -write mpr:stars ^
   -delete 0--1 ^
   tn-sg-shopbox.png ^
   mpr:stars[%VOTES%] ^
   -gravity north ^
   -geometry +0+84 ^
   -composite ^
      output.png
This is how it works...

First you have to get your number of votes into a variable. In this example "%VOTES%" will be 6.

The convert command starts by bringing in your stars sprite and crops it into 11 equal rows. Next it writes all those parts to a memory register named "mpr:stars" and deletes everything from the working stack. Now your "mpr:stars" contains your rows of stars at indexes 0 through 10.

Continue by bringing in your background image. Then get the row of stars that matches the number of votes from the memory register with "mpr:stars[%VOTES%]". Set the gravity to "north" and define the geometry to "+0+84" so your composite will be centered from left to right, and located 84 pixels down from the top.

Finish by doing the "-composite" to lay the stars on the background, and send it to the output file "output.png".
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Layer images and crop a sprite with stars for review badge

Post by snibgo »

GeeMack wrote:... mpr:stars[%VOTES%] ...
Very clever! Award yourself a Gold Star, and a tick.

[EDIT: This forum changed my word "G o l d" into ???? I have no idea why, and it spoils the joke. Grrr.]
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Layer images and crop a sprite with stars for review badge

Post by Bonzo »

Probably as it is a spam word as people sell it for use in games.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Layer images and crop a sprite with stars for review badge

Post by Bonzo »

While eating my tea I had another idea - Make the stars on the sprite spaced a lot further apart vertically so only one set would be on the image at the same time. When you composite the images only the ones on the base image will be seen and the rest will be cut off.
Post Reply