# Edit PNG PLTE

**URL:** <https://discourse.julialang.org/t/edit-png-plte/51305>\
**Category:** General Usage\
**Created:** [December 5, 2020, 3:32pm UTC](https://discourse.julialang.org/t/edit-png-plte/51305 "2020-12-05T15:32:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HugoTrentesaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugotrentesaux/32/12583_2.png) [@HugoTrentesaux](https://discourse.julialang.org/u/HugoTrentesaux)\
**Post date:** [December 5, 2020, 3:32pm UTC](https://discourse.julialang.org/t/edit-png-plte/51305/1 "2020-12-05T15:32:45Z")

</div>

The [PNG format](https://en.wikipedia.org/wiki/Portable_Network_Graphics) specifies an optional color palette (PLTE chunk). I want to edit this palette with Julia but do not find a starting point. Is there an existing library which provides this kind of “low level” control over PNG files?

---

<div class="post-metadata">

**Author:** ![ianshmean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ianshmean/32/216042_2.png) [@ianshmean](https://discourse.julialang.org/u/ianshmean)\
**Post date:** [December 5, 2020, 3:36pm UTC](https://discourse.julialang.org/t/edit-png-plte/51305/2 "2020-12-05T15:36:31Z")

</div>

Check out [https://github.com/JuliaIO/PNGFiles.jl](https://github.com/JuliaIO/PNGFiles.jl)

What you want may not be exported, but should be there

---

<div class="post-metadata">

**Author:** ![HugoTrentesaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugotrentesaux/32/12583_2.png) [@HugoTrentesaux](https://discourse.julialang.org/u/HugoTrentesaux)\
**Post date:** [December 5, 2020, 5:39pm UTC](https://discourse.julialang.org/t/edit-png-plte/51305/3 "2020-12-05T17:39:10Z")

</div>

Ok, I see. Thanks. I think it uses ImageMagick internally and that I won’t be able to edit the palette with it.  
As an alternative, I tried to modify the palette bits directly but I have to recompute the CRC checksum to avoid breaking the file. ([PNG Specification: Chunk Specifications](https://www.w3.org/TR/PNG-Chunks.html))

> **(edit) my attempt using CRC**
>
> ```julia
> f=open("img.png","r") # open image
> raw=read(f); close(f) # read raw data
> raw[804:806]=[UInt8(255),UInt8(255),UInt8(255)] # replace one color of palette
> raw[810:813] = reinterpret(UInt8, [crc32(raw[38:809])]) # compute and replace checksum
> f=open("img.png","w"); write(f,raw) # overwrite
> close(f)
> 
> ```

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [December 5, 2020, 5:48pm UTC](https://discourse.julialang.org/t/edit-png-plte/51305/4 "2020-12-05T17:48:38Z")

</div>

> [@HugoTrentesaux](#):
>
> I think it uses ImageMagick internally and that I won’t be able to edit the palette with it.

PNGFiles uses libpng, in case that helps.

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [December 5, 2020, 5:50pm UTC](https://discourse.julialang.org/t/edit-png-plte/51305/5 "2020-12-05T17:50:26Z")

</div>

There are a few functions related to PLTE:  
[https://github.com/JuliaIO/PNGFiles.jl/blob/master/gen/libpng/libpng\_api.jl#L711-L717](https://github.com/JuliaIO/PNGFiles.jl/blob/master/gen/libpng/libpng_api.jl#L711-L717)

---

<div class="post-metadata">

**Author:** ![HugoTrentesaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugotrentesaux/32/12583_2.png) [@HugoTrentesaux](https://discourse.julialang.org/u/HugoTrentesaux)\
**Post date:** [December 5, 2020, 6:02pm UTC](https://discourse.julialang.org/t/edit-png-plte/51305/6 "2020-12-05T18:02:25Z")

</div>

Yes, these are api calls, using pointers. I do not feel comfortable enough to play with it. For info, I added my attempt suing CRC in my last message (does not work yet).

---

<div class="post-metadata">

**Author:** ![HugoTrentesaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugotrentesaux/32/12583_2.png) [@HugoTrentesaux](https://discourse.julialang.org/u/HugoTrentesaux)\
**Post date:** [December 7, 2020, 2:05pm UTC](https://discourse.julialang.org/t/edit-png-plte/51305/7 "2020-12-07T14:05:05Z")

</div>

Finally I’m using GIMP to edit the color palette. I have no more time to try automatizing it 😢
