Change label in png file.

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
batjka911
Posts: 8
Joined: 2019-02-03T11:12:43-07:00
Authentication code: 1152
Location: Kaliningrad,Russia

Change label in png file.

Post by batjka911 »

I'm use Linux.

Code: Select all

magick -version    
Version: ImageMagick 7.0.8-25 Q16 x86_64 2019-01-28 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenCL OpenMP 
Delegates (built-in): bzlib cairo fontconfig freetype gslib heic jbig jng jp2 jpeg\
lcms lqr ltdl lzma openexr pangocairo png ps raw rsvg tiff webp wmf x xml zlib
I have png file (with transparent background) extracted from psd.

Code: Select all

identify -verbose test_7.png 
Image: test_7.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 836x131+0+0
  Resolution: 72x72
  Print size: 11.6111x1.81944
  Units: PixelsPerInch
  Colorspace: Gray
  Type: GrayscaleAlpha
  Base type: Undefined
  Endianess: Undefined
  Depth: 8-bit
  Channel depth:
    Gray: 8-bit
    Alpha: 8-bit
  Channel statistics:
    Pixels: 109516
    Gray:
      min: 54  (0.211765)
      max: 54 (0.211765)
      mean: 54 (0.211765)
      standard deviation: 0 (0)
      kurtosis: -2.90448e+53
      skewness: 5.53906e+36
      entropy: 0
    Alpha:
      min: 0  (0)
      max: 255 (1)
      mean: 84.2985 (0.330582)
      standard deviation: 118.02 (0.462824)
      kurtosis: -1.45103
      skewness: 0.720372
      entropy: 0.305547
  Alpha: graya(54,0)   #36363600
  Colors: 17
  Histogram:
     71162: ( 54, 54, 54,  0) #36363600 graya(54,0)
     34096: ( 54, 54, 54,255) #363636FF graya(54,1)
       760: ( 54, 54, 54, 64) #36363640 graya(54,0.25098)
       707: ( 54, 54, 54,192) #363636C0 graya(54,0.752941)
       676: ( 54, 54, 54,128) #36363680 graya(54,0.501961)
       239: ( 54, 54, 54, 16) #36363610 graya(54,0.0627451)
       234: ( 54, 54, 54,240) #363636F0 graya(54,0.941176)
       205: ( 54, 54, 54, 32) #36363620 graya(54,0.12549)
       193: ( 54, 54, 54, 96) #36363660 graya(54,0.376471)
       186: ( 54, 54, 54,224) #363636E0 graya(54,0.878431)
       184: ( 54, 54, 54,160) #363636A0 graya(54,0.627451)
       169: ( 54, 54, 54, 48) #36363630 graya(54,0.188235)
       162: ( 54, 54, 54,208) #363636D0 graya(54,0.815686)
       145: ( 54, 54, 54,144) #36363690 graya(54,0.564706)
       144: ( 54, 54, 54,112) #36363670 graya(54,0.439216)
       136: ( 54, 54, 54, 80) #36363650 graya(54,0.313725)
       118: ( 54, 54, 54,176) #363636B0 graya(54,0.690196)
  Rendering intent: Perceptual
  Gamma: 0.45455
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Matte color: grey74
  Background color: white
  Border color: srgb(223,223,223)
  Transparent color: none
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 836x131+83+257
  Origin geometry: +83+257
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2019-02-05T00:19:40+02:00
    date:modify: 2019-02-05T00:19:40+02:00
    dc:format: application/vnd.adobe.photoshop
    exif:ColorSpace: 1
    exif:DateTime: 2014:09:08 15:46:00
    exif:ExifOffset: 164
    exif:Orientation: 0
    exif:PixelXDimension: 1000
    exif:PixelYDimension: 1000
    exif:ResolutionUnit: 2
    exif:Software: Adobe Photoshop CC (Windows)
    exif:thumbnail:Compression: 6
    exif:thumbnail:JPEGInterchangeFormat: 302
    exif:thumbnail:JPEGInterchangeFormatLength: 0
    exif:thumbnail:ResolutionUnit: 2
    exif:thumbnail:XResolution: 72/1
    exif:thumbnail:YResolution: 72/1
    exif:XResolution: 72/1
    exif:YResolution: 72/1
    icc:copyright: Copyright (c) 1998 Hewlett-Packard Company
    icc:description: sRGB IEC61966-2.1
    icc:manufacturer: IEC http://www.iec.ch
    icc:model: IEC 61966-2.1 Default RGB colour space - sRGB
    label: John smith
    photoshop:ColorMode: 3
    photoshop:ICCProfile: sRGB IEC61966-2.1
    png:bKGD: chunk was found (see Background color, above)
    png:caNv: width=836, height=131, x_offset=83, y_offset=257
    png:cHRM: chunk was found (see Chromaticity, above)
    png:gAMA: gamma=0.45455 (See Gamma, above)
    png:IHDR.bit-depth-orig: 8
    png:IHDR.bit_depth: 8
    png:IHDR.color-type-orig: 4
    png:IHDR.color_type: 4 (GrayAlpha)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 836, 131
    png:pHYs: x_res=2834, y_res=2834, units=1
    png:sRGB: intent=0 (Perceptual Intent)
    png:text: 19 tEXt/zTXt/iTXt chunks were found
    png:tIME: 2014-09-08T18:46:02Z
    signature: fb07b54ff414f855230a3dc5db03e4afde81087f021523fd681213cd71c9c05c
    tiff:XResolution: 72
    tiff:YResolution: 72
    xmp:CreateDate: 2014-09-08T07:05:42-04:00
    xmp:CreatorTool: Adobe Photoshop CC (Windows)
    xmp:MetadataDate: 2014-09-08T15:46-04:00
    xmp:ModifyDate: 2014-09-08T15:46-04:00
    xmpMM:DocumentID: xmp.did:ec8f28e2-adfd-5f40-91e3-718da82328f8
    xmpMM:InstanceID: xmp.iid:ec8f28e2-adfd-5f40-91e3-718da82328f8
    xmpMM:OriginalDocumentID: xmp.did:ec8f28e2-adfd-5f40-91e3-718da82328f8
  Profiles:
    Profile-exif: 308 bytes
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 10750B
  Number pixels: 109516
  User time: 0.000u
  Elapsed time: 0:01.000
  Version: ImageMagick 7.0.8-25 Q16 x86_64 2019-01-28 https://imagemagick.org
We have label: John smith from this listing.

Is it possible to replace this label with (for example) my nickname-batjka911 in PNG with IM tools?
I read such a post,but this is another case.
Or only by Photoshop/Gimp?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Change label in png file.

Post by fmw42 »

You can do that as follows, but you need to know the font that was used. PNG does not store fonts used in the raster image. So I have just chosen one below.

Code: Select all

magick img.png -set option:dims "%wx%h" \
\( -size "%[dims]" -background black -fill white -font ubuntu -gravity center label:'batjka911' \) \
-alpha off -compose copy_opacity -composite \
result.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change label in png file.

Post by snibgo »

I think the OP doesn't want to change the image, but just the metadata property "label", like this:

Code: Select all

magick in.png -set label batjka911 out.png
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Change label in png file.

Post by fmw42 »

snibgo wrote: 2019-02-04T18:31:09-07:00 I think the OP doesn't want to change the image, but just the metadata property "label", like this:

Code: Select all

magick in.png -set label batjka911 out.png
You are probably right about that. But I do not see any label or comment in the image Properties with identify -verbose, though he shows that in his listing. Perhaps due to IMGUR changing his image.
batjka911
Posts: 8
Joined: 2019-02-03T11:12:43-07:00
Authentication code: 1152
Location: Kaliningrad,Russia

Re: Change label in png file.

Post by batjka911 »

snibgo wrote: 2019-02-04T18:31:09-07:00 I think the OP doesn't want to change the image, but just the metadata property "label", like this:

Code: Select all

magick in.png -set label batjka911 out.png
It works to change metadata property "label", but I needed to change the label content.
fmw42 wrote: 2019-02-04T18:03:39-07:00 You can do that as follows, but you need to know the font that was used. PNG does not store fonts used in the raster image. So I have just chosen one below.

Code: Select all

magick img.png -set option:dims "%wx%h" \
\( -size "%[dims]" -background black -fill white -font ubuntu -gravity center label:'batjka911' \) \
-alpha off -compose copy_opacity -composite \
result.png
Font.txt was in folder with psd file.
Text with this font-up to top. orig_font.png
Text with Roboto font. roboto_font.png

How to play with the font, font size and how to distribute the text across the entire width in your code?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Change label in png file.

Post by fmw42 »

You did not mention a PSD file. Imagemagick cannot read the text font from a PSD file. My command will give the closest that it can to stretching the text to fill the dimension of the image for the given font used and the characters in the text. You do not need to specify font size, only the dimensions of the image that you want to fill and the font. Imagemagick label will do the rest to optimize the font size so that it fills as best that it can to those dimensions. See label at https://imagemagick.org/Usage/text/. You may specify any two of width, height and pointsize. If you want, you can use any of the other methods, such a -annotate if you want more user control. But then you must decide on the pointsize. You should use -gravity center rather than gravity north (top). You can then -trim +repage, if you want a closer fit and do not mind a smaller image.

Please clarify further what you want, if my code does not work.
batjka911
Posts: 8
Joined: 2019-02-03T11:12:43-07:00
Authentication code: 1152
Location: Kaliningrad,Russia

Re: Change label in png file.

Post by batjka911 »

fmw42 wrote: 2019-02-04T21:33:05-07:00 if my code does not work.
Your code works great.

The question was from the desire to quickly use the logo template with my text.
But I realized that it was better to do it from scratch for more control.As they say at us:"Quickly even cats are not born".

Excellent as always. Thanks a lot.
Post Reply