Replies: 6 comments 5 replies
-
Here is one way to do that. Use parentheses processing to create the white area and use -annotate to draw the text centered in it. Then composite that over the cropped input image. I took some random larger image since you did not provide one.
|
Beta Was this translation helpful? Give feedback.
-
I suppose you are using bash. That is a question about bash, not ImageMagick. An example of setting a variable, and using the value of the variable:
The bash interpreter does the job of translating from |
Beta Was this translation helpful? Give feedback.
-
I plan to use it in Python but your suggestions work in bash, as in my test bash below:
Thanks for your help. Next, I will try in Python. |
Beta Was this translation helpful? Give feedback.
-
While I read up on the options you use in the command, I think the option "+repage" is the key to solving the problem with my original command. It seems the software keeps track of the last reference point on the image. This option resets it to (0,0). From there, the "-gravity center" option puts the reference point back to the center of the image. Without "+repage", the reference point is elsewhere and therefore "-draw text..." would not place text where you think it should be. Also, "text 0,476..." places text off the image since the reference point is at 486/2=243. This needs to be "text 0,223..." (20 pixels off from the image bottom) to place text at the center of the rectangular box with 40 pixel height. I updated my original command as below and it works:
Is my understanding correct of how this software works with respect to reference point and resetting it? |
Beta Was this translation helpful? Give feedback.
-
That's not quite correct. Your "reference point" is really the "image offset on the canvas". Imagine that an IM image is printed on a piece of paper, and that paper is glued onto a blank canvas (like a painting canvas). Normally, the canvas is the same size as the paper. When we The same applies to For example, where
We crop toes.png, then crop the result. After the first crop, the image is 50x50 pixels as expected. But the virtual canvas is The second crop is 20x20 pixels, at an offset of Now do the same, but with with
The first repage removes the virtual canvas metadata. In effect, the canvas is Some file formats (eg PNG and MIFF) record the entire canvas metadata. Some (eg TIFF) record only offsets. Most (eg JPG) don't record any canvas metadata. Why does IM do this? Because we might chop an image into many pieces, and do some operations on those pieces, then reassemble them. From the canvas offsets, the pieces will be put back where they came from. |
Beta Was this translation helpful? Give feedback.
-
Thanks for taking time to explain. I think the virtual canvas concept helps a lot with respect to offsets. I am new to IM (as of yesterday) and not quite grasp the concept of how IM works. Your explanation can be a good intro material for that. I looked into online document for draw text, until now I am not sure how it places the text even though it displays what I want. It appears to use the center of the text box for placement and not its top left corner. For this task, I can either go with Python/OpenCV or Python with IM via os.system(). I wonder what your thoughts are on performance and efficiency. Thanks. |
Beta Was this translation helpful? Give feedback.
-
Hello,
I am using ImageMagick 6.9.11-60 on Ubuntu 22.04LTS and would like to crop an image, create a white rectangular box at the bottom (inside the image), and add black text at the center of this box. Below are the steps I do with specifics:
1). Crop an image with 445x446 dimensions:
$ convert in.jpg -crop 445x486+1327+534 out.jpg ; (this step works)
2). Draw a white rectangular box of 445x40 at the bottom of out.jpg:
$ convert -fill white -draw 'rectangle 0,446,445,486' out.jpg rect_out.jpg ... (this step works)
3). Place black text at the center of the white rectangular box:
$ convert -font helvetica -fill black -pointsize 20 -gravity center -draw "text 0,476 'TEXT TO BE DISPLAYED'" rect_out.jpg Image_output.jpg ...(this step does not place any text)
The top left hand corner of my white rectangular box is at (0,446). To vertically center my text of 20 point size, I use "text 0,476..", so I should have 10 px top and bottom margin. With this setting, no text is displayed. However, after trial and error, with the setting of "text 0, 223..", the text appears centered in the white rectangular. Any idea as to why?.
[....the number 223 came from image height/2 minus white box height/2; or, (486/2)-(40/2)... ]
Also, is there a more efficient way to do this without using intermediate image files, i.e., in one command or some other methods?. My application is to crop a large number of images, so fast processing time is important.
Any help would be greatly appreciated. Thanks.
Beta Was this translation helpful? Give feedback.
All reactions